1

Im pretty new to spring framework and my JAVA application(Say Project_A) running at http://localhost:8080, does processing and forwards the processed content to http://localhost:9090 where another application(Say Project_B) is running. Project_B replies back to Project_A and then Project_A forwards the content outside .

Im now trying to write integration test for Project_A , so need two mocks, one at the request side and the other at Project_B side . For request, I can make use of MockMVC to simulate request and send . But for simulating the Project_B , Im thinking of creating a mock controller which receives requests from Project_A and send replies accordingly . So for that, I need to create another Controller running in different port . The default port in project_A is 8080 and in my new controller mock I need to use 9090.

Is there a way to use two different ports in one spring junit application ? Or any better way to achieve it with spring unit testing itself ?

shashantrika
  • 1,039
  • 1
  • 9
  • 29
  • Are you trying to do mock test or actual integration test ? Project_A and Project_B communicate over the rest? – IMParasharG Feb 18 '19 at 13:20
  • Yes project_A and Project_B are completely different entities and they communicate via REST . – shashantrika Feb 18 '19 at 16:01
  • What about my first question : Are you trying to do mock test cases or actual integration test cases? – IMParasharG Feb 18 '19 at 16:13
  • Actual integration test . And I followed this https://spring.io/guides/gs/testing-web/ and generated test case which sends request to my controller . But for other end Project_B , Im not sure how to handle in this scenario . – shashantrika Feb 18 '19 at 16:15
  • You already communicate Project_A to Project_B via REST. Then You can create TestRestTemplate object and send request to Project_B and validate response. – IMParasharG Feb 18 '19 at 21:41
  • @GovindParashar : Im planning to utilize TestRestTemplate for the external request to Project_A , which process the requests and sends another request to Project_B . Project_A is kind of interface between outside world and Project_B. For doing integration testing , I need to have two mocks system in place to achieve the behavior. In this case , I have created a dummy controller to server as a stub for Project_B. – shashantrika Feb 19 '19 at 12:18
  • Why you need two mocks system in place to achieve the behavior ? Mock test cases are separated for Project_A and Project_B It should not be link each other. You have to mock request and response for Project_A and same as for Project_B. For integration testing for Project_A to Project_B. I think Project_A aware about the address of Project_B then you have to create TestRestTemplate object and send request and get the response and validated it. – IMParasharG Feb 19 '19 at 13:14

2 Answers2

0

I generally use this in the application.properties

management.port=${second_Port}

0

you can use server.port=9090 in application-test.properties . And in your test class you can pick it like this

@TestPropertySource(value = { "classpath:application-test.properties" })

you also need to define server.port in place of actual port in your application.properties file

priyas
  • 415
  • 1
  • 8
  • 29