【发布时间】:2015-02-12 21:44:10
【问题描述】:
我正在创建一个小型 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.mockFetchValue.Response。
How to mock riak java client? 中接受的答案描述了 Mockito 不起作用的原因。
【问题讨论】:
-
您看过 riak 开发人员如何对自己的类进行单元测试吗?可能有帮助吗? github.com/basho/riak-java-client/tree/develop/src/test/java/…
-
好主意!不幸的是,他们的 FetchValue 测试看起来像这样: // TODO: 用这个做点什么。您不能模拟响应,因为父母不公开@Ignore public class FetchValueTest
-
:( 可能是一些集成测试可能是一种方式。github.com/basho/riak-java-client/tree/develop/src/test/java/…
-
另一种选择是尝试 powermock。
标签: java unit-testing riak