Unit test-repo-assertion notes

Unit test-repo-assertion notes

Record the problems encountered when writing the unit test of the repo class

Test framework use Robolectric+Mockito

Network framework: rxjava2+retrofit2

Initially, I wrote TestRuleinheritance ExternalResourceto help me do what I should do before and after executing the repo unit test method, for example:

1. usertoken
2. `Robolectric` 
 

When it ends

1. 
 

Why make an assertion at the end?

Because the class I tested is a repo, which is specifically used to interact with the server. The type of assertion is very single. The data returned is judged on a callback, code=200, or the data is not empty. I originally did this. written:


var callBack: (Any) -> Unit = {result->
      if (result is Resource<*>) {
            Assert.assertThat((result as? Resource<*>)?.status,         CoreMatchers.`is`(Status.SUCCESS))
        } else {
            Assert.assertNotNull(result)
        }
}
 

It doesn't seem to be a problem, but a key problem 1:

Writing assert in this way will not take effect

Because assert is actually an exception, the unit testing framework catches this exception and gives you a test failure

But this callback is requested by the network. The structure of the network request is the most popular set of rxjava+retrofit. This callback method is in the method of rxjava

The rxjava method is throw Throwable, assert is invalid in the callback, the exception is not reported at all, it is thrown...

Well this is the assertion 2.0 that came to us:

var result: Any? = null
var callBack: (Any) -> Unit = {
        result = it
    }

fun after() {
    if (result is Resource<*>) {
        Assert.assertThat((result as? Resource<*>)?.status, CoreMatchers.`is`(Status.SUCCESS))
    } else {
        Assert.assertNotNull(result)
    }
}
 

This after()method is ExternalResourcethe method, it will be executed after the method of each unit test is executed, so that the total line is OK

I ran the unit tests and most of them were normal, but there was still a small problem. When I ran a unit test method alone, everything was normal, and then when I ran an entire unit test class, the assertion error would not be Appears in the unit test method, but in the after()method , which makes it difficult for me to find the problem. I thought that the assertions in the test method were all passed...actually not...it made me look for problems for a long time

Then why is it like this?

The answer is in the following code, it comes from ourExternalResource

 private Statement statement(final Statement base) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                before();
                try {
                    base.evaluate();
                } finally {
                    after();
                }
            }
        };
    }
 

Yes ExternalResourcewith trywrapped our testing methods, I do not know why I'm a little aggrieved

But fortunately, if the problem is found, it will be solved if it is found. I copied ExternalResourceall the code inside and removed it tryand let my RepoTestRuledirect implementation TestRuletemporarily find no problems... If there are new problems, I will update them in time.