【发布时间】:2015-06-19 16:16:25
【问题描述】:
我正在尝试为 Spring Boot 项目中的 Spring MongoDB Repository 类编写 JUnit 测试用例。但我不断收到异常。
com.lordofthejars.nosqlunit.core.NoSqlAssertionError:预期 集合名称是 [student] 但插入集合名称是 []
有什么建议吗?非常感谢!
@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;
....
}
【问题讨论】:
-
Student类是什么样的? -
谢谢,Szymon。已将 Student 类的 sn-p 添加到问题中。
-
问题似乎出在@ShouldMatchDataSet。如果我删除该注释,
Assert.assertEquals(1, studentRepository.count());将毫无例外地执行。会不会是 NoSQLUnit 0.8.1 和 Spring 4 之间的兼容性问题?
标签: spring junit spring-boot spring-data-mongodb