1. The concept of unit testing
Unit testing is to write test code for the smallest functional unit. The smallest functional unit of a Java program is a method. Therefore, unit testing is a test for a Java method, and then checks the correctness of the method.
Two, Junit test framework
(1) Concept: Junit is a unit testing framework implemented in the Java language.
(2) Advantages:
- Optional test method
- Execute all test methods with one click
- Junit can generate test reports for all methods
- The test method is isolated
(3) to achieve unit testing - 1. Import the JUnit package, which is not required if the IDE has integrated the Junit framework.
- 2. Write a test method: the method must be a public non-static method with no parameters and no counters
- 3. Add @Test annotation
- 4. The test method completes the expected correctness test.
- 5. Run the test method. (Green - passed, red - failed)
Business code
public class UserService {
public String login(String loginname,String password){
if ("admin2".equals(loginname) && "123456".equals(password)) return "登录成功";
else return "用户名或密码错误";
}
public void selectName(){
System.out.println(10 / 0);
System.out.println("查询全部用户名");
}
}
test code
public class TestDemo{
@Test
public void testLogin(){
UserService service = new UserService();
String rs = service.login("admin","123456");
//预期结果的测试Assert——断言测试
Assert.assertEquals("功能业务出错!!","登录成功",rs);
}
@Test
public void testSelectName(){
UserService service = new UserService();
service.selectName();
}
}
Effect output 1
org.junit.ComparisonFailure: 功能业务出错!!
Expected :登录成功
Actual :用户名或密码错误
<Click to see difference>
at org.junit.Assert.assertEquals(Assert.java:117)
at com.zengoo.junit.TestDemo.testLogin(TestDemo.java:18)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
One-key test
Effect diagram
(4) Junit common annotations (Junit 4.0 and above)
annotation | illustrate |
---|---|
@Test | Test Methods |
@Before | Modified instance method, executed before each test method is executed |
@After | Modified instance method, executed after each test method is executed |
@BeforeClass | Static decoration method, executed only once before all test methods are executed |
@AfterClass | Static decoration method, executed only once after all test methods are executed |