模拟Mongo集合时出现NullPointerException

时间:2018-10-22 08:59:47

标签: java junit nullpointerexception powermockito mongo-java-driver

我正在尝试使用JUnit编写一种方法的单元测试,该方法只需获取特定的记录即可。我正在使用PowerMockito创建必要的模拟。但是,当我运行测试时,我得到了NullPointerException。

正在测试的方法如下:

public InputStream fetchBlankTemplate() {   

    MongoDatabase database = mongoClient.getDatabase("randomName");
    MongoCollection<Document> collection = database.getCollection("Templates");
    InputStream fileInputStream = new ByteArrayInputStream(
                                    collection.find(Filters.eq("templateId", 1))
                                    .first()
                                    .get("file", Binary.class)
                                    .getData());        
    mongoClient.close();

    return fileInputStream;     
}

上述方法的单元测试:

@RunWith(PowerMockRunner.class)
@PrepareForTest(DBConnection.class)
public class randomTest {

    @InjectMocks
    DBConnection dbcTest;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

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


        MongoClient mockClient = PowerMockito.mock(MongoClient.class);
        MongoDatabase mockDatabase = PowerMockito.mock(MongoDatabase.class);
        MongoCollection<Document> mockCollection = PowerMockito.mock(MongoCollection.class);                

        Binary mockBinary = PowerMockito.mock(Binary.class);
        byte[] testByteArray = new byte[1000];

        PowerMockito.when(mockClient.getDatabase("randomName")).thenReturn(mockDatabase);
        PowerMockito.when(mockDatabase.getCollection("Templates")).thenReturn(mockCollection);

//getting NullPointerException for the line below
    PowerMockito.when(mockCollection.find(Filters.eq("templateId", 1)).first().get("file", Binary.class).getData()).thenReturn(testByteArray);

        InputStream testStream = dbcTest.fetchBlankTemplate();
        assertNotNull(testStream);
    }   
}

堆栈跟踪如下:

at com.random.form_upload.test.randomTest.fetchBlankRecordTest(randomTest.java:68)
    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.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    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)

正在测试的方法按预期方式工作。问题出在单元测试中。我也可以肯定地确定

时会引发异常
collection.find(Filters.eq("templateId", 1))
                                    .first()

被调用。有什么想法吗?

0 个答案:

没有答案