在 Java 中使用 Mockito 模拟 MongoDB 的 DeleteResult

Mocking DeleteResult of MongoDB using Mockito in Java

下面的单元测试无法为正在测试的 Java 代码模拟 DeleteResult。获取 NullPointerException。我是 运行 JUnit 测试。是不是和delete语句中的Filters有关?

    @InjectMocks
    DBConnection mongoConnect;

    @Mock
    MongoClient mockClient;

    @Mock
    MongoCollection<Document> mockCollection;

    @Mock
    MongoDatabase mockDB;

    @Mock
    LinkedList<String> mockArrList;

    @Mock
    MongoIterable<String> mongoIter;

    @Mock
    DeleteResult mockDeleteResult;

    @SuppressWarnings("unchecked")
    @Test
    public void deleteDocTest1() {

        Mockito.when(mockClient.getDatabase(Mockito.anyString())).thenReturn(mockDB);       

        MongoIterable<String> mongoIter = Mockito.mock(MongoIterable.class);
        Mockito.when(mockDB.listCollectionNames()).thenReturn(mongoIter);       
        Mockito.when(mongoIter.into(new LinkedList<String>())).thenReturn(mockArrList);
        Mockito.when(mockArrList.contains(Mockito.anyString())).thenReturn(true);   
        Mockito.when(mockDB.getCollection(Mockito.anyString())).thenReturn(mockCollection);
        Mockito.when(mockCollection.deleteOne(Filters.and(Filters.eq("aid", "TS123"), 
                Filters.eq("year", "2018"),
                Filters.eq("position", "testCases"))))
                .thenReturn(mockDeleteResult);
        Mockito.when(mockDeleteResult.getDeletedCount()).thenReturn(1L);

        String msg = mongoConnect.deleteDocument("TS123", "testCases", "2018");
        assertEquals("Delete Successful", msg);     

    }

如果键匹配,被测试的代码只需要删除一条记录,如果没有这样的记录,return一个警告。下面正在测试的方法是 DBCollection class:

的一部分
public String deleteDocument(String aId, String collection, String year) {

        MongoDatabase database = mongoClient.getDatabase(databaseName);

        //checking if collection is present in the DB
        boolean collectionExists = database.listCollectionNames().into(new LinkedList<String>())
                .contains(collection);

        if(collectionExists) {
            MongoCollection<Document> collectionDocs = database.getCollection(collection);
            System.out.println(assoId+" "+collection+" "+year);         
            DeleteResult deleteResult = collectionDocs.deleteOne(Filters.and(Filters.eq("aid", aId), Filters.eq("year",year), Filters.eq("position",collection)));
            if(deleteResult.getDeletedCount() == 0) //the ERROR is at this line
                return "Delete: record does not exist";
        }else {
            return "Delete: record does not exist";
        }
        mongoClient.close();
        return "Successful Delete"; 

    }   

错误的堆栈跟踪:

java.lang.NullPointerException
    at com.repo.repository.DBConnection.deleteDocument(DBConnection.java:103)
    at com.repo.form_upload.test.DBTest.deleteDocTest1(DBTest.java:138)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.mockito.internal.junit.JUnitRule.evaluate(JUnitRule.java:16)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=14=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

有什么问题吗?

这里的问题是这个期望:

Mockito.when(mockCollection.deleteOne(Filters.and(Filters.eq("aid", "TS123"),
        Filters.eq("year", "2018"),
        Filters.eq("position", "testCases"))))
        .thenReturn(mockDeleteResult);

Bson 没有实现 equals 所以当 Mockito 试图确定它是否应该 return 来自你 deleteDocument 中的 collectionsDocs.deleteOne 调用的东西时它不能匹配过滤器参数,因此它确定 collectionsDocs.deleteOne return 什么都没有。要验证这一点,只需 运行 以下代码:

Bson one = Filters.and(Filters.eq("aid", "TS123"),
        Filters.eq("year", "2018"),
        Filters.eq("position", "testCases"));
Bson two = Filters.and(Filters.eq("aid", "TS123"),
        Filters.eq("year", "2018"),
        Filters.eq("position", "testCases"));

// one and two are not equal because Bson does not implement equals so 
// we'll just fall back to the standard instance check in Object
assertNotEquals(one, two);

你的测试将通过 - 尽管对过滤器的特异性较低 - 如果你表达 deleteOne 期望是这样的:

Mockito.when(mockCollection.deleteOne(any(Bson.class))).thenReturn(mockDeleteResult);

或者,您可以使用自定义匹配器对 Bson 应用您自己的相等检查。例如,您可以将 mockCollection.deleteOne 期望更改为以下内容:

Mockito.when(mockCollection.deleteOne(argThat(new BsonMatcher(Filters.and(Filters.eq("aid", "TS123"),
        Filters.eq("year", "2018"),
        Filters.eq("position", "testCases"))))))
        .thenReturn(mockDeleteResult);

并声明BsonMatcher如下:

public class BsonMatcher implements ArgumentMatcher<Bson> {

    private BsonDocument left;

    public BsonMatcher(Bson left) {
        this.left = left.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry());
    }

    @Override
    public boolean matches(Bson right) {
        // compare as BsonDocument, since this does provide an equals()
        return left.equals(right.toBsonDocument(BsonDocument.class, MongoClient.getDefaultCodecRegistry()));
    }
}

请注意,您还需要将 assertEquals("Delete Successful", msg); 更改为 assertEquals("Successful Delete", msg);,因为 deleteDocument returns "Successful Delete" :)