Я хочу реализовать тест JUnit 5 для тестирования сервисов:
Репозиторий JPA:
@Repository
public interface RiskFilterRepository extends JpaRepository<RiskFilters, Integer> {
.....
}
Сервисный интерфейс:
public interface RiskFilterService {
......
}
Реализация сервиса:
@Service
@Qualifier("riskFilterService")
@Transactional
public class RiskFilterServiceImpl implements RiskFilterService {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private RiskFilterRepository dao;
...
}
Сервис для тестирования:
@Service
public class BinCountryCheckFilterImpl {
@Autowired
@Qualifier("riskFilterService")
private RiskFilterService riskFilterService;
public void validateBinCountryCheckFilter() throws JAXBException, JsonProcessingException {
......
}
}
JUnit test
@RunWith(SpringRunner.class)
@EnableJpaRepositories(basePackages = {"org.datalis.plugin.service.RiskFilterRepository"})
@SpringBootTest(classes = { RiskFilterServiceImpl.class, RiskFilterService.class } )
@ContextConfiguration(classes = { H2TestProfileJPAConfig.class })
@Transactional
@EnableTransactionManagement
@EntityScan(basePackages = {"org.entity"})
public class BinCountryCheckFilterImplTest {
@Autowired
@Qualifier(value = "entityManager")
EntityManager manager;
@Autowired
private RiskFilterRepository repository;
@BeforeEach
public void beforeEachTest() throws IOException {
MockitoAnnotations.initMocks(this);
}
@Test
public void testBinCountryCheckFilterImpl() throws JsonProcessingException, JAXBException {
....
}
}
Когда я запускаю код, я получаю:
Error creating bean with name 'riskFilterServiceImpl': Unsatisfied dependency expressed through field 'dao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.datalis.plugin.service.RiskFilterRepository' available: expected at least 1 bean which qualifies as autowire candidate.
Полный стек ошибок: https://pastebin.com/84D0v6EQ
У вас есть идеи, как решить эту проблему?
РЕДАКТИРОВАТЬ: Я пытался это:
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class BinCountryCheckFilterImplTest {
@Autowired
@Qualifier(value = "entityManager")
EntityManager manager;
@Autowired
private RiskFilterRepository repository;
@BeforeEach
public void beforeEachTest() throws IOException {
MockitoAnnotations.initMocks(this);
}
@Test
public void testBinCountryCheckFilterImpl() throws JsonProcessingException, JAXBException {
....
}
}
Я получаю эту ошибку при запуске кода JUnit:
Could not detect default configuration classes for test class [org.datalis.rest.api.server.filter.bincountrycheck.BinCountryCheckFilterImplTest]: BinCountryCheckFilterImplTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration.