I wrote a test using mockito. I have the following REST
controller with this POST
request
@RestController
@RequestMapping(FriendResource.FRIENDS)
public class FriendResource {
public static final String FRIENDS = "/friends";
private FriendService friendService;
@Autowired
public FriendResource(FriendService friendService) {
this.friendService = friendService;
}
@PostMapping
public FriendDto create(@Valid @RequestBody FriendDto friendDto) {
return this.friendService.create(friendDto);
}
}
Here the method of the service implementation for mapping and persistence
@Override
public FriendDto create(FriendDto friendDto) {
Friend friend = this.friendPersistence.create(friendDto);
return new FriendDto(friend);
}
Here the test for this method:
@RunWith(MockitoJUnitRunner.class)
public class FriendResourceTest {
private MockMvc mockMvc;
@InjectMocks
private FriendResource resource;
@Mock
private FriendService friendService;
@Before
public void setUp() {
this.mockMvc = MockMvcBuilders
.standaloneSetup(resource)
.build();
}
@Test
public void shouldReturnFriendDtoWhenRetrieveFriendDto() throws Exception {
//given
FriendDto validDto = new FriendDto("Tom", "666666666");
when(friendService.create(validDto)).thenReturn(validDto);
//when
final ResultActions result = mockMvc.perform(
post(FRIENDS)
.accept(MediaType.APPLICATION_JSON))
.andDo(print());
//then
result.andExpect(status().isOk());
result.andExpect(content().contentType(MediaType.APPLICATION_JSON));
verify(friendService, times(1)).create(validDto);
}
}
I tried this but it didn't work: verify(friendService, times(1)).create(refEq(validDto));
And the log:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /friends
Parameters = {}
Headers = [Accept:"application/json"]
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type = com.user.rest.FriendResource
Method = com.user.rest.FriendResource#create(FriendDto)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = org.springframework.http.converter.HttpMessageNotReadableException
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 400
Error message = null
Headers = []
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: Status
Expected :200
Actual :400
<Click to see difference>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher$9(StatusResultMatchers.java:627)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:196)
at com.user.business_services.FriendResourceTest.shouldReturnFriendDtoWhenRetrieveFriendDto(FriendResourceTest.java:117)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:46)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
...
The post request returns an empty body and status 400. The other routs are passing the tests.
Thanks by the way.
Aucun commentaire:
Enregistrer un commentaire