我正在创建一个小型 java 服务,它根据所选地点返回餐厅列表。 使用 com.basho.riak:riak-client:2.0.0 从 Riak 检索数据,并将读取操作包装在 TenacityCommand 中。
下面描述了重要的类,如果您能帮助我创建一个可靠且简单的单元测试,我将很高兴。
命令是使用工厂创建的:
package service.command.factory;
import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.commands.kv.FetchValue;
import com.basho.riak.client.core.query.Location;
import com.basho.riak.client.core.query.Namespace;
import domain.Place;
import service.command.FetchRestaurantsCommand;
public class FetchRestaurantsCommandFactory {
private final RiakClient riakClient;
private final Namespace namespace;
public FetchRestaurantsCommandFactory(final RiakClient riakClient, final Namespace namespace) {
this.riakClient = riakClient;
this.namespace = namespace;
}
public FetchRestaurantsCommand create(final Place place) {
Location location = new Location(namespace, place.getName());
FetchValue riakCommand = new FetchValue.Builder(location).build();
return new FetchRestaurantsCommand(riakClient, riakCommand);
}
}
命令如下所示:
package service.command;
import java.util.Optional;
import service.command.keys.WhereToEatDependencyKeys;
import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.commands.kv.FetchValue;
import com.basho.riak.client.api.commands.kv.FetchValue.Response;
import com.yammer.tenacity.core.TenacityCommand;
import domain.Restaurant;
import domain.RestaurantList;
public class FetchRestaurantsCommand extends TenacityCommand<Optional<RestaurantList>>{
private final RiakClient riakClient;
private final FetchValue fetchValue;
public FetchRestaurantsCommand(RiakClient riakClient, FetchValue fetchValue) {
super(WhereToEatDependencyKeys.RIAK_GET_RESTAURANTS);
this.fetchValue = fetchValue;
this.riakClient = riakClient;
}
@Override
protected Optional<RestaurantList> run() throws Exception {
Response response = riakClient.execute(fetchValue);
return Optional.ofNullable(response.getValue(RestaurantList.class));
}
@Override
protected Optional<RestaurantList> getFallback() {
return Optional.of(RestaurantList.createFallback(new Restaurant("My failure suggestion")));
}
}
以上类的用法如下:
Place place = // Created from url parameter
RiakClient riakClient = // created on start using the app's conf
Namespace namespace = // created on start using the app's conf
FetchRestaurantsCommandFactory factory = new FetchRestaurantsCommandFactory(riakClient, namespace);
FetchRestaurantsCommand command = factory.create(place);
return command.execute();
除了 TenacityCommand
提供的功能之外,我应该如何断言我的系统按预期获取数据?
我最初的想法是模拟 RiakClient
以返回预定义的 FetchValue.Response
,然后对生成的 RestaurantList
进行断言。
不幸的是,由于其设计原因,不可能实例化或 Mockito.mock
一个 FetchValue.Response
。
How to mock riak java client? 中接受的答案描述了为什么 Mockito 不起作用。
最佳答案
据我了解,您想编写单元测试。所以你想测试一下假设一些 Response
是否Optional<RestaurantList>
实例是否正确构造。
我能想到的就是包裹riakClient.execute(fetchValue);
在 protected (或包私有(private)的)辅助函数中,例如:
Response fetch() {
return riakClient.execute(fetchValue);
}
然后在您的测试中您可以继承 FetchRestaurantsCommand
并覆盖fetch
函数通过返回任何 Response
现在,您可以编写任何测试来查看给定 Response
是否进行转换至Optional<RestaurantList>
行为是否符合预期。
如果您需要完整的代码并且我的解释不够清楚,请让我知道提供它。
关于java - 如何测试使用 Basho 的 riak-java-client 的 java 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28488380/