我试图在Spring项目中为类编写一个JUnit测试用例。但我一直有例外。
com.lordofthejars.nosqlunit.core.NoSqlAssertionError:预期的集合名是学生,但是插入集合名是[]
有什么建议吗?非常感谢!
@Configuration
@EnableMongoRepositories
@ComponentScan("com.tp.repository")
public class TestConfig extends AbstractMongoConfiguration {
@Bean
public Mongo mongo() {
return new Fongo("Fongo").getMongo();
}
@Bean
protected String getDatabaseName() {
return "test";
}
}单元测试类
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestConfig.class)
public class StudentRepositoryTests {
@Autowired
StudentRepository studentRepository;
@Autowired
private ApplicationContext applicationContext;
@Rule
public MongoDbRule mongoDbRule = MongoDbRuleBuilder.newMongoDbRule()
.defaultSpringMongoDb("student");
@Test
@ShouldMatchDataSet(location = "/student.json")
public void shouldSave() {
studentRepository.save(createStudent());
}
private Student createStudent() {
Student student = new Student();
student.setFirstName("First");
student.setLastName("Last");
return student;
}
}学生班
@Document(collection="student")
public class Student {
@Id
private String id;
@Indexed
private String firstName;
@Indexed
private String lastName;
....
}发布于 2015-04-15 07:31:29
是的,理论上应该有效,但出于某种原因,我不知道为什么它不起作用。诚然,我的示例和测试使用的是xml方法,而不是配置方法,但在这两种情况下都应该有效。似乎有两个Fongo实例,其中一个用于插入数据,另一个用于检查数据。您能在github帐户中提供代码吗?这样我就可以调试并查看发生了什么?
https://stackoverflow.com/questions/29620819
复制相似问题