我正在尝试在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;
....
}
答案 0 :(得分:0)