This is the 25th day of my participation in the Novembermore Challenge. SpringBoot2.0 introduced JUnit5 as our testing tool. What’s the difference between JUnit4 and JUnit5?

To use tests now, just create a Test class and add the @SpringBooTtest annotation on the class and the @test annotation on the Test method

@SpringBootTest
class SpringBoot01ApplicationTests {

    @Test
    void testRedis(){
        
    }


}
Copy the code

Let’s take you to use our new test, mainly introduce the use of this tag function

@DisplayName

@DisplayName("junit5 ") public class JunitTest5 {@DisplayName(" DisplayName ") @test void testDisplayName(){ System.out.println(1); }}Copy the code

This TAB is mainly used to mark our run TAB, and the result of the demo is shown below

@ BeforeEach and @ AfterEach

@beforeeach void testBeforeEach(){system.out.println (" start test "); }Copy the code
@aftereach void testAfterEach(){system.out.println (" end of test "); }Copy the code

Methods annotated @beforeeach are run before the test method starts, and methods annotated @aftereach are run after the test method ends

@ BeforeAll and @ AfterAll

@beforeall static void testBeforeAll()P{system.out.println (" All tests start "); } @afterall static void testAfterAll(){system.out.println (" all tests end "); }Copy the code

Our two methods were also started before the test, but are they different from the above ones? We can’t have just one test. If we have programs that need to be run in public, we can’t have a surround for every test. Add this @beforeall tag to run BeforeAll tests start and after all tests finish

@Timeout

	@SneakyThrows
    @Timeout(value = 5,unit = TimeUnit.MILLISECONDS)
    @Test
    void testTimeout(){
        Thread.sleep(100);
    }
Copy the code

The class he labeled has to be completed within the specified time or he’ll report an error

@SpringBootTest

If you are careful, you will immediately notice that we did not write the annotation @SpringBoottest on the code we just tested. Therefore, without this annotation, we cannot use SpringBoot.

	@Autowired
    UserMapper userMapper;

    @Test
    void te(){
        System.out.println(userMapper);
    }
Copy the code

Even our idea will give us hints directly

After we add the label, our Tomcat is also started

So did our date

@RepeatedTest()

	@RepeatedTest(3)
    @Test
    void testRepeatedTest(){
        System.out.println("3");
    }
Copy the code

Repeat the test annotation, we can annotate after the annotation how many times we test the class to run, it will run automatically