当 MockRestServiceServer 设置为 ExpectedCount.manyTimes() 时,预计不会有进一步的请求

No further requests expected while MockRestServiceServer was set to ExpectedCount.manyTimes()

我的 spring 集成应用程序有以下测试 class,已成功通过并单独启动

@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.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 是从另一个尝试次数已用尽的上下文调用的。你能帮我看看如何解决这个问题吗?

这个问题可以通过在测试 class 中使用 @DirtiesContext 以及 @RunWith:

来解决
 * Test annotation which indicates that the
 * {@link org.springframework.context.ApplicationContext ApplicationContext}
 * associated with a test is <em>dirty</em> and should therefore be closed
 * and removed from the context cache.
 *
 * <p>Use this annotation if a test has modified the context &mdash; for
 * example, by modifying the state of a singleton bean, modifying the state
 * of an embedded database, etc. Subsequent tests that request the same
 * context will be supplied a new context.
 *
 * <p>{@code @DirtiesContext} may be used as a class-level and method-level
 * annotation within the same class or class hierarchy. In such scenarios, the
 * {@code ApplicationContext} will be marked as <em>dirty</em> before or
 * after any such annotated method as well as before or after the current test
 * class, depending on the configured {@link #methodMode} and {@link #classMode}.
 *

这里是关于此事的文档:https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/testing.html#dirtiescontext