MockHttpServletResponse返回空主体

时间:2019-02-22 02:59:14

标签: spring-boot mocking mockito junit4

以下是我的控制器

@RestController
@RequestMapping(value = Routes.SEND_FCM)
public class AdminFcmController {

private final FCMService fcmService;

@Autowired
public AdminFcmController(FCMService fcmService) {
    this.fcmService = fcmService;
}

@ResponseStatus(HttpStatus.CREATED)
@PostMapping(value = "/admin/logs/add")
public AdminFcmResource addBulkFcmLog(@RequestBody BulkFcmDataResource adminFcmResource) {
    return fcmService.addBulkFcmLog(adminFcmResource);
}
}

我正在尝试在上述控制器中模拟fcmService的行为。因此,我模拟了fcmService并将其注入到AdminFcmController中,如下所示。

@RunWith(MockitoJUnitRunner.class)
public class AdminFcmControllerTest {

    protected MockMvc mockMvc;
    @InjectMocks
    AdminFcmController adminFcmController;

    @Mock
    FCMService fcmService;
    private JacksonTester<BulkFcmDataResource> bulkFcmDataResourceJacksonTester;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(adminFcmController)
                .build();
    }

    @Test
    public void addBulkFcmLog() throws Exception {
        JacksonTester.initFields(this, new ObjectMapper());

        AdminFcmResource adminFcmResource = AdminFcmResource.builder()
                .adminId(123L)
                .build();

        given(this.fcmService.addBulkFcmLog(any(BulkFcmDataResource.class))).willReturn(adminFcmResource);

         mockMvc.perform(
                post(Routes.SEND_FCM + "/admin/logs/add")
                        .accept(MediaType.APPLICATION_JSON)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(bulkFcmDataResourceJacksonTester.write(BulkFcmDataResource.builder().adminId(123L).build()).getJson()))
                .andExpect(status().isCreated())
                .andDo(print());

   }

为此,我使用了BDD测试,也尝试使用了模仿的when.then和doReturn.when方法,但它们也返回了空的正文。我还尝试使用对象映射器创建jacksonConverter,但它也失败了。空尸有什么原因吗?

如果是这样,解决方案是什么?

这是我得到的输出

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /api/ns/fcm/admin/logs/add
       Parameters = {}
          Headers = {Content-Type=[application/json], Accept=[application/json]}

Handler:
             Type = com.esewa.ns.firebase.AdminFcmController
           Method = public com.esewa.ns.firebase.resource.AdminFcmResource com.esewa.ns.firebase.AdminFcmController.addBulkFcmLog(com.esewa.ns.firebase.resource.BulkFcmDataResource)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 201
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

更新

当我使用 @RunWith(SpringRunner.class)和@SpringBootTest 连同@AutoWired和@MockBean一样,该测试似乎有效。但是我不知道如何。对此有解释吗?

0 个答案:

没有答案