Java – Test and launch the spring boot application
I have a JUnit test. After the test, start a spring boot application (in my example, the main class is springtestdemoapp):
@WebIntegrationTest @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SpringTestDemoApp.class) public class SpringTest { @Test public void test() { // Test http://localhost:8080/ (with Selenium) } }
Using spring boot 1.3 3. Release everything is normal Nevertheless, the annotations @ webintegration test and @ springapplicationconfiguration have been in spring boot 1.5 2. Delete in release I tried to refactor the code into a new version, but I couldn't Through the following test, my application did not start before the test, http://localhost:8080 Return 404:
@RunWith(SpringRunner.class) @SpringBootTest(classes = SpringTestDemoApp.class) @WebAppConfiguration public class SpringTest { @Test public void test() { // The same test than before } }
How can I refactor my test to work in spring boot 1.5?
Solution
@The webenvironment option in spring boottest is very important It can adopt none, mock and random_ PORT,DEFINED_ Port equivalent
>None will only create spring beans, not any simulated servlet environment. > Mock will create spring beans and simulate servlet environment. > RANDOM_ Port will start the actual servlet container on the random port; This can be automatically assembled using @ localserverport. > DEFINED_ Port gets the port defined in the properties and uses it to start the server
If no webenvironment is defined, the default value is random_ PORT. So the application may start for you on a different port
Try overwriting it with defined_ Port, or try to automatically assemble the port number and try to run the test on that port