Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
Spring boot unable to autowire class in tests after disable JPA

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

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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?

over 4 years ago · Santiago Trujillo Relatório

0

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 {
      ...
} 
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda