单元测试代码质量之必备,junit4中有几个有用但是不常用的注解。
前序:
public abstract class StringUtilsExt {
public static boolean isEmpty(String str) {
return StringUtils.isEmpty(str);
}
public static boolean is(String str) {
if (StringUtils.isNotEmpty(str)) {
throw new RuntimeException();
}
return true;
}
}
@Ignore:就是忽略此代码的执行
@Ignore
public final void test() {
StringUtilsExt.is(null);
}
@Test(expected=RuntimeException.class):此单元测试期待有RuntimeException的异常
@Test(expected=RuntimeException.class)
public final void testIs() {
StringUtilsExt.is(null);
}
@Test(timeout=1):此代码执行如果超过指定的时间那么执行不通过
@Test(timeout=1)
public final void testIsEmpty() {
String str=null;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
Assert.assertTrue(StringUtilsExt.isEmpty(str));
}