Skip to content
Advertisement

Spring Boot Application is starting up from a JUnit test but unable to access through URL

I have a JUnit Test that starts my spring boot appcliation (Application.java).

JavaScript

If I run the JUnit test, Application is successfully starting up, but not accessible through url

Application Logs:

JavaScript

When I tried to access the application through url ‘http://localhost:8080/process‘, It says Site can’t be reached.

enter image description here

Advertisement

Answer

Why would you do such thing? This should be a unit test and the flow of the test should be the following: Start the application -> Call your controller endpoint -> assert that a specific text/element on that page is present -> Shut down the application.

Start the application : @RunWith(SpringRunner.class) doing this for you, no need to start is manually.

Shut down the application : At the end of your test class Spring boot does this for you (That’s why you cant access your app in the browser)

For further help please see my answer here: How to test (rest) enpoints

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement