Reputation: 453
hope you all are doing well
I'm facing some problems on testing my Spring Boot application. I created a simple API (currently has only one method, and it's working) and I created the domain tests disabling JPA configurations. When I test with JPA disabled, the tests provide the following error:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mhrehbein.curriculum.register.infrastructure.mysql.ISpringDataAboutMeRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
When I enable JPA configuration it works fine, but I would like to disable once it is unit tests. You can check the code in the following PR: https://github.com/retatu/curriculum/pull/6/files (sorry for it)
In the code you can see that all tests are broke, the tests are basically the same and the example is here: https://github.com/retatu/curriculum/blob/dev/src/test/java/com/mhrehbein/curriculum/register/domain/entity/AboutMeTest.java
Appreciate any help
Upvotes: 2
Views: 1001
Reputation: 2701
In your code you declared an interface that extends PagingAndSortingRepository
:
public interface ISpringDataAboutMeRepository extends PagingAndSortingRepository<AboutMe, UUID> {
}
It means that spring boot must autoconfigure this repository bean for you, namely, it will be configured a proxy bean of SimpleJpaRepository
.
But since you disabled DataSourceAutoConfiguration
in application-test.properties
, no DataSource
bean is configured, therefore the automatic JpaRepositoriesAutoConfiguration
is not activated and user-defined JPA-repositories, in your case ISpringDataAboutMeRepository
, are not registered, which leads to the NoSuchBeanDefinitionException
. More info about it here.
Also, if you disable HibernateJpaAutoConfiguration
, EntityManagerFactory
bean isn't configured, which is necessary to create your ISpringDataAboutMeRepository
bean.
As a result, if you want to run @SpringBootTest
, you need to remove your spring.autoconfigure.exclude
property from application-test.properties
and configure a database for tests, e.g. in-memory H2-database:
Add to your build.gradle
:
testImplementation group: 'com.h2database', name: 'h2', version: '1.4.200'
Your application-test.properties
:
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa
Otherwise, you can use @MockBean
annotation on your test classes to mock your ISpringDataAboutMeRepository
so:
@SpringBootTest
@ExtendWith(SpringExtension.class) // remove it since @SpringBootTest already contains it
@TestPropertySource(locations = "classpath:application-test.properties")
@MockBean(ISpringDataAboutMeRepository.class)
public class AboutMeTest {
...
}
Upvotes: 2
Reputation: 1255
Spring has it's spring application context which is working as a container of beans. The BeanFactory
represents spring's inversion of control container. What it does is exposing beans to the application. When your application requires a bean which is not available then it throws NoSuchBeanDefinitionException
.
I think your question and the root cause is best answered in the below question. Hope it will five you insights of the problem.
What is a NoSuchBeanDefinitionException and how do I fix it?
Upvotes: 1