【问题标题】:NoSQLUnit MongoDB insert collection names are []NoSQLUnit MongoDB 插入集合名称为 []
【发布时间】: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


【解决方案1】:

是的,理论上它应该可以工作,但由于某种原因,我不知道它为什么不工作。确实,我的示例和测试使用 xml 方法而不是配置方法,但它应该适用于这两种情况。似乎会有两个 Fongo 实例,一个用于插入数据,另一个用于检查数据。您能否在 github 帐户中提供代码,以便我调试并查看发生了什么?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 2013-08-29
    • 2014-09-22
    • 1970-01-01
    • 2020-10-19
    相关资源
    最近更新 更多