- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我将@Service 添加到 Controller 后,我的单元测试失败了。该项目是 Spring-boot v 2.0.1.RELEASE。我花了很多时间试图找到答案,但没有运气。该测试在我添加 @Service 注释之前有效,并且我的服务类中有一个存储库。
堆栈跟踪:
2018-04-24 12:57:12.487 WARN 940 --- [ main] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'fleetController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'uk.co.vw.lead.service.ContactUsService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Controller :
@Slf4j
@RestController
@RequestMapping(value = VERSION, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public class FleetController {
public static final String VERSION = "1.0";
@Autowired
private ContactUsService service;
@InitBinder
public void initBinder(final WebDataBinder webDataBinder) {
webDataBinder.registerCustomEditor(NatureOfEnquiryEnum.class, new NatureOfEnquiryEnumConverter());
webDataBinder.registerCustomEditor(FleetSizeEnum.class, new FleetSizeEnumConverter());
}
@PostMapping(value = "/fleet/contact-us")
public ResponseEntity contactUs(@Valid ContactUsDTO formDTO) {
service.createForm(formDTO);
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
@PostMapping(value = "/fleet/request-demo")
public ResponseEntity requestDemo(@Valid RequestDemoDTO demoDTO) {
return new ResponseEntity(HttpStatus.NO_CONTENT);
}
服务:
@Service
public class ContactUsServiceImpl implements ContactUsService {
@Autowired
private FleetRepository repository;
@Override
public void createForm(ContactUsDTO formDTO) {
ContactUsForm form = populateContactUsForm(formDTO);
repository.save(form);
}
测试类:
@RunWith(JUnitPlatform.class)
@WebMvcTest(FleetController.class)
@ExtendWith(SpringExtension.class)
public class FleetControllerTest {
private final String CONTACT_US_URL = "/fleet/contact-us";
@Autowired
private MockMvc mockMvc;
@MockBean
private FleetRepository repository;
@Autowired
private ContactUsService service;
@Test
public void contactUsSuccessTest() throws Exception {
this.mockMvc.perform( post("/" + VERSION + CONTACT_US_URL)
.contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.param("firstname", "John")
.param("lastname", "Doe")
.param("company", "Skynet")
.param("emailAddress", "john.connor@sky.net")
.param("telephone", "020 8759 4294")
.param("natureOfEnquiry", "new")
.param("comments", "some comments")
.param("recaptchaResponse", "success"))
.andExpect(status().isNoContent());
}
@Test
public void contactUsMissingRequiredFieldsTest() throws Exception {
this.mockMvc.perform( post("/1.0/fleet/contact-us")
.contentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE))
.andExpect(status().isBadRequest());
}
请帮忙,因为我不知道发生了什么。
最佳答案
用 @WebMvcTest
注释的测试分类是仅关注 Spring MVC 组件的测试: Controller 。
因此无法 Autowiring 单元测试中声明的服务字段:
@Autowired
private ContactUsService service;
所以你也应该模拟这个依赖:
@MockBean
private ContactUsService service;
另请注意,由于 FleetController
对 FleetRepository
没有任何直接依赖性,因此不需要模拟此 bean:
@MockBean
private FleetRepository repository;
更糟糕的是,它在上下文中添加了一个模拟,可能会在您的测试期间产生副作用。
您只需模拟被测 Controller 的直接依赖项。
作为替代方案,如果您只想模拟一些 bean 而不是所有不是 Controller 的 bean,请不要使用 @WebMvcTest
而不是使用 @SpringBootTest
这将加载整个上下文。
然后在测试类中声明您要使用 @MockBean
模拟的类。
关于java - @WebMvcTest 测试类中的 UnsatisfiedDependencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50001424/
我正在我的应用程序中实现 JWT 身份验证。一切正常,但是当我运行已经存在的 @WebMvcTests 并检查返回的状态代码时,它们都失败并显示“实际:403”。 这是我当前的测试套件之一: @Web
我正在尝试使用 @WebMvcTest 创建 Controller 测试,据我了解,当我添加测试类的 @WebMvcTest(ClientController.class) 注释时它不应该创建大量的
在我将@Service 添加到 Controller 后,我的单元测试失败了。该项目是 Spring-boot v 2.0.1.RELEASE。我花了很多时间试图找到答案,但没有运气。该测试在我添加
我想在 BookRestController 中测试我的休息端点。我使用 @WebMvcTest 编写了一个测试。 @RunWith(SpringRunner.class) @WebMvcTest(B
我在 groovy 上有 Controller @RestController @RequestMapping('/v1') @CompileStatic class DatasourceResour
Spring Boot 1.4 添加了 @WebMvcTest,它连接了测试我的应用程序的 Web 切片所需的部分。这太棒了,但我也想确保我的自定义过滤器和安全代码已连接,这样我也可以验证它们是否正常
我要test application slices ,但有一个包我想排除,因为它与那些测试根本无关。 我正在尝试以这种方式排除包: @RunWith(SpringRunner.class) @WebM
我有一个 spring rest mvc Controller ,它的 url 是“/public/rest/vehicle/get”。在我的安全配置中,我定义了对/public/rest 的任何请求
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
使用@WebMvcTest 将通过查找@SpringBootConfiguration 类(例如@SpringBootApplication)自动配置所有web 层bean。 如果配置类在不同的包里,
我正在编写一个 Controller 测试,其中 Controller 看起来像 @RestController public class VehicleController { @Autow
我有一个 Spring Boot 应用程序,可生成大量 Controller ,我的目标是为特定 Controller 创建集成测试。我读到我们可以使用 @WebMvcTest 注释实现一个测试切片,
我有点被这个问题难住了。我见过的例子和我在网上找到的所有内容都表明这一点 public ResponseEntity getTerminalsBySearchTerm( @PathPa
我正在学习如何测试我的 SpringBoot 应用。 现在我正在尝试通过为现有工作项目创建测试来学习。 我从我的 AdminHomeController 开始,它在管理员登录时管理主页: @Con
我有以下主类。 @EnableJpaAuditing @SpringBootApplication @EnableJpaRepositories(repositoryFactoryBeanClass
我在测试 Spring Controller 时遇到了问题。我在我的测试类中使用注释@WebMvcTest。当我运行测试时,我得到这个错误:没有可用的“org.springframework.boot
是否有可能在测试上下文中声明一个 RestController ,最好是作为 Spring Boot 测试的内部类?我确实需要它用于特定的测试设置。我已经尝试以下简单示例作为 POC: import
我的目标是将之前使用 Spring Boot 1.3 开发的 Spring Boot 应用程序迁移到最新的 Spring Boot 版本 1.4。该应用程序由几个 maven 模块组成,其中只有一个包
我有一个 Controller 和一个使用 @WebMvcTest 的测试,它运行良好。现在我需要添加一些验证逻辑,为此我 @Autowired一个额外的 bean(一个 @Component ,一个
我有一个 Spring Boot 应用程序 (1.5.10.RELEASE),其中包含如下所示的主要 (SpringBootApplication): @SpringBootApplication @
我是一名优秀的程序员,十分优秀!