下面的单元测试无法模拟被测试Java代码的DeleteResult。获取一个NullPointerException。我正在JUnit上运行测试。与delete语句中的过滤器有关吗?

    @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);

    }


如果键匹配,正在测试的代码只需删除一条记录,如果没有这样的记录,则返回警告。下面的方法正在测试中,它是DBCollection类的一部分:

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$1.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$1.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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.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尝试确定是否应从collectionsDocs.deleteOne中的deleteDocument调用返回某些内容时,它无法与filter参数匹配,因此它确定collectionsDocs.deleteOne不返回任何内容。要验证这一点,只需运行以下代码:

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返回"Successful Delete" :)

07-26 03:32