Nosuchbeandefinitionexception Expected At Least 1 Bean Which Qualifies as Autowire Candidate for This Dependency

Expected at least 1 bean which qualifies as autowire candidate in Test

I found a solution to my problem! This works fine for me
https://stackoverflow.com/a/29774201/15349979

Glad if this helps someone

expected at least 1 bean which qualifies as autowire candidate

So, the issue was the lack of MySQL connector dependency and MySQL version in my pom.xml, due to which it was not able to connect to db and hence resulting in the above error.

    <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>

As soon as I included this dependency, it was no longer throwing the above-mentioned error.

Also, I modified application.properties with the below-mentioned detail:-

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

NoSuchBeanDefinitionException expected at least 1 bean which qualifies as autowire candidate for this dependency

Add the package of JpaProfessorDao to the list of packages to be scanned

<context:component-scan base-package="com.personal.online.controller, com.personal.online.dao" />

Autowiring: expected at least 1 bean which qualifies as autowire candidate

You need to use @SpringBootTest to load an application context to be used in your tests:

@RunWith(SpringRunner.class)
@SpringBootTest
public class LocationRepositoryTest {

//Your tests

}

No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:

The problem is that the Lombok constructor won't contain the @Value annotation. Rather, it will look like this:

public EmailService(String sender, UserRepository userRepository, JavaMailSender javaMailSender) {
this.sender = sender;
this.userRepository = userRepository;
this.javaMailSender = javaMailSender;
}

Due to this, Spring doesn't know how to pass the sender field, and so it complains that there's no bean of type String.

The solution is to write your own constructor:

// Add the @Value to the sender parameter
public EmailService(@Value(value = "${spring.mail.username}") String sender, UserRepository userRepository, JavaMailSender javaMailSender) {
this.sender = sender;
this.userRepository = userRepository;
this.javaMailSender = javaMailSender;
}

After that, you can remove the @Value annotation from the sender field (since it's on the constructor) and remove the @AllArgConstructor annotation.



Related Topics



Leave a reply



Submit