我正在试图找到一种方法来创建一个使用HashMap的Mongo DB存储库的模拟实现。通过这种方式,我不必模拟每个数据库调用,只需让数据库充当黑盒子,然后只检查结果。
答案 0 :(得分:1)
我发现这篇文章非常有用:http://www.heavyweightsoftware.com/blog/creating-a-mock-class-for-unit-testing-with-a-spring-repository/
简而言之,这是该页面的实施:
public class MockWidgetRepository implements WidgetRepository{
Map<Long, Widget> backingMap = new HashMap<>();
@Override
public <S extends Widget> S save(S entity) {
backingMap.put(entity.getId(), entity);
return entity;
}
@Override
public <S extends Widget> List<S> save(Iterable<S> entites) {
return null;
}
@Override
public Widget findOne(Long id) {
Widget result = backingMap.get(id);
return result;
}
@Override
public boolean exists(Long aLong) {
return false;
}
@Override
public List<Widget> findAll() {
List<Widget> result = new ArrayList<>();
result.addAll(backingMap.values());
return result;
}
@Override
public Iterable<Widget> findAll(Iterable<Long> longs) {
return null;
}
@Override
public long count() {
return 0;
}
@Override
public void delete(Long aLong) {
}
@Override
public void delete(Widget entity) {
}
@Override
public void delete(Iterable<? extends Widget> entities) {
}
@Override
public void deleteAll() {
backingMap.clear();
}
@Override
public List<Widget> findAll(Sort sort) {
return null;
}
@Override
public Page<Widget> findAll(Pageable pageable) {
return null;
}
@Override
public <S extends Widget> S insert(S entity) {
return null;
}
@Override
public <S extends Widget> List<S> insert(Iterable<S> entities) {
return null;
}
@Override
public <S extends Widget> S findOne(Example<S> example) {
return null;
}
@Override
public <S extends Widget> List<S> findAll(Example<S> example) {
return null;
}
@Override
public <S extends Widget> List<S> findAll(Example<S> example, Sort sort) {
return null;
}
@Override
public <S extends Widget> Page<S> findAll(Example<S> example, Pageable pageable) {
return null;
}
@Override
public <S extends Widget> long count(Example<S> example) {
return 0;
}
@Override
public <S extends Widget> boolean exists(Example<S> example) {
return false;
}
}
答案 1 :(得分:0)
您可以使用Fongo(https://github.com/fakemongo/fongo)或https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo。后者被spring框架用作嵌入式内存Mongodb。