如何将@WebMvcTest用于单元测试POST方法?

时间:2018-10-07 14:58:19

标签: java rest post junit mockito

我正在使用Spring Boot和Mockito运行单元测试,并且正在测试RESTful服务。当我尝试测试GET方法时,它可以成功运行,但是当我尝试测试POST方法时,则失败。我应该怎么做才能解决这个问题?预先感谢!

这是REST控制器的代码:

package com.dgs.restfultesting.controller;

import java.net.URI;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import com.dgs.restfultesting.business.ItemBusinessService;
import com.dgs.restfultesting.model.Item;

@RestController
public class ItemController {

    @Autowired
    private ItemBusinessService businessService;

    @GetMapping("/all-items-from-database")
    public List<Item> retrieveAllItems() {
        return businessService.retrieveAllItems(); 
    }

    @PostMapping("/items")
    public Item addItem(@RequestBody Item item) {
        Item savedItem = businessService.addAnItem(item); 

        return savedItem;
    }
}

业务层:

package com.dgs.restfultesting.business;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dgs.restfultesting.data.ItemRepository;
import com.dgs.restfultesting.model.Item;

@Component
public class ItemBusinessService {

    @Autowired
    private ItemRepository repository;

    public Item retrieveHardcodedItem() {
        return new Item(1, "Book", 10, 100); 
    }

    public List<Item> retrieveAllItems() {

        List<Item> items = repository.findAll(); 

        for (Item item : items) {
            item.setValue(item.getPrice() * item.getQuantity());  
        }

        return items;  
    }

    public Item addAnItem(Item item) {
        return repository.save(item); 
    }
}

ItemControllerTest:

package com.dgs.restfultesting.controller;

import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.Arrays;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import com.dgs.restfultesting.business.ItemBusinessService;
import com.dgs.restfultesting.model.Item;

@RunWith(SpringRunner.class)             
@WebMvcTest(ItemController.class)  
public class ItemControllerTest {

    @Autowired
    private MockMvc mockMvc;    

    @MockBean
    private ItemBusinessService businessService;

    @Test
    public void retrieveAllItems_basic() throws Exception {

        when(businessService.retrieveAllItems()).thenReturn(
                Arrays.asList(new Item(2, "iPhone", 1000, 10),
                        new Item(3, "Huawei", 500, 17)));

        RequestBuilder request = MockMvcRequestBuilders
                .get("/all-items-from-database") 
                .accept(MediaType.APPLICATION_JSON); 

        MvcResult result = mockMvc.perform(request)
                .andExpect(status().isOk())
                .andExpect(content().json("[{id:2, name:iPhone, price:1000}, {id:3, name:Huawei, price:500}]"))  // This will return an array back, so this data should be within an array
                .andReturn();  
    }   

    @Test
    public void createItem() throws Exception {
        RequestBuilder request = MockMvcRequestBuilders
                .post("/items")
                .accept(MediaType.APPLICATION_JSON)
                .content("{\"id\":1,\"name\":\"Book\",\"price\":10,\"quantity\":100}")
                .contentType(MediaType.APPLICATION_JSON);

        MvcResult result = mockMvc.perform(request)
                .andExpect(status().isCreated())
                .andExpect(header().string("location", containsString("/item/")))
                .andReturn();
    }
}

测试retrieveAllItems_basic()方法没有问题,但是当我尝试为createItem()方法运行JUnit测试时,它不起作用,并且得到了以下信息:java.lang.AssertionError:预期状态:< 201>,但为:<200>

这是我在控制台中收到的:

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /items
       Parameters = {}
          Headers = {Content-Type=[application/json], Accept=[application/json]}
             Body = <no character encoding set>
    Session Attrs = {}

Handler:
             Type = com.dgs.restfultesting.controller.ItemController
           Method = public com.dgs.restfultesting.model.Item com.dgs.restfultesting.controller.ItemController.addItem(com.dgs.restfultesting.model.Item)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

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

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 200
    Error message = null
          Headers = {}
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []
2018-10-07 17:53:51.457  INFO 55300 --- [       Thread-3] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@71075444: startup date [Sun Oct 07 17:53:48 EEST 2018]; root of context hierarchy

