【发布时间】:2018-04-05 11:10:21
【问题描述】:
我有一个 Spring Bean,它使用 RestTemplateBuilder 在其构造函数中发送请求以发送请求:
@Service
class MyService {
MySettingsFromRemote settings;
MyService(RestTemplateBuilder builder, @Value("${my-url}") String url){
var rt = builder.build();
setting = rt.getForEntity(url, MySettingsFromRemote.class);
}
...
}
在测试期间,我想使用一些预定义的数据来模拟使用 MockRestServiceServer(或者可能模拟用于发送请求的 RestTemplateBuilder)的响应,以便加载应用程序上下文。地址写在application.properties文件中。
我试过这样做:
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.client.AutoConfigureMockRestServiceServer;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.client.match.MockRestRequestMatchers;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@RunWith(SpringRunner.class)
@AutoConfigureMockRestServiceServer
@SpringBootTest
public class CollectorApplicationTest {
@Autowired
MockRestServiceServer server;
@Value("${components.web-admin-portal.rest.schemas}")
String webAdminPortal;
@Before
public void init() {
server.expect(MockRestRequestMatchers.requestTo(webAdminPortal))
.andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
}
@Test
public void contextLoads() {
}
}
但上下文在 @Before 方法执行之前加载并失败,并显示一条消息称 MockRestServiceServer 没有预期请求。
然后我尝试使用ApplicationContextInitializer:
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.test.web.client.match.MockRestRequestMatchers;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
public class AppInit implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(final ConfigurableApplicationContext context) {
context.refresh();
MockRestServiceServer server = context.getBean(MockRestServiceServer.class);
String webAdminPortal = context.getEnvironment()
.getProperty("components.web-admin-portal.rest.schemas");
server.expect(MockRestRequestMatchers.requestTo(webAdminPortal))
.andRespond(withSuccess("{}", MediaType.APPLICATION_JSON));
}
}
但随后它抱怨MockServerRestTemplateCustomizer has not been bound to a RestTemplate.我认为可以通过在测试类上使用@RestClientTest 注释来解决此问题,因为它会禁用RestTemplateBuilder 的自动配置并启用MockRestServiceServer 的确认:
@RunWith(SpringRunner.class)
@SpringBootTest
@RestClientTest
@ContextConfiguration(initializers = AppInit.class)
public class CollectorApplicationTest {
但它并没有改变任何东西。
提前致谢。
【问题讨论】:
标签: java spring unit-testing junit