相关疑难解决方法(0)

如何用mockito模拟最后一堂课

我有一个最后的课,类似这样:

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一起做这件事?

java junit mockito

169
推荐指数
13
解决办法
21万
查看次数

如何创建Single.just(Void)

我正在为我的应用程序编写一些单元测试用例。我想模拟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.rxUpdateSingle<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>

junit unit-testing java-8 vert.x rx-java

5
推荐指数
1
解决办法
1702
查看次数

标签 统计

junit ×2

java ×1

java-8 ×1

mockito ×1

rx-java ×1

unit-testing ×1

vert.x ×1