我试图在我的一些 Spring 应用程序测试中使用这个库,但没有取得多大成功。似乎支持Spring Data MongoDB,但我没有设法让它工作。这些示例不完全遵循@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 扩展,可以为我提供一些想法,为什么指定的数据集没有被加载到数据库中?
先感谢您!