vendredi 14 décembre 2018

Testing MockBean Null

I have this class definition

@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;
}
}

where Reservation is a simple 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 + "]";
}
}

when I try to test this class

@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());
}

I got this error:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.mockito.internal.debugging.LocationImpl]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.mockito.internal.debugging.LocationImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: 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"])

.... ....

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.mockito.internal.debugging.LocationImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: 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"])

How can I inject reservation in the right way??

Thank you

Aucun commentaire:

Enregistrer un commentaire