【发布时间】:2019-06-13 12:14:35
【问题描述】:
我有一个 ApiRepository 类,它将包含我所有的 API 调用,但目前只有一个:
public class RestApiRepository {
private RestClient restClient;
public RestApiRepository(RestClient restClient) {
this.restClient= restClient;
}
public Observable<AuthResponseEntity> authenticate(String header, AuthRequestEntity requestEntity) {
return restClient.postAuthObservable(header, requestEntity);
}
}
RestClient 界面如下所示:
public interface SrsRestClient {
@POST(AUTH_URL)
Observable<AuthResponseEntity> postAuthObservable(@Header("Authorization") String authKey, @Body AuthRequestEntity requestEntity);
}
所以,我尝试运行通过的测试,但是当我生成代码覆盖率报告时,返回的代码行是红色的。
这是我的测试类:
public class RestApiRepositoryTest {
private RestApiRepository restApiRepository;
@Mock
private RestClient restClient;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
restApiRepository = Mockito.spy(new RestApiRepository(restClient));
}
@Test
public void test_success() {
String token = "token";
AuthRequestEntity requestEntity = new AuthRequestEntity();
AuthResponseEntity responseEntity = new AuthResponseEntity();
Mockito.when(restClient.postAuthObservable(token, requestEntity)).thenReturn(Observable.just(responseEntity));
}
}
我相信测试通过了,但没有得到任何验证,对吧?这不应该是什么时候 - 那么返回就足够了吗?
【问题讨论】:
-
答案是:你是对的,没有任何东西被验证,当 - then 是不够的。请参考文档How do I ask a good question?
-
真正的问题是是否值得测试这种简单的委托。老实说,只要这里没有任何逻辑,我就不会理会它。你最终会得到更多的代码和几乎为零的价值。
标签: android unit-testing mockito observable rx-java2