SoFunction
Updated on 2025-03-08

SpringBoot @Test unit testing method

1. Ordinary test

1. Preliminary understanding: springboot generally uses maven to build projects, and there is a test package in maven projects (although test cases can exist under src, the specifications are unified in test)

Our test package can synchronize the package structure under src and write test use cases for the corresponding java class. This is very important when doing unit testing.

2. A simple test case, just like the main method

public class UtilTest {
    @Test
    public void currencyTest() throws Exception{
        String uuid = MD5Util.getMd5("Test Cases");
 
        (uuid);
    }
}

Note that we used this annotation@Test,Depend onJUnitProvided, it goes back to scan the method with this annotation to call it, thereby achieving the corresponding effect

2. SpringBoot call test

1. When we call the controller, service, and mapper layers in springboot, we need to inject them, and we need the relevant environment of springboot.

2. Test cases:

 
@RunWith()
@SpringBootTest(classes = )
public class CatTest {
    @Autowired
    private ElasticDao elasticDao;
    @Autowired
    private testService testService;
 
    @Test
    public void testEs() throws Exception{
        ResponseEntity responseEntity = (,"test","test");
        (responseEntity);
    }
 
    @Test
    public void testTestService() throws Exception{
        ResultEntity ResultEntity = ("1");
        (ResultEntity);
    }
}

Here we have added two annotations@RunWithand@SpringBootTest, you can enter SpringBootTest to view this code to confirm the web environment. Generally, we willclasses point to the startup class

 webEnvironment() default ;

3. Junit4 unit test

This plugin that relies on idea is Junit

  • 1. Select the java class to test
  • 2. Press and hold the alt+insert key
  • 3. Choose Junit Test
  • 4. Choose Junit4
  • 5. Generate under the test corresponding package

The test content can be selected as the above direct call method body, or it can be a Mock object provided by Junit

/**
      * Simulate mvc test object
      */
    private MockMvc mockMvc;
 
    /**
      * web project context
      */
    @Autowired
    private WebApplicationContext webApplicationContext;
 
    /**
      * All test methods are executed before they are executed
      */
    @Before
    public void before() {
        //Get mock object        mockMvc = (webApplicationContext).build();
    }

Compare whether the test results meet expectations and can be used

("Expected Results", "Actual results");
();
();
();
();

We can useJacocoto obtain branch coverage of our unit tests

I won't describe it too much here. If you are interested, you can learn about it.

4. Dependence

<dependency>
	<groupId></groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>
<dependency>
	<groupId></groupId>
	<artifactId>spring-boot-test</artifactId>
	<version>2.1.</version>
</dependency>
<dependency>
	<groupId>junit</groupId>
	<artifactId>junit</artifactId>
	<version>4.12</version>
</dependency>

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.