简体   繁体   English

使用 Junit5 和 mockito 进行单元测试

[英]Unit Test Using Junit5 and mockito

I am trying to write a unit test case for android using Junit-5 and mockito But how to write test case for this kind of function when there is a return statment我正在尝试使用 Junit-5 和 mockito 为 android 编写单元测试用例但是当有返回语句时如何为这种 function 编写测试用例

```public void doSomething() {
        if (isTrue()) {
            return;
        }
        some other code 
}```

As the return type of your method is void , you can only test behavior and not the state.由于您的方法的返回类型是void ,您只能测试行为而不是 state。

If your method is coordinating the invocation of other methods on other objects (collaborators), you can use Mockito to verify that the interaction with another (mocked) object happened.如果您的方法正在协调对其他对象(协作者)的其他方法的调用,您可以使用 Mockito 来验证与另一个(模拟的)object 的交互是否发生。

In case there's an early return in your method, you could ensure the invocation did not happen.如果您的方法提前返回,您可以确保调用没有发生。

Here are some further reads that might help:以下是一些可能有帮助的进一步阅读:

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

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