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
Generate Bank Account Number With Random
How to Convert a Kotlin Source File to a Java Source File
How to Check Type of Variable in Java
Is There a Java Setting for Disabling Certificate Validation
Java Spring: Best Way to Convert a File to a Multipartfile
How to Prevent Duplicate Results in Hibernate
What Happens When Base and Derived Classes Each Have Variables With the Same Name
Java 8 Lambda Create List of Strings from List of Objects
Spring Boot - Exception Encountered During Context Initialization. How to Know the Reason of Error
Multipartexception: Current Request Is Not a Multipart Request
Pass Multiple Parameters to Rest API - Spring
How to Join Results of Multiple Tables in Spring JPA Repository
How to Set a Default Entity Property Value With Hibernate
Java How to Retrieve More Than 1 Million Rows in a Resultset
Regular Expression to Remove Everything But Characters and Numbers