Springboot integrates Junit5 common annotations

@Test

After the Springboot2.0 version, Junit is the version of 5. A few, just add @Test to the method

@Test
void fun1(){
    
    
    int res = 1+1;
    Assertions.assertEquals(2,res);
    System.out.println(123);
}

@BeforeEach & @AfterEach

It will be executed every time @Test is executed

 @BeforeEach
    void before(){
    
    
        System.out.println("before");
    }

    @AfterEach
    void after(){
    
    
        System.out.println("after");
    }

    @Test
    void fun1(){
    
    
        int res = 1+1;
        Assertions.assertEquals(2,res);
        System.out.println(123);
    }
    @Test
    void fun2(){
    
    

        System.out.println(456);
    }

@BeforeAll & @AfterAll

No matter how many @Tests there are, they are executed only once, and the methods under @BeforeAll & AfterAll must be static methods

@BeforeAll
static void init(){
    
    
    System.out.println("init");
}

@AfterAll
static void destory(){
    
    
    System.out.println("destory");
}

@BeforeEach
void before(){
    
    
    System.out.println("before");
}

@AfterEach
void after(){
    
    
    System.out.println("after");
}

@Test
void fun1(){
    
    
    int res = 1+1;
    Assertions.assertEquals(2,res);
    System.out.println(123);
}
@Test
void fun2(){
    
    

    System.out.println(456);
}

@SpringBootTest

In the SpringBoot project, when we new a class in the test class, this class may also involve other beans, and the original @Test does not involve the context of Springboot, so when we write the test class in the SpringBoot project , added @SpringBootTest on the class, will find attentive junior partner in the project selection we created Spring Initializrwhen you create SpringBoot project, there will be a Test file directory, you will see automatically created with the best of Test class @SpringBootTest

At the same time, it can also cooperate with the injection of @Autowired

@SpringBootTest  //能够初始化springboot的上下文,防止new Serv01 的对象同时依赖其他的Bean
public class Test2 {
    
    

    @Autowired
    Serv01 serv01;

    @Test
    void t1(){
    
    
        int add = serv01.add(1, 1);
        Assertions.assertEquals(2,add);
        System.out.println(11);

    }
}

Of course, running this test case with @SpringBootTest will also start our SpringBoot project

@MockBean

To achieve a simulation through mock, if our test needs to be written to the database, it may cause some irreversible operations

We will use mock to simulate these dangerous operations

Let's look at the following code

@SpringBootTest  //能够初始化springboot的上下文,防止new Serv01 的对象同时依赖其他的Bean
public class Test2 {
    
    

    @MockBean
    Serv01 serv01;

    @Test
    void t1(){
    
    
        int add = serv01.add(1, 1);
        Assertions.assertEquals(2,add);
        System.out.println(11);

    }
}

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-E0fAxy1F-1614608984966)(SpringBoot.assets/image-20210301211956830.png)]

Through the breakpoint, the variable add is actually equal to 0

The reason is that the bean of our serv01 in the SpringBoot context has been replaced by our mock, because our mock does not have any specified operations, so our int variable add will only have a default value of 0

We add some specified operations to the mock

@SpringBootTest
public class Test2 {
    
    

    @MockBean
    Serv01 serv01;

    @Test
    void t1(){
    
    
        when(serv01.add(1,1)).thenReturn(3);
        int add = serv01.add(1, 1);
        Assertions.assertEquals(2,add);
        System.out.println(11);

    }
}

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-qFa2e1lm-1614608984968)(SpringBoot.assets/image-20210301212408593.png)]

We modify the code again, add the sub method to Serv01, and then run the test

We can find that in the add method that we specify the rule, the result is 3, while the sub method that does not specify the rule defaults to the int variable as 0

If we want sub to be tested in the original way and don’t want to be affected by @MockBean, we can use @SpyBean

@SpyBean

It is between @MockBean and @Autowired. When the method is configured, it will be executed according to the configuration rule, and the test will be executed according to the original method if there is no configuration.

If we are creating a serv02 and let serv01 call the method of serv02, will the result of the operation be the same?

@Service
public class Serv02 {
    
    
    int add2(int a, int b){
    
    
        return a+b;
    }
}
@Service
public class Serv01 {
    
    

    @Autowired
    Serv02 serv02;

    public int add(int a,int b){
    
    
        return serv02.add2(a,b);
    }

    public int sub(int a,int b){
    
    
        return a-b;
    }
}
@SpringBootTest  //能够初始化springboot的上下文,防止new Serv01 的对象同时依赖其他的Bean
public class Test2 {
    
    

    @SpyBean
    Serv01 serv01;

    @MockBean
    Serv02 serv02;

    @Test
    void t1(){
    
    
        when(serv01.add(1,1)).thenReturn(3);
        int add = serv01.add(1, 1);
        int sub = serv01.sub(2,1);
        Assertions.assertEquals(3,add);
        Assertions.assertEquals(1,sub);
        System.out.println(11);

    }
}

The operation is successful, even if we mock serv02, call the add2 method of serv02 through the add method called by serv01, the operation is still successful!

Finally, according to the test specification, we generally correspond to one test for one class, but there may also be multiple tests for one class, because there may be cases where test calls multiple classes!

Guess you like

Origin blog.csdn.net/weixin_46195957/article/details/114273530