Spring Data - MongoDB - JUnit 测试
我会对测试有一个问题 Spring Data - MongoDB 和 JUnit.
UserRepository 如下:
我得到以下例外:
无法创建一个实例 [... .repository.UserRepository]: 指定的类是接口
现在我的问题将是如何做到的 UserRepository 是一个实例,但没有执行阶级,因为 Spring Data 实现自己的吗? 如果我不说 USerRepository 通过 @Repository, 那 Spring 不会创建一个对象 bean
[EDIT]
我尝试了一个关联的链接的示例,如果我在主要方法的顶部启动应用程序,它就很好。
然后我试图实现一个测试类,但在这种情况下我得到了相同的例外:
创建beans命名时出错 'hello.test.TestClass': 注射加载的依赖性失败; 包括一个例外 org.springframework.beans.factory.BeanCreationException: 也许不吧 autowire 领域:私人 hello.CustomerRepository hello.test.TestClass.repository; 包括一个例外 org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有资格挑解豆型 [hello.CustomerRepository] 发现取决于:预计不少 1 德雷迪伊有资格获得 autowire 这种依赖的候选人。 注释依赖性: {@org.springframework.beans.factory.annotation.Autowired/required=true/}
我的测试课看起来如此 src/test/java/hello/test /hello.test是一个包/:
和我的 CustomerRepository 看起来像那样 /用注释 @Configuration/:
事实上,我不知道我需要推出测试的注释 - 也许你还有一个优惠,以便我可以解决这个问题。
</customer></customer,></user,>
@RunWith/SpringJUnit4ClassRunner.class/
@SpringApplicationConfiguration/classes = { UserRepository.class, User.class }/
public class MyJUnitTest {
UserRepository 如下:
@Repository
public interface UserRepository extends MongoRepository<user, string=""> {
User findByUsername/final String username/;
}
我得到以下例外:
无法创建一个实例 [... .repository.UserRepository]: 指定的类是接口
现在我的问题将是如何做到的 UserRepository 是一个实例,但没有执行阶级,因为 Spring Data 实现自己的吗? 如果我不说 USerRepository 通过 @Repository, 那 Spring 不会创建一个对象 bean
[EDIT]
我尝试了一个关联的链接的示例,如果我在主要方法的顶部启动应用程序,它就很好。
然后我试图实现一个测试类,但在这种情况下我得到了相同的例外:
创建beans命名时出错 'hello.test.TestClass': 注射加载的依赖性失败; 包括一个例外 org.springframework.beans.factory.BeanCreationException: 也许不吧 autowire 领域:私人 hello.CustomerRepository hello.test.TestClass.repository; 包括一个例外 org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有资格挑解豆型 [hello.CustomerRepository] 发现取决于:预计不少 1 德雷迪伊有资格获得 autowire 这种依赖的候选人。 注释依赖性: {@org.springframework.beans.factory.annotation.Autowired/required=true/}
我的测试课看起来如此 src/test/java/hello/test /hello.test是一个包/:
@ComponentScan/"hello"/
@EnableMongoRepositories/basePackages = "hello"/
@RunWith/SpringJUnit4ClassRunner.class/
@SpringApplicationConfiguration/classes = { CustomerRepository.class, Customer.class }/
public class TestClass {
@Autowired
private CustomerRepository repository;
@Test
public void testMethod// {
System.out.println/"repositoryd: " + repository/;
}
}
和我的 CustomerRepository 看起来像那样 /用注释 @Configuration/:
@Configuration
public interface CustomerRepository extends MongoRepository<customer, string=""> {
public Customer findByFirstName/String firstName/;
public List<customer> findByLastName/String lastName/;
}
事实上,我不知道我需要推出测试的注释 - 也许你还有一个优惠,以便我可以解决这个问题。
</customer></customer,></user,>
没有找到相关结果
已邀请:
3 个回复
窦买办
赞同来自:
您可以使用
初始下载所有配置 spring.
你的测试看起来像
当然你想使用 embedded mongodb 用于测试,所以添加
八刀丁二
赞同来自:
不需要注释
或者
. Spring 在扩展存储库的基本类时,请为您提供。
设置存储库 Mongo, 你需要扩展
有以下注释。
您还想为模块化使用内存中的数据库 / 集成测试以便它们不会更改生产数据库。
要启用它只是添加以下依赖项:
最后,配置测试类
八刀丁二
赞同来自:
您需要指定配置类。 N。
, 我们
, 可能不是我假设的那样。
开始基础知识 Spring 和 Spring Data MongoDB, 一定要熟悉自己
https://spring.io/guides/gs/ac ... godb/
工作 .