0

I am trying to put an integration test for my JPA repository defined as

@Repository
public interface TaskRepository extends JpaRepository<Task, Long>, JpaSpecificationExecutor<Task> {
    @Query("SELECT r from Task r where r.user.id = :userId AND r.date >= :startDate AND r.date <= :endDate")
    List<Task> getTasksBetweenDates(@Param("userId") long userId, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate);
}

I put a test as follows:

@ExtendWith(SpringExtension.class)
@DataJpaTest
public class TaskRepositoryIntegrationTest {
    @Autowired
    private TaskRepository taskRepository;

     <test methods>
}

However, IntelliJ complains "Could not autowire. No beans of TaskRepository type found".

Can someone advise?

EDIT: The error message is:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

    
ZZZ
  • 645
  • 4
  • 17
  • can you show the error message with complete stack trace – Ryuzaki L May 05 '21 at 23:41
  • Error message added. – ZZZ May 05 '21 at 23:56
  • Check [this](https://stackoverflow.com/questions/47487609/unable-to-find-a-springbootconfiguration-you-need-to-use-contextconfiguration) and [this](https://www.baeldung.com/spring-boot-unable-to-find-springbootconfiguration-with-datajpatest) links it should help. – Grigorii Riabov May 06 '21 at 07:23

1 Answers1

0

In my project, we used Spring Boot (v2.4.3). The following code worked fine for testing JPA repository.

@SpringBootTest
public class PersonRepositoryTest {

  @Autowired
  PersonRepository personRepository;
...
Peter Quan
  • 788
  • 4
  • 9