Ole*_* K. 5 spring spring-integration spring-boot spring-boot-test
我的 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
}
}
Run Code Online (Sandbox Code Playgroud)
但是我有另一个测试,它具有相同端点的其他设置 (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
Run Code Online (Sandbox Code Playgroud)
经过数小时的调试,我发现设置已成功应用,但看起来 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 — 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}.
*
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11575 次 |
| 最近记录: |