我正在尝试在我的一些Spring应用程序测试中使用this库,但没有取得多大成功。 Spring Data MongoDB似乎是supported,但我没有设法让它工作。 examples没有完全遵循@Repository方法。
假设我有一个DummyClass POJO:
@Document(collection = DummyClass.ITEMS_COLLECTION_NAME)
public class DummyClass {
public static final String ITEMS_COLLECTION_NAME = "dummy";
//Maps _id mongodb internal field
private BigInteger id;
private String a;
private String b;
public DummyClass() {
super();
}
public DummyClass(String a, String b) {
this.a = a;
this.b = b;
}
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
}
我有各自的存储库接口和自定义接口/实现:
@Repository
public interface DummyClassRepository extends MongoRepository<DummyClass, Long>, DummyClassRepositoryCustom {
}
public interface DummyClassRepositoryCustom {
/* My dummy methods declaration */
}
public class DummyClassRepositoryImpl implements DummyClassRepositoryCustom{
/* My dummy methods implementation */
}
这就是我的测试环境的样子(简化):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation=
"http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<context:component-scan base-package="org.company.project.data.*"/>
<!-- Fongo config (used by NoSQL-Unit) -->
<bean name="fongo" class="com.foursquare.fongo.Fongo">
<constructor-arg value="InMemoryMongo" />
</bean>
<bean id="mongo" factory-bean="fongo" factory-method="getMongo" />
<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" />
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoDbFactory"/>
</bean>
<mongo:repositories base-package="org.company.project.data.repositories.mongodb"/>
<!-- Some other beans declaration -->
</beans>
显然,我的MongoDB存储库位于org.company.project.data.repositories.mongodb
。
问题是我无法将某个.json加载到内存中的MongoDB实例。到目前为止我所拥有的:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/test-context.xml"})
public class MyTest {
private static Logger logger = Logger.getLogger(MyTest.class);
@Autowired
private ApplicationContext applicationContext;
@Rule
public MongoDbRule mongoDbRule = newMongoDbRule().defaultSpringMongoDb("test");
@Autowired
private DummyClassRepository dummyClassRepository;
@Test
@UsingDataSet(locations = {"/dummy.json"}, loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void myTest(){
logger.info(dummyClassRepository.findAll().size());
assert(false);
}
}
.json文件的内容遵循文档中指定的格式,但无论我指定什么,在启动测试方法时,集合始终为空。例如,此.json文件的示例可能是:
{
"dummy": [
{
"_class": "org.company.project.data.model.DummyClass",
"_id": "51557362e4b083f9e619f99c",
"a": "a",
"b": "b"
},
{
"_class": "org.company.project.data.model.DummyClass",
"_id": "51557362e4b083f9e619f99d",
"a": "c",
"b": "d"
}
]
}
令我震惊的是,曾经,在测试方法本身中,我能够毫无问题地将我的虚拟实例添加到数据库,这基本上告诉我存储库实际上工作正常。
是否有人使用此JUnit扩展,可以为我提供一些想法,说明为什么指定的数据集没有加载到数据库中?
提前谢谢!
答案 0 :(得分:3)
如果你想要一个如何做到这一点的好例子。在https://github.com/JohnathanMarkSmith/spring-mongo-demo
查看johnathan mark smiths git示例答案 1 :(得分:2)
Jaranda,
似乎您的spring存储库和nosqlunit正在处理不同的数据库。尝试修改弹簧配置以使用相同的nosqlunit数据库。
<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" dbname="test" />