elasticsearch,junit5,Spring Boot,elasticsearch,Junit5" /> elasticsearch,junit5,Spring Boot,elasticsearch,Junit5" />

Spring boot 如何处理Elasticsearchn CRUD操作的负JUNIT测试用例

Spring boot 如何处理Elasticsearchn CRUD操作的负JUNIT测试用例,spring-boot,elasticsearch,junit5,Spring Boot,elasticsearch,Junit5,我已经为elasticsearch CRUD操作创建了JUNIT测试用例,我给出了下面的代码。在代码评审阶段之后,我从团队得到了一个更新,我已经涵盖了测试用例的所有积极场景,但还没有涵盖消极场景。我不知道如何处理负面用例 @Test void findById() throws Exception { EmployeeInformation EmployeeGet = Eservice.findById("elcrud", "2"); assertNotNull(Employ

我已经为elasticsearch CRUD操作创建了JUNIT测试用例,我给出了下面的代码。在代码评审阶段之后,我从团队得到了一个更新,我已经涵盖了测试用例的所有积极场景,但还没有涵盖消极场景。我不知道如何处理负面用例

@Test
void findById() throws Exception {

    EmployeeInformation EmployeeGet = Eservice.findById("elcrud", "2");

    assertNotNull(EmployeeGet.getId());

    assertNotNull(EmployeeGet.getFirstName());

    assertNotNull(EmployeeGet.getLastName());

}

@Test
void deleteProfileDocument() throws Exception {

    String Result = Eservice.deleteProfileDocument("elcrud", "3");

    System.out.println(Result);

    assertEquals(Result, "DELETED");
}

@Test
void search() throws Exception {

    List<EmployeeInformation> Emp=Eservice.searchByTechnology("Lucidworks","elcrud");

    System.out.println(Emp.size());

    int Result = Emp.size();

    assertTrue(Result  >= 0 );
}

@Test
void searchByName() throws Exception {

    List<EmployeeInformation> Emp=Eservice.findProfileByName("junit","elcrud");

    System.out.println(Emp.size());

    int Result = Emp.size();

    assertTrue(Result  >= 0 );
}
@测试
void findById()引发异常{
EmployeeInformation EmployeeGet=Eservice.findById(“elcrud”,“2”);
assertNotNull(EmployeeGet.getId());
assertNotNull(EmployeeGet.getFirstName());
assertNotNull(EmployeeGet.getLastName());
}
@试验
void deleteProfileDocument()引发异常{
字符串结果=Eservice.deleteProfileDocument(“elcrud”,“3”);
系统输出打印项次(结果);
资产质量(结果,“删除”);
}
@试验
void search()引发异常{
列出Emp=Eservice.searchByTechnology(“Lucidworks”、“elcrud”);
System.out.println(Emp.size());
int Result=Emp.size();
assertTrue(结果>=0);
}
@试验
void searchByName()引发异常{
List Emp=Eservice.findProfileByName(“junit”、“elcrud”);
System.out.println(Emp.size());
int Result=Emp.size();
assertTrue(结果>=0);
}

有人能帮我实现上述代码的JUNIT测试用例的负面场景吗?

我想,如果ES操作失败、没有结果或出现任何其他意外场景,您的技术团队需要测试

一个例子可能是删除概要文件:

如果删除操作成功,您已经完成了测试。但如果此操作失败或未成功处理,则没有测试

@Test
void deleteProfileDocument() throws Exception {

    //here you delete a profile which is NOT in the index
    String Result = Eservice.deleteProfileDocument("elcrud", "3");

    System.out.println(Result);

    //and here you asssert the negative result. (Not sure which result will come)
    assertEquals(Result, "NOT_FOUND");
}
我还看到,在您的测试中,如果出现错误,您会抛出一个异常。这可能是另一个很好的负面测试场景。因此,如果您可以向ES发送一个操作,并且该操作引发一个异常,那么您可以创建一个测试来预期该异常

对于Junit4,您可以使用以下内容:

@Test(expected = YourExpectedException.class)
对于Junit5,您可以使用以下内容:

Exception exception = assertThrows(YourExpectedException.class, () -> {
        Eservice.findProfileByName(Exception values);
    });

    String expectedMessage = "expected message";
    String actualMessage = exception.getMessage();

    assertTrue(actualMessage.contains(expectedMessage));

按附加问题编辑

如果你有一个阳性和阴性的测试,我建议把它们放在不同的测试方法中。因此,没有必要对事情进行详尽的评论

在您的情况下,可能是这样的:

肯定的:

@Test
void testUpdateItem_POSITIVE() throws Exception {
....
}
否定:

@Test
void testUpdateItem_NEGATIVE() throws Exception {
....
}

取而代之的是NotFound放置您的异常,该异常在使用不存在的id搜索配置文件时抛出

Hi Patrick,正如您所建议的,我已经创建了代码示例并在下面给出。请查看我的代码并回答我的问题?如果有任何澄清,请发表您的意见。提前谢谢
    @Test(expected = NotFound.class)
void shouldThrowExceptionWhenProfileIsNotExist() throws Exception {

    Eservice.findById("elcrud", "some_id");
}