更新-----------------------------

我尝试设置以下位置:item / id。

这是控制器的代码:

@PostMapping("/items")
public ResponseEntity<Object> addItem(@RequestBody Item item) {
    Item savedItem = businessService.addAnItem(item); 
    HttpHeaders httpHeaders = new HttpHeaders();
    UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance();

    UriComponents uriComponents =
            uriComponentsBuilder.path("/item/{id}").buildAndExpand(savedItem.getId());
    httpHeaders.setLocation(uriComponents.toUri());

    return new ResponseEntity<>(savedItem, httpHeaders, HttpStatus.CREATED); 
}

这是测试代码:

@Test
public void createItem() throws Exception {

    RequestBuilder request = MockMvcRequestBuilders
            .post("/items")
            .accept(MediaType.APPLICATION_JSON)
            .content("{\"id\":1,\"name\":\"Book\",\"price\":10,\"quantity\":100}")
            .contentType(MediaType.APPLICATION_JSON);

    MvcResult result = mockMvc.perform(request)
            .andExpect(status().isCreated())
            .andExpect(header().string("location", containsString("/item/1")))
            .andReturn();
}

当我为createItem()方法运行JUnit测试时,出现以下错误:org.springframework.web.util.NestedServletException:请求处理失败;嵌套的异常是java.lang.NullPointerException

2 个答案:

答案 0 :(得分:2)

从您的控制器返回201::由于您的断言测试通过使用201状态期望created,但是您的控制器正在返回200(确定)。

   @PostMapping("/items")
    public ResponseEntity<?> addItem(@RequestBody Item item) {
        Item savedItem = itemBusinessService.addAnItem(item);

        return new ResponseEntity<>(savedItem, HttpStatus.CREATED);
    }

或修改测试以检查状态为OK(200)。如果不想声明“位置”,请更新测试。

 @Test
 public void createItem() throws Exception {
 RequestBuilder request = MockMvcRequestBuilders
        .post("/items")
        .accept(MediaType.APPLICATION_JSON)
        .content("{\"id\":1,\"name\":\"Book\",\"price\":10,\"quantity\":100}")
        .contentType(MediaType.APPLICATION_JSON);

MvcResult result = mockMvc.perform(request)
        .andExpect(status().isOk()).andReturn();

}

更新-允许位置标头作为响应

如果您希望“位置” 从标头返回,请在下面修改控制器和测试用例以检查标头中的位置。

步骤1:在控制器的添加项方法中,添加位置uri并返回。

 @PostMapping("/items")
    public ResponseEntity<?> addItem(@RequestBody Item item) {
        Item savedItem = businessService.addAnItem(item);
        HttpHeaders httpHeaders = new HttpHeaders();
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.newInstance();

        UriComponents uriComponents =
                uriComponentsBuilder.path("/item/").buildAndExpand("/item/");
        httpHeaders.setLocation(uriComponents.toUri());
        return new ResponseEntity<>(savedItem, httpHeaders, HttpStatus.CREATED);
    }

第2步:现在您的测试将按预期断言"location"

 @Test
    public void createItem() throws Exception {
        RequestBuilder request = MockMvcRequestBuilders
                .post("/items")
                .accept(MediaType.APPLICATION_JSON)
                .content("{\"id\":1,\"name\":\"Book\",\"price\":10,\"quantity\":100}")
                .contentType(MediaType.APPLICATION_JSON);

        MvcResult result = mockMvc.perform(request)
                .andExpect(status().isCreated())
                .andExpect(header().string("location", containsString("/item/")))
                .andReturn();
    }

答案 1 :(得分:1)

首先,我在您的createItem测试中没有看到模拟程序部分

Item item = new Item();
Item newItem = new Item();
when(businessService.addAnItem(item)).thenReturn(newItem);

,并且在您的控制器中我看不到Location标头。像下面这样的代码可能会更好:

@PostMapping("/items")
public ResponseEntity<?> addItem(@RequestBody Item item) {
    Item savedItem = itemBusinessService.addAnItem(item);

    return ResponseEntity.created(UriComponentsBuilder.fromHttpUrl("http://yourserver/item"));
}

我希望这可以为您提供帮助