Java Spring 启动测试“没有可用类型的合格 bean”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44925324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:47:54  来源:igfitidea点击:

Spring boot test "No qualifying bean of type available"

javaspringspring-mvccassandra

提问by fengye87

I'm quite a newbie to Spring boot, but here's the problem I'm facing now:

我是 Spring Boot 的新手,但这是我现在面临的问题:

// Application.java
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

  @Autowired
  private Cluster cluster = null;

  @PostConstruct
  private void migrateCassandra() {
    Database database = new Database(this.cluster, "foo");
    MigrationTask migration = new MigrationTask(database, new MigrationRepository());
    migration.migrate();
  }
}

So basically, I'm trying to bootstrap a spring application, and after that, do some cassandra migrations.

所以基本上,我正在尝试引导一个 spring 应用程序,然后做一些 cassandra 迁移。

I also have defined a repository for my user model:

我还为我的用户模型定义了一个存储库:

// UserRepo.java
public interface UserRepo extends CassandraRepository<User> {
}

Now I'm trying to test my repo class using the following simple test case:

现在我正在尝试使用以下简单的测试用例来测试我的 repo 类:

// UserRepoTest.java
@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase(replace = Replace.NONE)
@DataJpaTest
public class UserRepoTest {

  @Autowired
  private UserRepo userRepo = null;

  @Autowired
  private TestEntityManager entityManager = null;

  @Test
  public void findOne_whenUserExists_thenReturnUser() {
    String id = UUID.randomUUID().toString();
    User user = new User();
    user.setId(id);
    this.entityManager.persist(user);

    assertEquals(this.userRepo.findOne(user.getId()).getId(), id);
  }

  @Test
  public void findOne_whenUserNotExists_thenReturnNull() {
    assertNull(this.userRepo.findOne(UUID.randomUUID().toString()));
  }
}

I would expect the test to pass, but instead, I got an error saying "No qualifying bean of type 'com.datastax.driver.core.Cluster' available". It looks like spring failed to autowire the clusterobject, but why is that? How do I fix this? Thanks a lot!

我希望测试通过,但相反,我收到一条错误消息,提示“没有可用的 'com.datastax.driver.core.Cluster' 类型的合格 bean”。看起来 spring 未能自动装配cluster对象,但这是为什么呢?我该如何解决?非常感谢!

采纳答案by JimHawkins

The test environment needs to know where your beans are defined, so you have to tell it the location.

测试环境需要知道你的 bean 是在哪里定义的,所以你必须告诉它位置。

In your test class, add the @ContextConfigurationannotation:

在您的测试类中,添加@ContextConfiguration注释:

@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase(replace = Replace.NONE)
@DataJpaTest
@ContextConfiguration(classes = {YourBeans.class, MoreOfYourBeans.class})
public class UserRepoTest {

  @Autowired
  private UserRepo userRepo = null;

  @Autowired
  private TestEntityManager entityManager = null;