Junit framework:
Junit is an open source java unit testing framework.
Today we will introduce junit5. junit5 consists of three different sub-project modules, including Junit Platform, Junit Jupiter and Junit Vintage. It supports Java 8 and above. The editor I use here is IntelliJ IDEA. You only need to check the final result to know whether the method interface of the entire project is smooth. Each unit test case is relatively independent, started by Junit, and automatically called. No need to add additional calling statements.
Adding, deleting, and blocking test methods will not affect other test methods.
Junit is mainly used for white box testing. The steps for white box testing are as follows:
1. Test planning stage: Develop the test schedule according to the requirements specification.
2. Test design stage: According to the function of the code, test cases are manually designed for basic functional testing. According to the programming instructions, the software structure is divided and test cases are designed according to certain standardized methods.
3. Test execution phase: input test cases and get test results.
4. Test summary stage: Compare the test results with the expected results of the code, analyze the cause of the error, find and solve the error.
Next, let’s introduce the annotations inside:
@Test: Represents the test method without declaring any attributes.
@ParameterizedTest: Indicates that the method is a test method. In addition, this annotation can also allow a test method to be run multiple times using different people.
@RepeatedTest: This annotation allows the test method to customize the number of repeated runs.
@TestFactory: Indicates that a method is based on a data-driven dynamic test data source.
@Displayname: Declares a custom display name for the test class or test method.
@BeforeEach: Indicates that the specified method is run before each test method is run.
@AfterEach: Indicates that the specified method will be run after each test method is run.
@BeforeAll: Executed before all test methods of the current class, this method can contain some initialization code.
@AfterAll: Executed after all test methods of the current class.
@Disabled: Indicates that a test class or method is invalid.
Assertion:
Fail: The assertion test failed.
AssertTrue/asserFalse: Assert true or false.
AssertNull/assertNotNull: Assert is null or not null.
assertEquals/assertNotEquals: Assert that two values are not equal.
AssertArrayEquals: Assert that all array elements are the same.
AssertSame/assertNotSame: Assert whether two objects are the same.
AssertThrows/assertDoesNotThrow: Assert whether an exception is thrown.
AssertAll: Assert that multiple conditions are met;
Next we create a class and create several methods:
package test;//包机制 import java.lang.reflect.Array; public class MixedOperation { public int mixeOperation(int x,int y){ return division((x+y),y); } public int division(int x,int y){ int result=(x/y); return result; } public static void main(String[] args) { MixedOperation mixedOperation=new MixedOperation(); System.out.println(mixedOperation.mixeOperation(5,1)); } }
We create a class named MixedOperation;
package test; import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; public class MixedOperationTest { private MixedOperation mixedOperation; @BeforeEach public void init(){ mixedOperation=new MixedOperation(); } @Test public void successTest(){ System.out.println("run a test:x=4,y=2"); int result=mixedOperation.mixeOperation(4,2); Assertions.assertEquals(3,result); } /* @DisplayName("失败") public void errorTest(){` System.out.println("run a test:x=4,y=0"); ArithmeticException exception=new ArithmeticException( ArithmeticException.class -> { mixedOperation.mixeOperation(4, 0); } ); }*/ @Disabled("参数") @Test @DisplayName("参数") @ParameterizedTest @CsvSource({"6,3,3","5,2,3","6,2,4"}) public void caTest(int x,int y,int excepted){ System.out.println("run a test :x="+x+"y="+y); System.out.println(excepted); int t= mixedOperation.mixeOperation(x,y); Assertions.assertEquals(excepted,t); } @Disabled @Test public void Next(){ System.out.println("抛出一个异常"); System.out.println(Assertions.assertThrows(IllegalArgumentException.class, () ->mixedOperation.mixeOperation(2,0))); } @Disabled @Test void error(){ Assertions.assertThrows(Exception.class,()->{Assertions.assertEquals(1,0);}); } @Test void sure(){ int result=mixedOperation.mixeOperation(4,2); Assertions.assertTrue(3==result);//断言相等 } }
This is a test class we created called MixedOperationTest;
Related recommendations: "java Video Tutorial"
The above is the detailed content of Use of white-box testing framework (JUnit). For more information, please follow other related articles on the PHP Chinese website!