Home Why Spring Boot Rest services not called its dependent services from Junit?
Reply: 0

Why Spring Boot Rest services not called its dependent services from Junit?

user2491 Published in July 16, 2018, 6:25 am

I have created Spring Boot rest service, explaining about my service flow.

When request http://localhost:8082/rest/service1 to this URL

it will call service1 -> then service 1 internally calls service 2 (micro services running in the same tomcat) to give response back.

Now I have created a JUnit test class with necessary annotations. When run JUnit class, it is starting embedded tomcat.

But when service1 -> calls service2, I am getting IO exception. Attached my JUnit class for reference. Also mentioned the exception which occurred.

Note: If I started the application and execute JUnit, service 1 -> service 2 communicated properly. But I wanted to run through JUnit without start the server manually.

Let me know if you need more information. Thanks in Advance.

ChannelServicesRestResource caught unknown exception =org.springframework.web.client.ResourceAccessException(I/O error on GET request for "http://localhost:8082/rest/service2": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect)

public class DsmMockStrategyTest {

    private WebApplicationContext context;

    private MockMvc mockMvc;

    private BankerDashboardConfiguration dashboardConfig;

    private String originationTaskContextPath;
    private String originationTaskHostUrl;

    private final RestTemplate restTemplate = new RestTemplate();

    public void setUp() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.context).build();
        dashboardConfig = context.getBean(BankerDashboardConfiguration.class);
        WireMockServer wms = new WireMockServer();

    public void testDsmCalls() throws Exception {
        try {
            // Getting Response
            RequestBuilder dashboardRequestBuilder = MockMvcRequestBuilders.get("/bankers/1/dashboard")
            MvcResult mvcDashboardResult = mockMvc.perform(dashboardRequestBuilder).andReturn();

            System.out.println("*************** : " + mvcDashboardResult.getResponse().getContentAsString());

        } catch (Exception e) {
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.311153 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO