I have an issue with a spring integration test.
The behavior:
When I run the test below in isolation, it is in success.
However, when all tests are run, many of them including the one below are in error.
When I ignore the test below and run all test, all are in success.
I haven't included the error stacktrace because it is highly related to our business logic and I suspect the error is related to my usage of spring boot test @SpyBean
.
Here is the test:
@RunWith(SpringRunner.class)
@ActiveProfiles(profiles = "test")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
...
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private DataKeyStore dataKeyStore;
@SpyBean
private TokenTools tokenTools;
...
@Test
public void myTest() throws Exception {
doReturn("someGeneratedToken")
.doReturn("someGeneratedToken")
.doCallRealMethod()
.when(tokenTools)
.createToken(any(TokenProfile.class), anyString(), anyString());
...
Please note that DataKeyStore
is a dependency of TokenTools
.
As I said above, I suspect tests are stepping on each other and my @SpyBean
somehow leaks on other test classes...
My question is how can I make sure this test does not step on the other tests? I have tried the @DirtiesContext
annotation to no avail...
Also what puzzles me is that the @SpyBean
is already reset (as per the documentation/javadoc).
Can anyone please help?
edit: Using my IDE to debug the tests indicates that TokenTools
is instantiated only twice for all tests: once at the initialization of tests and a second time for creating the @SpyBean
for the test above. The remainder of tests run after the test above use the second instance i.e. the @SpyBean
instance...