vendredi 30 octobre 2020

@DataJpaTest loads context so fails on missing beans

Been at this for a few hours now and tried various things with no avail.

I am trying to write basic @DataJpaTest classes to test my native queries etc.

The issue is the test seems to try load the entire SpringApplication context rather than the "slice" required for the tests.

reflectoring.io - Data JPA Tests

As far as I understand, @DataJpaTest should only loads up the bare minimum beans for Entity Management, Repositories and a few other basic beans.

I am overriding the "default H2 database" it uses (as I have custom native SQL quieries) so my test looks like:

@DataJpaTest
@ActiveProfiles("jpa")
@Log4j2
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class BrandServiceJpaDataTest {

    @Autowired
    private WebApiBrandServiceRepo brandServiceRepo;

    @Autowired
    private BrandRepo brandRepo;

    @Test
    @DisplayName("Get Brand Services from REPO")
    public void getBrandServices() {
        final List<BrandService> all = brandServiceRepo.findAll();
        log.info(all);
    }

}

Properties are simple, I was trying to use @TestContainers but would rather have this working with a basic local hosted DB:

#============ DATABASE ===========#
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/aspwtest
spring.datasource.username=dev
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true
        
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = create-drop

When running the test, I can see in the logs the application context being loaded up with and leading to missing bean errors as it seems to be trying to load far too much of the context (i.e. missing RestTemplate/Validation beans etc.).

Description:

Field validator in ms.allstar.wallet.webserver.patching.EntityPatcher required a bean of type 'javax.validation.ValidatorFactory' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'javax.validation.ValidatorFactory' in your configuration.

Is there some form of config I am missing?

I know I could probably go around annotating some beans like @ActiveProfile("jpadatatest") but that feels very hacky and would sort of defeat the purpose of @DataJpaTest existing.

Aucun commentaire:

Enregistrer un commentaire