繁体   English   中英

Mockito验证在第二个单元测试中失败

[英]Mockito verify fails in second unit test

我将Mockito与JUnit一起使用以实现Android项目中某个类的单元测试。问题是我在两个随后的测试中调用Mockito.verify ,它们的测试完全相同(以确保我正确使用了Mockito),但是有趣的是,第二个测试中的验证总是会失败。我怀疑在每次测试之前都需要使用@before注释或类似的东西进行一些操作,但我错过了,这里有一些我正在做的代码片段。

我使用Android Studio 3.4.1,Mockito 2.7.22和JUnit 4.12。

@Test
public void test_onStart_do_nothing() throws Exception {
    ZConnectionService zConnectionService = new ZConnectionService();
    ZConnection mockedZConnection = mock(ZConnection.class);

    doNothing().when(mockedZConnection).connect();
    zConnectionService.initConnection(mockedZConnection);

    verify(mockedZConnection, times(1)).connect();
}

@Test
public void test_onStart_throw_IO_exceptioon() throws Exception {
    ZConnectionService zConnectionService = new ZConnectionService();
    ZConnection mockedZConnection = mock(ZConnection.class);

    doNothing().when(mockedZConnection).connect();
    zConnectionService.initConnection(mockedZConnection);
    // Line above is the line that error message points to!

    verify(mockedZConnection, times(1)).connect();
}

测试功能到了

public void initConnection(ZConnection connection) {
    Log.d(TAG,"initConnection()");

    if (mConnection == null) {
        mConnection = connection;
    }

    if (!mActive) {
        mActive = true;

        if (mThread == null || !mThread.isAlive()) {
            mThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    // The code here runs in a background thread.
                    Looper.prepare();
                    mTHandler = new Handler();

                    try {
                        mConnection.connect();
                    } catch (IOException e) {
                        Intent i = null;
                        i = new Intent(ZConnectionService.UI_NOTCONNECTED);
                        i.setPackage(getApplicationContext().getPackageName());
                        getApplicationContext().sendBroadcast(i);

                        e.printStackTrace();

                        // Stop the services all together.
                        stopSelf();
                    }

                    Looper.loop();
                }
            });

            mThread.start();
        }
    }
}

我希望两项测试都可以顺利通过。 实际上,当我分别运行它们时,两个测试都通过了,但是当我运行整个套件时它们都失败了,错误是:

Wanted but not invoked:
mockedZinkConnection.connect();
-> at com.app.z.ZConnectionServiceUnitTest.test_onStart_throw_IO_exceptioon(ZConnectionServiceUnitTest.java:207)
Actually, there were zero interactions with this mock.

我认为这个问题是多线程的。 当您调用initConnection ,它将在Thread调用mConnection.connect() 。您遇到的问题是该Thread需要一些时间才能完成,最终您会调用verify(mockedZConnection, times(1)).connect(); 在线程实际到达connect()调用之前。 确保它的一种方法是在启动Thread后将其加入,它将等待Thread完成后再继续:

mThread.start();
try {
    mThread.join();
} catch (InterruptedException i) {
    i.printStackTrace();

}

现在,这两个测试都应该起作用。

当然这在代码中是不可接受的,因为它否定了Thread的使用。 您将需要其他方式进行测试。

我能想到的一种方法是在测试模拟之前等待线程完成测试:

@Test
public void test_onStart_throw_IO_exceptioon() throws Exception {
    ZConnectionService zConnectionService = new ZConnectionService();
    ZConnection mockedZConnection = mock(ZConnection.class);
    doNothing().when(mockedZConnection).connect();

    zConnectionService.initConnection(mockedZConnection);

    // Wait for the Thread to complete
    while(zConnectionService.mThread.isAlive()) {
        Thread.sleep(100);
    }
    verify(mockedZConnection, times(1)).connect();
}

尝试过,对我来说很好。 但不确定是否是最佳做法,因为您需要公开类的某些内部信息,这违反了封装

也许在您的ZConnectionService类上使用受保护的包isThreadAlive()方法是可以接受的

boolean isThreadAlive() {
    return mThread.isAlive();
}

和测试中的循环

while(zConnectionService.isThreadAlive()) {
    Thread.sleep(100);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM