如何在删除时使用Mockito制作JUnit

问题描述 投票:0回答:1

我需要你们的一点帮助。

我想对此代码进行测试:

//Delete - Patient
void deletePatient(long id) throws PatientNotFoundException {
    long deletedRecord = clientWrapperWrite.deleteSingle(PATIENT_COLLECTION_NAME, id);
    if (deletedRecord == 0) {
        throw new PatientNotFoundException(id);
    }
}

这是我为测试所做的

    //Delete Patient TestUnit - OnProgress (SOS)
@Test
public void deletingEmptyPatient() throws PatientNotFoundException, IOException {
    MongoClientWrapper mockClientWrapperWrite = mock(MongoClientWrapper.class);
    MongoClientWrapper mockClientWrapperRead = mock(MongoClientWrapper.class);
    Patient dummyPatient = createSinglePatient();

    myPatientFacade = MyPatientFacadeBuilder.create("conf/mypatient-server-test.properties")
            .withMongoClientWrapperRead(mockClientWrapperRead)
            .withMongoClientWrapperWrite(mockClientWrapperWrite)
            .build();

    myPatientFacade.addSinglePatient(dummyPatient);
    myPatientFacade.deletePatient(10001L);


    ArgumentCaptor<Long> patientIdCaptor = ArgumentCaptor.forClass(Long.class);
    ArgumentCaptor<String> collectionNameCaptor = ArgumentCaptor.forClass(String.class);

    verify(mockClientWrapperWrite,times(1))
            .deleteSingle(collectionNameCaptor.capture(),patientIdCaptor.capture());

}

这就是调试器告诉我的内容:

关于职务:

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito2</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>

这是调试器告诉我的:They said i dont insert the person, but using the function before delete i add the person

我仍在学习如何使用JUnit和Mockito学习。请指导我。谢谢您的光临,祝您有个美好的一天

java intellij-idea junit mockito powermock
1个回答
-1
投票

请使用H2或HSQL,并在存储库层测试用例上准备单独的层,而不是来自控制器层。

遵循步骤

  • 插入一条记录
  • 选择记录(大小应大于1)
  • 删除相应记录
  • 选择记录并验证(大小为零或空异常)

乐于助人]

© www.soinside.com 2019 - 2024. All rights reserved.