简体   繁体   English

这是单元测试还是集成测试?

[英]Is this a unit test or integration test?

I am learning how to create a unit test in Spring Boot JPA with JUnit and Mockito我正在学习如何使用 JUnit 和 Mockito 在 Spring Boot JPA 中创建单元测试

I have done up a test class,我已经完成了一个测试课,

public void testInsertionMethod() throws Exception {

    String URI = "/insertionURL";
    ShoppingList list = new ShoppingList (3, new Fruit(1), new Vegetable(1));
    String inputJson= this.jsonConversionMethod(list);

    Assert.assertEquals(1, list.getNumOfItems());
    Assert.assertEquals(1, list.getFruit().getFruitId);
    Assert.assertEquals(1, list.getVegetable.getVegetableId());

    Mockito.when(shoppingListSvc.save(Mockito.any(ShoppingList.class))).thenReturn(list);

    MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.post(URI).param("fruitId", "1").param("vegId", "1"). accept(MediaType.APPLICATION_JSON).content(inputJson).contentType(MediaType.APPLICATION_JSON)).andReturn();

    MockHttpServletResponse mockHttpServletResponse = mvcResult.getResponse();
    String jsonOutput = mockHttpServletResponse.getContentAsString();
    assertThat(outputJson).isEqualTo(inputJson);
    Assert.assertEquals(HttpStatus.OK.value(), mockHttpServletResponse.getStatus());

}

Can anyone advise on how I can improve my unit testing?任何人都可以就如何改进我的单元测试提出建议吗? Am I doing anything wrong here?我在这里做错了什么吗?

It's a component test.这是一个组件测试。 Also your test class is probably annotated with此外,您的测试类可能带有注释

@AutoConfigureMockMvc
@SpringBootTest

cause you are using mockMvc to mock a call to endpoint.因为您正在使用 mockMvc 来模拟对端点的调用。

In my opinion it does not make sense to do this part在我看来,做这部分没有意义

 Assert.assertEquals(1, list.getNumOfItems());
 Assert.assertEquals(1, list.getFruit().getFruitId);
 Assert.assertEquals(1, list.getVegetable.getVegetableId());

cause you created this object in the test and you know what did you put there.因为你在测试中创建了这个对象并且你知道你在那里放了什么。 The only thing you should be interested in is what your endpoint returns.您唯一应该感兴趣的是端点返回的内容。 You can check it as you already do via comparing inputJson and outputJson or do it like this:您可以通过比较 inputJson 和 outputJson 来检查它,或者像这样检查它:

        .andExpect(status().isOk())
        .andExpect(jsonPath("$.id", equalTo(...)))
        .andExpect(jsonPath("$.firstName", notNullValue()))
        .andExpect(jsonPath("$.lastName").isNotEmpty())
        .andExpect(jsonPath("$.email", equalTo(...)))
        .andExpect(jsonPath("$.partnerId", equalTo(...)));

Depends on what is important for you to check.取决于您要检查的重要内容。 Eg if your response returns id which is generated by some internal class/method you will not be able to construct such inputJson cause you cannot predict which id will be returned.例如,如果您的响应返回由某些内部类/方法生成的 id,您将无法构造这样的 inputJson,因为您无法预测将返回哪个 id。 In this case you are able to check that id is not empty在这种情况下,您可以检查 id 不为空

        .andExpect(jsonPath("$.id").isNotEmpty())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM