SpringBoot test configuration properties and start web environment

Table of contents

 Properties specific to loading tests

operation result

Using external beans for testing

operation result

The speed test class starts the web environment

we are in test class 

operation result 


 Properties specific to loading tests

Click to view @SpringBootTest source code

Temporary configuration can be added later, or can be set using the command line args parameter. Test-specific parameters set will override those in the configuration file.

package com;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(args = {properties = {"test.properties=1234"})

public class TestProperties {
    @Value("${test.properties}")
    private String ps;
    @Test
    public void test(){
        System.out.println(ps);
    }
}

operation result

You can also use command line arguments

args = {"--test.properties=4321"},

The priority of the command line parameters is higher than that of the configuration file, so when the two coexist, the command line is the main one.

@SpringBootTest(args = {"--test.properties=4321"},properties = {"test.properties=1234"})

 The properties set by this test class are only valid for the current test and have little impact

Using external beans for testing

package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration//说明当前为配置类
public class TestBean {
    @Bean//创建bean
    public String mess(){
        return "this bean run ";
    }
}

Under the test class, use the @Import annotation to load the current test configuration 

package com.test;

import com.config.TestBean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;

@SpringBootTest
@Import({TestBean.class})
public class TestBeanNow {
    @Autowired//注入bean对象
    public String mess;

    @Test
    public void test(){
        System.out.println(mess);
    }

}

operation result

The speed test class starts the web environment

Running in the test class generally does not start the server, as shown in the figure below. Both show the success or failure of the operation

 Let's Ctrl+b click into the @SpringBootTest source code to view, there is a web

 The default value is MOCK, mock: a simulated web environment is provided by default, and the embedded server will not be started

we are in test class 

 

 The first one starts with the port specified in your configuration file, if not, it starts with 8080 by default

The second mock: a simulated web environment is provided by default, and the embedded server will not be started

The third is to not start the server

The fourth is random port startup

We test random port startup

package com;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class WebTest {
    @Test
    public void test(){


    }
}

operation result 

Run it twice to see the port results, they are all random 

Guess you like

Origin blog.csdn.net/weixin_60719453/article/details/127350113