@DataJpaTest requires a class outside the test

In a SpringBoot application, I would like to do some tests regarding the repository level.

@RunWith(SpringRunner.class)
@DataJpaTest
public class VisitRepositoryTest {

     @Autowired
     private TestEntityManager entityManager;

     @Autowired
     private VisitRepository visitRepository;

     ...
}

When I try to run a test from VisitRepositoryTest, I get an error messageDefaultConfigService

The defaultConfigService field in com.norc.Application requires a bean of type 'com.norc.service.DefaultConfigService' that cannot be found.

So, you need to run for this Application?

I tried to put a bean from DefaultConfigServicein VisitRepositoryTest, but this is not allowed.

This class is used in my application.

@EntityScan(basePackageClasses = {Application.class, Jsr310JpaConverters.class})
@SpringBootApplication
@EnableScheduling
public class Application implements SchedulingConfigurer {

      @Autowired
      private DefaultConfigService defaultConfigService;
      ...
}

How to do it?


Edit

In my application, I use this class on the cron tab:

@Service
public class DefaultConfigServiceImpl implements DefaultConfigService {

    private final DefaultConfigRepository defaultConfigRepository;

    @Autowired
    public DefaultConfigServiceImpl(final DefaultConfigRepository defaultConfigRepository) {
         this.defaultConfigRepository = defaultConfigRepository;
    }
}
+4
source share
1

, @SpringBootApplication , , , @SpringBootConfiguration , .

. @DataJpaTest, Spring Boot , . . @SpringBootConfiguration: , , , , .

@SpringBootApplication @SpringBootConfiguration, , , ( IMO, ).

( ), ( , @DataJpaTest). , . beans .

, , SchedulingConfiguration - ( , ). , , SchedulingConfigurer. , , .

, , FooService . , dimitrisli ( ),

@RunWith(SpringRunner.class)
@DataJpaTest
@Import(FooService.class)
public class VisitRepositoryTest {
  ...
}
+19

Source: https://habr.com/ru/post/1663427/


All Articles