不知道也没去过大公司是怎么使用junit的,我只是初步了解和使用的,用注解倒是挺方便的,分享下:
1、测试由@Test注释开始
Method method=new Method();
@Test
public void say(){
System.out.println(method.say("ss","sss"));
Assert.assertEquals("say", method.say());
}
2、Fixture
@Before
public void initialize (){……}
@After
public void dispose (){……}
3、异常和测试时间
@Test(expected= IndexOutOfBoundsException.class)
public void empty() {
new ArrayList<Object>().get(1);
}
@Test(timeout = 10)
public void checkEmail(){
assertEquals(true, RegexUtil.checkEmail("add.dd@sina.com"));
}
4、忽略测试方法
@Ignore
5、测试套件
@RunWith(Suite.class)
@Suite.SuiteClasses({TestCheckEmail.class, TestTimeUtil.class})
public class CustomizeRunner{
}
完整:
public class Method {
public String say(){
return "say";
}
public int max(){
return 1;
}
public int max(int x,int y){
return x+y;
}
public String say(String name,String dialog){
return name+"say:"+dialog;
}
}
public class Test1 {
Method method=new Method();
@Test
public void say(){
System.out.println(method.say("ss","sss"));
Assert.assertEquals("say", method.say());
}
}
public class Test2 {
Method method=new Method();
@Test
public void max(){
System.out.println(method.max());
}
}
@RunWith(Suite.class)
@SuiteClasses({Test1.class,Test2.class})
public class Test12 {
}