简体   繁体   English

如何在 Robolectric 单元测试中断言 void 方法

[英]How to assertion a void method in Robolectric Unit Test

How to verify a void method call in Robolectric test case where as no data coming out the called method.如何在 Robolectric 测试用例中验证 void 方法调用,因为被调用的方法没有数据。

What to assert in this case?在这种情况下要断言什么? Below given an example of the requirement.下面给出了一个要求的例子。

public class SampleClass(){

    final String TAG = SampleClass.class.getSimpleName();

    public void log(){
        Log.d(TAG, "Entry Loggd");
    }

}

@Test
public void logEntry_test(){

    SampleClass sc = new SampleClass();
    sc.log();

    // What to assert here to verify this log method

}

First off, good on you for writing tests!!!首先,祝你写测试好!!! There are a few ways to go about testing that an internal logger is called.有几种方法可以测试调用内部记录器。 It's equally as important to understand what you're looking to test.了解您要测试的内容同样重要 Testing that a class is logging a specific message is most likely a fragile test , so be fore-warned that you probably don't need it.测试一个类正在记录一条特定的消息很可能是一个脆弱的测试,所以预先警告你可能不需要它。

Method #1: Using Robolectric方法#1:使用Robolectric

Robolectic documentation doesn't lend itself to answering basic questions, but its codebase is very well documented with its tests. Robolectic 文档不适合回答基本问题,但它的代码库及其测试记录得非常好。 A basic understanding of its principles and how shadows work can get you a long way.对其原理和阴影如何工作有一个基本的了解可以让你走得很远。 ShadowLog tests lay the ground work to this solution. ShadowLog 测试为此解决方案奠定了基础。

@RunWith(RobolectricTestRunner.class)
public class SampleClassTest {
    @Test
    public void log_writesExpectedMessage() {
        new SampleClass().log();

        ShadowLog.LogItem lastLog = ShadowLog.getLogs().get(0);
        assertThat(lastLog.msg).isEqualTo("some message");
        // or
        assertThat(lastLog.msg).isNotNull();
    }
}

Tests using Robolectric v3.1.2 Add the following to your build.gradle file:使用 Robolectric v3.1.2 进行测试将以下内容添加到您的build.gradle文件中:

testCompile 'org.robolectric:robolectric:3.1.2'

Method #2: Making use of Abstractions方法#2:利用抽象

If your sample class derives from an Android class (Activity, Fragment, Application, etc), then using android.util.Log makes sense, but bear in mind that your test will need to be a Robolectric or AndroidInstrumented test.如果您的示例类派生自 Android 类(Activity、Fragment、Application 等),那么使用android.util.Log是有意义的,但请记住,您的测试需要是 Robolectric 或 AndroidInstrumented 测试。 If your SampleClass is just some POJO, then using a simple logging framework may make your testing efforts easier.如果您的SampleClass只是一些 POJO,那么使用简单的日志记录框架可能会使您的测试工作更轻松。 For example, using Jake Wharton's Timber , your class and test can be written as follows:例如,使用 Jake Wharton 的Timber ,您的类和测试可以如下编写:

import timber.log.Timber;

public class SampleClass {
    void log() {
        Timber.d("some message");
    }
}

// SampleClassTest.java
public class SampleClassTest {
    // setting up a Tree instance that we define below
    TestTree testTree = new TestTree();

    @Test
    public void log_writesExpectedMessage() {
        // setting up Timber to us the test classes log writer
        Timber.plant(testTree);
        // invoke the logging function
        new SampleClass().log();
        // assert
        assertThat(testTree.lastMessage).isEqualTo("some message");
    }

    private class TestTree extends Timber.Tree {
        private String lastMessage;

        @Override
        protected void log(int priority, String tag, String message, Throwable t) {
            lastMessage = message;
        }
    }
}

Good luck, happy testing!祝你好运,测试愉快!

In my understanding you want to mock static methods.据我了解,您想模拟静态方法。 I guess, using static mocks are not the most elegant way to testing.我想,使用静态模拟并不是最优雅的测试方式。 Better to use an abstraction as recommended by abest .最好使用abest推荐的抽象。 Although, it can be done with PowerMock .虽然,它可以用PowerMock来完成。

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

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