我有一个最后的课,类似这样:
public final class RainOnTrees{
public void startRain(){
// some code here
}
}
Run Code Online (Sandbox Code Playgroud)
我在其他类中使用此类,如下所示:
public class Seasons{
RainOnTrees rain = new RainOnTrees();
public void findSeasonAndRain(){
rain.startRain();
}
}
Run Code Online (Sandbox Code Playgroud)
在我的JUnit测试类中,Seasons.java我想模拟这个RainOnTrees类.我怎么能和Mockito一起做这件事?
我正在为我的应用程序编写一些单元测试用例。我想模拟MongoClient update方法,但是更新返回Single<Void>。
when(mongoClient.rxUpdate(anyString(), any(JsonObject.class), any(JsonObject.class)))
.thenReturn(Single.just(Void))
Run Code Online (Sandbox Code Playgroud)
现在Single.just(Void)不起作用,正确的方法是什么?
-更新-
所以我正在编写updateUserProfile方法的单元测试,为此我已经嘲笑了service。但是service.updateAccount方法返回是我无法模拟的。
//Controller class
public void updateUserProfile(RoutingContext routingContext){
// some code
service.updateAccount(query, update)
.subscribe(r -> routingContext.response().end());
}
//Service Class
public Single<Void> updateAccount(JsonObject query, JsonObject update){
return mongoClient.rxUpdate("accounts", query, update);
}
Run Code Online (Sandbox Code Playgroud)
因为的返回类型mongoClient.rxUpdate是Single<Void>,所以我无法模拟该部分。
现在,我已经解决的解决方法是:
public Single<Boolean> updateAccount(JsonObject query, JsonObject update){
return mongoClient.rxUpdate("accounts", query, update).map(_void -> true);
}
Run Code Online (Sandbox Code Playgroud)
但这只是做事的拙劣方式,我想知道如何才能准确地创建 Single<Void>