I have a Spring Boot application which includes UserRepository
, it extends JpaRepository
and marked as @Repository
. I want to create a test class UserRepository
which starts like that:
@ActiveProfiles("test")
@ExtendWith(SpringExtension.class)
@DataJpaTest
class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
@Test
void testFindByUsername_Ok() {
...
yaml configuration:
spring:
datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: user
password: pwd
jpa:
database-platform: org.hibernate.dialect.H2Dialect
I have PostgreSQL database and want to use H2 as embedded DB for testing.
pom: parent: spring-boot-starter-parent 2.7.7 dependencies: h2 2.1.214, postgresql 42.6.0, junit-jupiter-engine 5.9.2 (all latest for March 2023).
While launching the test case, the following error occurs:
Failed to load ApplicationContext. @ComponentScan ANNOTATION type filter requires an annotation type: UserRepository. I don't use @ComponentScan or @SpringBootApplication.
I tried using those annotations on test class and they didn't help:
@EnableJpaRepositories("my.base.package.dao.repository")
@EntityScan("my.base.package.dao.model.")
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
Package of test matches package of repository.
I cannot add @SpringBootTest
on my test because it conflicts with @DataJpaTest
:
Configuration error: found multiple declarations of @BootstrapWith for test class UserRepositoryTest.
What am I missing here? How to sovle this problem: Failed to load ApplicationContext @ComponentScan ANNOTATION type filter requires an annotation type