Created
March 10, 2014 06:26
-
-
Save sjyun/9460357 to your computer and use it in GitHub Desktop.
junit을 쓸 수 없는 상황이라면 guava를 이용한 precondition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package loggerTest; | |
import static com.google.common.base.Preconditions.*; | |
import static java.lang.System.err; | |
public class GuavaTest1 { | |
private final boolean initialzed = false; | |
public void testForNonNullArgument(final String parameter) | |
{ | |
final String localPrameter = checkNotNull(parameter, "null값은" | |
+ "허용되지 않습니다."); | |
} | |
public void testDivisorNotZero(final int divisor){ | |
checkArgument(divisor != 0, "0으로 나눌 수 없습니다."); | |
} | |
public void testArrayElement(final String[] strArray, | |
final int indexNumber){ | |
final int index = checkElementIndex(indexNumber, strArray.length, | |
"지정된 Array 요소 위치가 벗어났습니다."); | |
} | |
public void testarrayPosition(final String[] strarray, final int indexNumber){ | |
final int index = checkPositionIndex(indexNumber, strarray.length, | |
"지정된 Array 요소 위치가 벗어났습니다."); | |
} | |
public void testState(){ | |
checkState(this.initialzed, "초기화 되지 않았습니다."); | |
} | |
public static void printHeader(final String newHeaderText){ | |
err.println("\n====================="); | |
err.println("===" + newHeaderText); | |
err.println("======================="); | |
} | |
public static void main(String[] args) { | |
final GuavaTest1 test1 = new GuavaTest1(); | |
printHeader("preconditions.checkNotNull"); | |
try{ | |
test1.testForNonNullArgument(null); | |
}catch(NullPointerException npe) | |
{ | |
npe.printStackTrace(); | |
} | |
printHeader("precondition.checkArgument"); | |
try{ | |
test1.testDivisorNotZero(0); | |
}catch(IllegalArgumentException illArgEx){ | |
illArgEx.printStackTrace(); | |
} | |
printHeader("Preconditions.checkElementIndex"); | |
try{ | |
test1.testArrayElement(new String[]{"Dustin","java"},3 ); | |
}catch(IndexOutOfBoundsException ioobEx){ | |
ioobEx.printStackTrace(); | |
} | |
printHeader("Preconditions.checkPositionIndex"); | |
try{ | |
test1.testarrayPosition(new String[]{"Dustin","java"},3); | |
}catch(IndexOutOfBoundsException ioobex){ | |
ioobex.printStackTrace(); | |
} | |
printHeader("Preconditions.checkState"); | |
try{ | |
test1.testState(); | |
}catch(IllegalStateException illStateEx){ | |
illStateEx.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment