When combined with JUnit, Spring Test provides an efficient and convenient testing solution, while Spring Boot Test adds slice testing and enhanced Mock capabilities on top of Spring Test.

Spring Boot Test supports three types of tests:

  1. Unit tests, method-oriented tests, often annotated with @test
  2. Functional testing, business-oriented testing, and the ability to Mock from aspect testing can also be used. Common annotations include @runwith, @SpringbooTtest, and so on
  3. Slice testing, for the difficult to test boundary function, between unit testing and functional testing, commonly annotated with @runwith, @webmvctest and so on

Key elements and support methods in the testing process are as follows:

  • Test the runtime environment and start the Spring container with @runwith and @Springboottest
  • Mock capabilities, Mockito provides Mock capabilities
  • Assertion capabilities. AssertJ, Hamcrest, and JsonPath provide assertion capabilities
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyServerTestApplication {

    public static void main(String[] args) { SpringApplication.run(MyServerTestApplication.class, args); }}Copy the code

To enable testing in SpringBoot, simply introduce the Spring-boot-starter-test dependency and use the @runwith and @Springboottest annotations to start testing.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
Copy the code

With the introduction of spring-boot-starter-test, the associated test dependent libraries will also be included

  1. JUnit, the Java testing standard, relies on version 4.12 by default (JUnit5 and JUnit4 differ greatly)
  2. Spring Test & Spring Boot Test, Test support
  3. AssertJ provides a way to stream assertions
  4. Hamcrest provides a rich matcher library of matching objects (also known as constraints or predicates)
  5. Mockito, a Mock framework, allows you to create Mock objects by type, specify specific responses based on method parameters, and support assertions for Mock invocation procedures
  6. JSONassert, which provides assertion capabilities for JSON
  7. JsonPath, which provides XPATH functionality for JSON

Unit testing

public class UserServiceTest extends MyServerTestApplication {
   
   @Autowired
   private IUserService userService;
   
   @Test
   public void testAddUser(a) {
       userService.add(buildUser("Jack".18));
   }
   
   private User buildUser(String username, int age) {
       User user = new User();
       user.setUsername(username);
       user.setAge(age);
       returnuser; }}Copy the code
  • @runwith is an annotation provided by JUnit4 that connects Spring and JUnit
  • The @SpringBooTtest replaces the @ContextConfiguration annotation in Spring-Test. The purpose of this annotation is to load the ApplicationContext, start the Spring container, and SpringBootTest automatically retrieves the configuration file. The search sequence starts with the current package and goes up the ladder to find classes annotated by @SpringBootApplication or @SpringBootConfiguration

A functional test

With @SpringBoottest, Spring will load all the managed beans, basically starting the entire service, and the test can be functionally tested

Since the Web is the most common service, the @Springboottest annotation also gives four Settings for the Environment parameters of the Web

    public static enum WebEnvironment {
        MOCK(false),
        RANDOM_PORT(true),
        DEFINED_PORT(true),
        NONE(false);

        private final boolean embedded;

        private WebEnvironment(boolean embedded) {
            this.embedded = embedded;
        }

        public boolean isEmbedded(a) {
            return this.embedded; }}Copy the code
  1. Mock, default values, and provide a Mock environment that can be used and @ AutoConfigureMockMvc or @ AutoConfigureWebTestClient collocation, open the Mock related functions, as embedded service didn’t really start, also won’t listen Web service port
  2. RANDOM_PORT, which starts a real Web service and listens on a random port
  3. DEFINED_PORT, starting a real Web service, listening on a defined port (retrieved from the Spring configuration file)
  4. NONE, launches a non-Web ApplicationContext that provides neither a Mock environment nor a real Web service

If the current service has no Web-related dependencies in its classpath, Spring will launch a non-Web ApplicationContext, in which case the webEnvironment will be meaningless

reference

Write unit tests in SpringBoot