I have a spring boot application that starts and runs a class that listens for an Application Creation Event , to call an external service to get some data, and then use that data to push some rules to the class path to execute. For local testing, we poked fun at an external service in our application that works great when the application starts.
The problem is when testing the application by running it using the spring boot annotation test and the built-in additive container, either on:
In the case of RANDOM PORT, when starting the application, it selects the URL for the breadboard service from the properties file in a specific port and has no idea where the built-in container is located, since it is accidentally selected, therefore, for the answer.
In the case of a DEFINED PORT, for the first file of the test file it is executed successfully, but at the moment when the next file is selected, it does not mean that the port is already in use.
Testing is logically divided into several files and requires an external service, called before the container starts to load the rules.
How can I either share the built-in container between test files in case of using a specific port or refactor my application code instead to gain access to a random port at startup during the execution of the test case.
Any help would be appreciated.
Application Launch Code:
@Component public class ApplicationStartup implements ApplicationListener<ApplicationReadyEvent> { @Autowired private SomeService someService; @Override public void onApplicationEvent(ApplicationReadyEvent arg0) { try { someService.callExternalServiceAndLoadData(); } catch (Execption e) {} } }
Test Code Annotations: Test1
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @TestPropertySource("classpath:test-application.properties") public class Test1 { @Autowired private TestRestTemplate restTemplate; @Test public void tc1() throws IOException {.....}
Test Code Annotations: Test2
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @TestPropertySource("classpath:test-application.properties") public class Test2 { @Autowired private TestRestTemplate restTemplate; @Test public void tc1() throws IOException {.....}
java spring-boot integration-testing spring-boot-test
ahj.ashish
source share