I've build a service with two endpoints, and I want to cover both endpoints with integration tests. To prevent these integrationtests from reaching other services, I'm using the MockRestServiceServer
class to mock calls and responses to other HTTP services.
TestOperationA:
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles({"integration"})
@ComponentScan(basePackages = "nl.xyz")
public class OperationAIntegrationTest {
MockRestServiceServer mockServer;
@Autowired
RestTemplate restTemplate;
@Autowired
OperationA operationA;
@Before
public void setup() {
this.mockServer = MockRestServiceServer.bindTo(restTemplate).bufferContent().ignoreExpectOrder(true).build();
this.mockServer.reset();
}
@After
public void finish() {
// Verify all method calls are run after the testcase.
this.mockServer.verify();
this.mockServer.reset();
}
And then testcases contain stuff like:
this.mockServer.expect(requestTo(ENDPOINT_OAUTH))
.andExpect(method(HttpMethod.POST))
.andRespond(withSuccess(objectMapper.writeValueAsString(oAuthToken), MediaType.APPLICATION_JSON));
I do the same for OperationBIntegrationTest. This includes the binding to restTemplate
!
Now the problem is that if I run all testcases seperately, everything succeeds. If I run all the testcases from OperationA ór OperationB, they all succeed. But when I run all the testcases so the integrationtests from OperationA and OperationB are executed in sequence, the testcases from OperationB fail. Even though I see that Spring Boot gets started anew when the testing framework jumps to the second testing file.
I'm thinking that the MockRestServiceServer
does not get cleaned up or I'm doing something wrong with the binding to RestTemplate
. I tried the .reset()
and .verify()
combinations by placing them in @Before
and @After
, but with no effect. Does anybody know why this is happening?
Apparently, some stuff was happening in the background causing certain variables and methods not being updated anymore since a previous test already updated it. When I don't dirty the application context (by having more MockBean's for example) then everything is allright.
So the ones adjusting the values in the background should be marked dirty.