Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot test "No qualifying bean of type available"

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

// 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.

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:

// 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 cluster object, but why is that? How do I fix this? Thanks a lot!

like image 838
fengye87 Avatar asked Jul 05 '17 11:07

fengye87


People also ask

How do I fix no qualifying bean of type?

The error is “expected at least 1 bean which qualifies as autowire candidate.” The solution is to add the injection class in the ApplicationContext using annotation @Component. The exception “NoSuchBeanDefinitionException: No qualifying bean of type” is resolved if the annotation @Component is added in the Lion class.

What is @SpringBootTest?

The @SpringBootTest annotation is useful when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests. We can use the webEnvironment attribute of @SpringBootTest to configure our runtime environment; we're using WebEnvironment.

What is WebMvcTest?

@WebMvcTest annotation is used for Spring MVC tests. It disables full auto-configuration and instead apply only configuration relevant to MVC tests. The WebMvcTest annotation auto-configure MockMvc instance as well.


1 Answers

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

In your test class, add the @ContextConfiguration annotation:

@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; 
like image 164
JimHawkins Avatar answered Sep 19 '22 06:09

JimHawkins