Java Precondition and Testing

197 阅读1分钟

Common Preconditions Usages:

import com.google.common.base.Preconditions
Preconditions.checkNotNull(anythingYouWantToCheckNotNull);
Preconditions.checkArgument(anythingYouWantToCheckTrue);
Preconditions.checkArgument(anythingYouWantToCheckTrue, "error Message When False");

Unit Test for Preconditions

@Test(expected = NullPointerException.class)
public void testNull() throws Exception {
    // in the function that you want to test, 
    // make the content you want to checkNotNull null:
    // e.g. Preconditions.checkNotNull(null);
    ....
}

@Test(expected = IllegalArgumentException.class)
public void testNull() throws Exception {
    // in the function that you want to test, 
    // make the content you want to checkTrue false:
    // e.g. Preconditions.checkArgument(false);
    ....
}