如何创建Single.just(Void) [英] How to create Single.just(Void)
问题描述
我正在为我的应用程序编写一些单元测试用例.我想模拟 MongoClient
update
方法,但是更新返回 Single< Void>
.
I am writing some unit test cases for my application. I want to mock MongoClient
update
method, but the update returns Single<Void>
.
when(mongoClient.rxUpdate(anyString(), any(JsonObject.class), any(JsonObject.class)))
.thenReturn(Single.just(Void))
现在 Single.just(Void)
不起作用,正确的方法是什么?
Now Single.just(Void)
doesn't work, what is the correct way of doing it?
-更新-
因此,我正在为 updateUserProfile
方法编写单元测试,并且为此我模拟了 service
.但是 service.updateAccount
方法返回是我无法模拟的.
So I am writing unit test for updateUserProfile
method and for that I have mocked service
. But the service.updateAccount
method return is something I am not able to mock.
//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);
}
由于 mongoClient.rxUpdate
的返回类型为 Single< Void>
,因此我无法模拟该部分.
Because the return type of mongoClient.rxUpdate
is Single<Void>
, I am not able to mock that part.
目前,我已经确定的解决方法是:
For now the workaround which I have figured out is:
public Single<Boolean> updateAccount(JsonObject query, JsonObject update){
return mongoClient.rxUpdate("accounts", query, update).map(_void -> true);
}
但这只是一个很简单的方法,我想知道如何才能准确地创建 Single< Void>
But this is just a hacky way of doing it, I want to know how can I exactly create Single<Void>
推荐答案
有一种返回 Single< Void>
的方法可能会引起一些担忧,因为一些用户已经在评论中表达了对此的看法.
Having a method returning Single<Void>
may raise some concerns, as some users have already expressed their view on this in the comments.
但是,如果您坚持这样做,而您确实需要对其进行模拟(无论出于何种原因),肯定有多种方法可以创建 Single< Void>
实例,例如,您可以使用Single类的create方法:
But if you are stuck with this and you really need to mock it (for whatever reason), there are definitely ways to create a Single<Void>
instance, for example you could use the create method of the Single class:
Single<Void> singleVoid = Single.create(singleSubscriber -> {});
when(test.updateAccount(any(JsonObject.class), any(JsonObject.class))).thenReturn(singleVoid);
Single<Void> result = test.updateAccount(null, null);
result.subscribe(
aVoid -> System.out.println("incoming!") // This won't be executed.
);
请注意:由于无法在没有反射的情况下实例化Void,因此您将无法实际发射Single项.
Please note: you won't be able to actually emmit a Single item, since Void can't be instantiated without reflection.
在某些情况下最终可以使用的技巧是省略泛型类型参数,而改为发出Object,但这很容易导致ClassCastException.我不建议使用此:
A trick that could eventually work in some cases is to ommit the generic type argument and emmit an Object instead, but this could lead easily to a ClassCastException. I would not recommend to use this:
Single singleObject = Single.just(new Object());
when(test.updateAccount(any(JsonObject.class), any(JsonObject.class))).thenReturn(singleObject);
Single<Void> result = test.updateAccount(null, null);
// This is going to throw an exception:
// "java.base/java.lang.Object cannot be cast to java.base/java.lang.Void"
result.subscribe(
aVoid -> System.out.println("incoming:" + aVoid)
);
当然,您也可以使用反射(如Minami Namikaze所建议的那样):
And of course you could use reflection as well (as already suggested by Minato Namikaze):
Constructor<Void> constructor = Void.class.getDeclaredConstructor(new Class[0]);
constructor.setAccessible(true);
Void instance = constructor.newInstance();
Single<Void> singleVoidMock = Single.just(instance);
when(test.updateAccount(any(JsonObject.class), any(JsonObject.class))).thenReturn(singleVoidMock);
Single<Void> result = test.updateAccount(null, null);
result.subscribe(
aVoid -> System.out.println("incoming:" + aVoid) // Prints: "incoming:java.lang.Void@4fb3ee4e"
);
这篇关于如何创建Single.just(Void)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!