Spring test with @DataJpaTest can't autowire class with @Repository (but with interface repository works!)

I think I was right about the problem. After find a post on Github and read the Spring Documentation:

@DataJpaTest can be used if you want to test JPA applications. By default it will configure an in-memory embedded database, scan for @Entity classes and configure Spring Data JPA repositories. Regular @Component beans will not be loaded into the ApplicationContext.

My PersonRepository is considered a regular @Component, because it is not a Spring Data JPA repository (the interface is). So, it is not loaded.

The alternative solution is to use @SpringBootTest instead of @DataJpaTest.

The disadvantage with this solution is that will load all your context while running your test and, with this, disabling the test slicing. But do the job.

Another option, still using @DataJpaTest, is include a @Repository filter annotation, like this:

@DataJpaTest(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Repository.class))

Just another alternative might be @Import as shown here https://stackoverflow.com/a/41084739/384674.