【问题标题】:jsonRootName missing while performing unit test case for spring hateoas为 spring hatoas 执行单元测试用例时缺少 jsonRootName
【发布时间】:2015-06-11 20:20:29
【问题描述】:

我使用 spring-boot-starter-Hateoas 开发了一个 rest 服务,我能够正确获取 json 输出,如下所示:

"_embedded": {
   "bills": 
         {
          uid: "123"
          code: "0000"

而且我需要使用 mockito 编写相同的单元测试用例。我写的代码如下。

ApplicationTest.java:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class ApplicationTest {

BillControllerAutoTest:

public class BillControllerAutoTest {

private BillService mockBillService;
private MockMvc mockMvc;
private static final String BILL_UID = "99991";

@Before
public void setupController() {
           mockBillService= mock(BillService .class);
          BillController controller = new BillController (mockBillService);
    mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}    

@Test
public void testGetBills() throws Exception {
    // some fake data
    final List<Bill> fakeBillList= new ArrayList<>();
    fakeBillList.add(BillFake.bill("1234"));

    when(mockBillService.getBills(BILL_UID))
            .thenReturn(fakeBillList.stream());

    // execute and verify
    mockMvc.perform(get("/bills/" + BILL_UID ))
            .andExpect(content().string(containsString("\"embedded\":{\"bills\"")))

BillController.java:

@RestController
@RequestMapping(value = "/bills/{billUid}", produces = "application/hal+json")
public class BillController extends BaseController {
private BillService billService;

    @RequestMapping(method = RequestMethod.GET, value = "")
public ResponseEntity<Resources<Resource<Bill>>> getBills(@PathVariable String billUid) {
    return resourceListResponseEntity(
            () -> billService.getBills(billUid),
            bill-> createResource(billUid),
            resources -> resources.add(linkTo(methodOn(BillController .class)
                    .getBills(billUid)).withSelfRel()));
}

依赖关系:

dependencies {
compile "org.springframework.boot:spring-boot-starter-hateoas"
compile "org.springframework.boot:spring-boot-starter-ws"
compile "org.springframework.boot:spring-boot-starter-actuator"

testCompile("org.springframework.boot:spring-boot-starter-test")
} 

我的构建失败并出现以下 stackTrace:

java.lang.AssertionError: Response content
Expected: a string containing "\"_embedded\":{\"bills\""
 but: was 
"content":[
   {
   uid: "123"
   code: "0000"

这意味着“_embedded : { bills”在单元测试的mockMvc返回的响应中不可用。我是否缺少任何配置,请告诉我。任何帮助将不胜感激。

【问题讨论】:

    标签: java spring-boot mockito junit4 spring-hateoas


    【解决方案1】:

    我在这里回答了非常相似的问题:Spring's MockMVC Responses Don't Match Browser Response

    简而言之:spring HATEOAS 为正确渲染 hal 添加了额外的配置(如此处所述:http://docs.spring.io/spring-hateoas/docs/0.19.0.RELEASE/reference/html/#configuration)。在您的测试中,您必须手动应用此配置。查看第一个链接以获取有关如何操作的详细信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      • 2016-04-15
      • 2018-12-18
      • 2018-10-09
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      相关资源
      最近更新 更多