I have a test class
@SpringBootTest
@ActiveProfiles("test")
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
public class ApplicationIntegrationTest {
@Autowired
MockMvc mvc;
@Autowired
MyRepository myRepository;
@Test
public void givenCourses() throws Exception{
MyEntity entity = new Entity();
myRepository.save(entity);
mvc.perform(get("/someOperation")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content()
.contentTypeCompatibleWith(MediaType.APPLICATION_JSON));
}}
MyRepository:
@Repository
public interface MyRepository extends JpaRepository<MyEntity, Long>, JpaSpecificationExecutor<MyEntity> {}
MyEntity
@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
}
application.properties
spring.datasource.url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false;MODE=MYSQL
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=test
spring.datasource.password=test@123
hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.generate-ddl=true
hibernate.hbm2ddl.auto=create
spring.flyway.validateMigrationNaming= true
spring.flyway.locations= classpath:db/migration/h2
server.port=8082
When running test the Autowired myRepository is getting null. I have tried @RunWith(SpringRunner.class)
annotation but getting exception Cannot autowire myRepository No qualifying bean of type MyRepository found