3

I have a Spring Boot application with a REST API. Behind the scenes it uses a vended SDK to call the vendors service. I need to run load tests on my application, but don’t want to call the vendor API and accidentally crash their system during testing.

Is it possible to use Mockito outside of a JUnit test to create a mock for the vendor SDK objects during the normal application runtime?

I figured I would use a profile based configuration beam to enable the mocked object when profile is “performance-test”. But I can find no article/discussion/mention of anyone using Mockito this way and it is making me second guess my approach. Thoughts?

user1625624
  • 119
  • 1
  • 10
  • Possible duplicate of [How to mock remote REST API in unit test with Spring?](https://stackoverflow.com/questions/25564533/how-to-mock-remote-rest-api-in-unit-test-with-spring) – tkruse Dec 05 '17 at 02:27

1 Answers1

1

You probably should look for using wiremock or similar software to mock the vendor service, like here: Integration Testing with a fake server

Wiremock is a stub server that you can conveniently start/stop from within JUnit tests. It acts like the remote server when you set up responses. The documentation is really good, I do not want to copy&paste all of it here.

Just a sample:

public class MyTest {
    @Rule
    public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort().dynamicHttpsPort());

    @Test
    public void exampleTest() {
        stubFor(get(urlEqualTo("/my/resource"))
                .willReturn(aResponse()
                    .withStatus(200)
                    .withBody("<response>Some content</response>")));

        ...        
        verify(postRequestedFor(urlMatching("/my/resource/[a-z0-9]+"))
                .withRequestBody(matching(".*<message>1234</message>.*")));
    }
}

For loadtest, you would rather run the stub standalone somewhere, and have some script set up the responses.

You probably don't find mention of Mockito because embedding mocks with stub responses into your application is a bad idea and will not help you getting realistic results for load tests (because your responses will be much faster and not pass through serialization/deserialization).

Else also look here: How to mock remote REST API in unit test with Spring?

tkruse
  • 10,222
  • 7
  • 53
  • 80
  • Appreciate the response. Didn’t consider the impact a Mockito itself could have my apps performance. So the idea here would be to host a separate wiremock server, which acts as a go-between my app and actual vendor API, can optionally record stubs, and in a load test scenario only returns mocked responses. To truly separate them then, the wiremock server should be run as an external process? If I understand correctly. – user1625624 Dec 06 '17 at 09:20
  • not as go-through, just a stub. Easy to run from tests or standalone. – tkruse Dec 06 '17 at 10:02