2013-06-26 45 views
2

我想在我的一些Spring应用程序测试,但没有成功使用this库。看来春季数据MongoDB是supported,但我没有设法让它工作。 examples没有完全遵循@Repository方法。NoSQLUnit MongoDB的测试:春季数据MongoDB的方法

比方说,我有一个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扩展为什么特定数据没有被加载到数据库中,可以提供我一些想法的工作?

预先感谢您!

+1

jaranda,以后你对nosqlunit和这个职位,我测试过nosqlunit意见,并为我工作得很好。可能发生的事情是,MongoDbRule和DummyClassRepository连接到不同的数据库。 MongoDbRule使用“test”数据库,并且不清楚DummyClassRepository使用的是哪个数据库。在我的MongoTemplate配置中,我明确地设置了应该使用的数据库。正如我在另一个问题中所说的,我不使用xml来配置Spring,至少在我的测试中。 –

+1

在弹簧数据文档的所有示例中,他们在db-factory上使用dbname参数:。尝试指定“测试”作为dbname –

+0

@MiguelCartagena我怎么错过了!它现在有用,谢谢!如果您将答复添加为答案,我会高兴地接受:)格拉西亚斯! – jarandaf

回答

2

Jaranda,

看来你的春天库和nosqlunit在不同的数据库工作。尝试修改你的spring配置以使用相同的nosqlunit数据库。

<mongo:db-factory id="mongoDbFactory" mongo-ref="mongo" dbname="test" />