【发布时间】:2019-05-15 18:39:55
【问题描述】:
我有这个类定义
@RestController
public class ReservationController {
@Autowired
private Reservation reservation;
@RequestMapping(value = "/reservation", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
@ResponseBody
public Reservation getReservation() {
return reservation;
}
}
其中 Reservation 是一个简单的 Pojo
public class Reservation {
private long id;
private String reservationName;
public Reservation() {
super();
this.id = 333;
this.reservationName = "prova123";
}
public Reservation(long id, String reservationName) {
super();
this.id = id;
this.reservationName = reservationName;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getReservationName() {
return reservationName;
}
public void setReservationName(String reservationName) {
this.reservationName = reservationName;
}
@Override
public String toString() {
return "Reservation [id=" + id + ", reservationName=" + reservationName + "]";
}
}
当我尝试测试这个类时
@WebMvcTest
@RunWith(SpringRunner.class)
public class MvcTest {
@Autowired
private MockMvc mockMvc;
@MockBean(name = "reservation")
private Reservation reservation;
@Test
public void postReservation() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/reservation"))
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(MockMvcResultMatchers.status().isOk());
}
}
我收到了这个错误:
org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 org.springframework.http.converter.HttpMessageConversionException: 类型定义错误:[简单类型,类 org.mockito.internal.debugging.LocationImpl];嵌套异常是 com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.mockito.internal.debugging.LocationImpl 并且没有发现创建 BeanSerializer 的属性(为避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:spring.boot.usingSpringBoot.entity.Reservation$MockitoMock$980801978["mockitoInterceptor"]->org.mockito.internal.creation.bytebuddy.MockMethodInterceptor["mockHandler"]->org.mockito.internal.handler.InvocationNotifierHandler["invocationContainer "]->org.mockito.internal.stubbing.InvocationContainerImpl["invocationForStubbing"]->org.mockito.internal.invocation.InvocationMatcher["invocation"]->org.mockito.internal.invocation.InterceptedInvocation["location"] )
.... ....
原因:com.fasterxml.jackson.databind.exc.InvalidDefinitionException:找不到类 org.mockito.internal.debugging.LocationImpl 的序列化程序,也没有发现创建 BeanSerializer 的属性(为避免异常,请禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:spring.boot.usingSpringBoot.entity.Reservation$MockitoMock$980801978["mockitoInterceptor"]->org.mockito.internal.creation.bytebuddy.MockMethodInterceptor["mockHandler"]->org.mockito.internal.handler.InvocationNotifierHandler ["invocationContainer"]->org.mockito.internal.stubbing.InvocationContainerImpl["invocationForStubbing"]->org.mockito.internal.invocation.InvocationMatcher["invocation"]->org.mockito.internal.invocation.InterceptedInvocation["位置"])
如何以正确的方式注入预订?
谢谢
【问题讨论】:
-
首先,您的 resevation 类是一个简单的 pojo,因此它不包含在 spring 上下文中,这意味着您可能需要创建一个 servie 预订类,它必须使用 Service(或组件)进行注释并且在您的测试类,如果你想模拟你使用 InjectMock 的代码
-
是的,你是对的......我忘记了 @Component 到 Reservation Class
标签: spring spring-boot testing mockito