![](/img/trans.png)
[英]Autowired MockMvc is null with JUnit 5 and Spring boot
[英]Spring Boot Error @Autowired RestTemplateBuilder with junit
尝试使用 RestTemplateBuilder 在 Spring Boot 2.1.4 中 @Autowired 一个 RestTemplate。 当我运行 junit 测试时,尝试自动装配 RestTemplate 时出现错误。
我在这里看到: 如何使用注释自动装配 RestTemplate似乎 RestTemplateBuilder 更好,所以我想使用它。
这是配置文件:
@Configuration
public class Beans {
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}
这是测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = Beans.class)
public class AppTest extends TestCase {
@Autowired
private RestTemplate restTemplate;
}
错误是:
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method restTemplate in beanDeclerations.Beans required a bean of type 'org.springframework.boot.web.client.RestTemplateBuilder' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.springframework.boot.web.client.RestTemplateBuilder' in your configuration.
我编辑了其他自动装配的工作。 我在这里缺少什么? 在网上搜索后,我发现 spring 自动连线 RestTemplateBuilder,为什么不在这里这样做?
编辑:我最终使用了 @RestClientTest() 并且现在不得不将 RestTemplateBuilder Bean 移动到主类,稍后我会将它移动到不同的地方。 谢谢您的帮助。
RestTemplateBuilder 应该可以通过自动配置获得(参见: https : //github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org /springframework/boot/autoconfigure/web/client/RestTemplateAutoConfiguration.java )。 我认为由于您的@ContextConfiguration 缺少此配置。 你有一些可能性。 尝试将 RestTemplateBuilder 的 AutoConfig 添加到您的 ContextConfiguration。 第二个是创建一个 TestConfiguration 并创建您自己的 RestTemplateBuilder 或直接创建一个 RestTemplate。 第三个是不要注入 RestTemplate - 在你的测试中手工构建它。 您还可以删除 @ContextConfiguration-Annotation 但这将导致加载您在项目中定义的每个配置的测试。
还有一个用于测试的 RestTestTemplate ( https://www.baeldung.com/spring-boot-testresttemplate )。
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = {TestConfig.class, RestTemplateAutoConfiguration.class})
public class SandboxApplicationTests {
@Autowired
RestTemplate restTemplate;
@Test
public void contextLoads() {
}
}
上面的片段对我有用。 如果 ContextConfiguration 中没有 RestTemplateAutoConfiguration,由于缺少 RestTemplateBuilder-Bean,无法创建 RestTemplate
我有同样的问题(在 Spring Boot 2.5.5 应用程序中) 。
为了解决它,我必须在我的测试类中添加@AutoConfigureWebClient 。
所以我的测试是这样的:
@AutoConfigureWebClient
@WebMvcTest(controllers = TeamController.class)
public class TeamControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private FooService fooService;
@Test
public void findAllFoos() throws Exception {
mockMvc.perform(get("/foos"))
.andExpect(status().isOk());
}
}
但是我在某处读到它可能被认为是应该由 Spring Team 修复的错误(也许这个注释会直接添加到 @WebMvcTest 中?) 。
我是这样认为的:与使用 @SpringBootTest 时相比,测试开始和停止的速度更快(因为最后一个使您的应用程序完全启动)。
更多信息: https : //docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/web/client/AutoConfigureWebClient.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.