JavaEE之单元测试

创建工程

Gradle->[Java,Web]->[GroupId:com.xuan,Artifactld:projectName]->[Use auto-import,Creat dir…..lly]

添加测试

选中要测试的->ctrl+shift+t

被测试代码

1
2
3
4
5
6
public class JunitHello {
public String printHello(){
System.out.printf("hello junit");
return "hello junit";
}
}

测试代码(目录test/java下有效)

1
2
3
4
5
6
7
public class JunitHelloTest {
@Test
public void printHello() throws Exception {
JunitHello junitHello=new JunitHello();
assertEquals(junitHello.printHello(),"hellojunit");
}
}

测试结果

1
2
3
4
5
6
7
8
9
hello junit
org.junit.ComparisonFailure:
Expected :hello junit
Actual :hellojunit
<Click to see difference>

at com.xuan.test.JunitHelloTest.printHello(JunitHelloTest.java:14)

Process finished with exit code -1
注意

自动导入了junit的包、测试代码只能在\test目录下才能使用,不然招不到junit的包,不需要添加JUnitGenerator V2.0自动生成测试模块插件