У меня есть следующий тестовый класс для моего приложения с пружинной интеграцией, который успешно проходит при одиночном запуске
@SpringBootTest(classes = {BackupTestDefinition.class})
@ActiveProfiles({"test", "dev"})
@RunWith(SpringRunner.class)
public class BackupServiceTest {
@Value(value = "${ne.endpoint}")
private String ne;
@Autowired
private RestTemplate restTemplate;
private MockRestServiceServer mockServer;
@Before
public void setup() {
mockServer = MockRestServiceServer.bindTo(restTemplate).build(new UnorderedRequestExpectationManager());
mockServer.expect(ExpectedCount.manyTimes(), requestTo(UriComponentsBuilder.fromHttpUrl(ne).build().toUri())).andExpect(method(HttpMethod.POST)).andRespond(withSuccess());
}
@Test
public void testNotificationProcessing() throws IOException, InterruptedException, InitializationException, ExecutionException {
//some testing code
}
}
Но у меня есть другой тест, который имеет другие настройки (ExpectedCount.times (1)) для той же конечной точки и имеет другое TestDefinition. Итак, в этом наборе тестов есть пара контекстов. И когда я запускаю их вместе, я получаю следующее исключение
at org.springframework.test.web.client.AbstractRequestExpectationManager.createUnexpectedRequestError(AbstractRequestExpectationManager.java:141)
at org.springframework.test.web.client.UnorderedRequestExpectationManager.validateRequestInternal(UnorderedRequestExpectationManager.java:49)
at org.springframework.test.web.client.AbstractRequestExpectationManager.validateRequest(AbstractRequestExpectationManager.java:76)
at org.springframework.test.web.client.MockRestServiceServer$MockClientHttpRequestFactory$1.executeInternal(MockRestServiceServer.java:289)
at org.springframework.mock.http.client.MockClientHttpRequest.execute(MockClientHttpRequest.java:94)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:659)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:620)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:538)
Caused by: java.lang.AssertionError: No further requests expected: HTTP POST
После нескольких часов отладки я обнаружил, что настройки были успешно применены, но похоже, что restTemplate был вызван из другого контекста, где количество попыток было исчерпано. Не могли бы вы помочь мне выяснить, как можно решить эту проблему.