Home> Java> javaTutorial> body text

How SpringBoot tests configuration properties and web startup environment

PHPz
Release: 2023-05-19 17:20:16
forward
728 people have browsed it

Load test-specific attributes

Click on @SpringBootTest source code to view

How SpringBoot tests configuration properties and web startup environment

You can add temporary configuration later, or you can use the command line args parameter set up. The set test-specific parameters 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); } }
Copy after login

Run results

How SpringBoot tests configuration properties and web startup environment

You can also use command line parameters

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 parameters take precedence

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

How SpringBoot tests configuration properties and web startup environment

This test The attributes set by the class are only valid for the current test and have little impact

Use 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 "; } }
Copy after login

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); } }
Copy after login

Running results

How SpringBoot tests configuration properties and web startup environment

Speed test class starts the web environment

Generally, the server will not be started when running in the test class, as shown below. They all display information about the success or failure of the operation

How SpringBoot tests configuration properties and web startup environment

We Ctrl b click into the @SpringBootTest source code to view, there is a question about the web

How SpringBoot tests configuration properties and web startup environment

The default value is MOCK, mock: provides a simulated web environment by default and will not start the embedded server

We are in the test class

How SpringBoot tests configuration properties and web startup environment

The first one is started with the port specified in your configuration file. If not, it will be started with 8080 by default.

The second mock: provides a simulated web environment by default and will not start the embedded server

The third is not to start the server

The fourth is to start the random port

We test the 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(){ } }
Copy after login

Running results

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

How SpringBoot tests configuration properties and web startup environment

The above is the detailed content of How SpringBoot tests configuration properties and web startup environment. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!