Ctrl+Shift+T
,然后Create a new Test
即可。
Junit4可以直接运行我们的某个方法,没有main入口函数是断然不行的。其实在org.junit.runner包下,有个JUnitCore.class,其中就有一个 标准的main方法,这就是JUnit入口函数。
Runner只是一个抽象类,表示用于运行Junit测试用例的工具,通过它可以运行测试并通知Notifier运行的结果。通常我们可以在待测方法所在的类之上使用@RunWith
注解来为这个测试类指定一个特定的Runner。Junit的默认Runnner------BlockJunit4ClassRunner。当我们不为测试类添加@RunWith
注解的时候,其实使用的就是这个Runner,它作为默认Runner只为我们提供了基本的基于Junit生命周期的测试注解。下面列出一些比较有用的Runner。
Suit------它可以一次生执行全面在多个类中的测试用例,例如:
@RunWith(Suite.class)
@SuiteClasses({Person.class, People.class})
public class TestSuitMain{
//虽然这个类是空的,但依然可以运行Junit测试,运行时,它会将Person.class和//People.class中的所有测试用命都执行一遍!
}Parameterized------在普通的单元测试中被
@RunWith(Parameterized.class)
public class TestGenerateParams{
private String greeting;
public TestGenerateParams(String greeting){
super();
this.greeting = greeting;
}
@Test
public void testParams(){
System.out.println(greeting);
}
/**
* 这里的返回的应该是一个可迭代数组,且方法必须是public static
* @return
*/
@Parameters
public static List getParams(){
return Arrays.asList(new String[][]{{"hello"},{"hi"},{"good morning"},{"how are you"}});
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。