gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:37:15 28 4
gpt4 key购买 nike

我的 spring-integration 应用程序有以下测试类,该应用程序成功通过单独启动

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

最佳答案

可以使用 @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

关于spring - 当 MockRestServiceServer 设置为 ExpectedCount.manyTimes() 时,预计不会有进一步的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50239757/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com