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?

Maharajan
1#
Maharajan Published in 2017-12-07 07:10:21Z

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)



@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@TestPropertySource("classpath:application-test.properties")
public class DsmMockStrategyTest {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    private BankerDashboardConfiguration dashboardConfig;

    private String originationTaskContextPath;
    private String originationTaskHostUrl;

    private final RestTemplate restTemplate = new RestTemplate();

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

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

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

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

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

© 2016 Powered by mzan.com design MATCHINFO