Я хочу реализовать тест JUnit для этой службы Spring с помощью doReturn
для имитации запроса:
@Service
public class BinCountryCheckFilterImpl {
@Autowired
@Qualifier("riskFilterService")
private RiskFilterService riskFilterService;
@Autowired
@Qualifier("binlistsService")
private BinlistsService binlistsService;
public BinCountryCheckFilterImpl() {
}
public PaymentResponse validateBinCountryCheckFilter(Merchants merchant, Contracts contract, Terminals terminal,
PaymentTransaction message, HttpServletRequest request) throws JAXBException, JsonProcessingException {
List<RiskFilters> filter_list = riskFilterService.findRiskFiltersByTerminalIdAndType(terminal.getId(), "BinCountryCheckFilter");
Optional<BinLists> db_bin_list = binlistsService.findByName(billing_address_country);
}
}
Я пробовал этот тест JUnit:
public class BinCountryCheckFilterImplTest {
Terminals terminal = null;
@InjectMocks
private BinCountryCheckFilter binCountryCheckFilter;
private BinlistsService binlistsService = Mockito.mock(BinlistsService.class);
private RiskFilterService riskFilterService = Mockito.mock(RiskFilterService.class);
@Mock
List<RiskFilters> riskFiltersList = null;
Optional<BinLists> binList = null;
@BeforeEach
public void beforeEachTest() throws IOException {
MockitoAnnotations.initMocks(this);
riskFiltersList = new ArrayList<RiskFilters>();
RiskFilters filter = new RiskFilters();
riskFiltersList.add(filter);
BinLists binLists = new BinLists();
binList = Optional.of(binLists);
terminal = new Terminals();
terminal.setId(11);
}
@Test
public void testBinCountryCheckFilterImpl() throws JsonProcessingException, JAXBException {
// when(riskFilterService.findRiskFiltersByTerminalIdAndType(anyInt(), anyString())).thenReturn(riskFiltersList);
doReturn(riskFiltersList).when(riskFilterService).findRiskFiltersByTerminalIdAndType(anyInt(), anyString());
when(binlistsService.findByName(anyString())).thenReturn(binList);
List<RiskFilters> filter_list = riskFilterService.findRiskFiltersByTerminalIdAndType(11, "BinCountryCheckFilter");
Optional<BinLists> bin = binlistsService.findByName("US");
BinCountryCheckFilterImpl binCountryCheckFilterImpl = new BinCountryCheckFilterImpl();
binCountryCheckFilterImpl.validateBinCountryCheckFilter(merchant, contract, terminal, authorizeTransactiontMockup(), request);
}
Код работаетнормально, когда я пытаюсь смоделировать запросы с помощью doReturn
....., но это не удается, когда я вызываю реальную службу Spring, чтобы протестировать ее с NPE в этой строке в классе Java службы BinCountryCheckFilterImpl
:
List<RiskFilters> filter_list = riskFilterService.findRiskFiltersByTerminalIdAndType(terminal.getId(), "BinCountryCheckFilter");
Как я могу смоделировать этот запрос SQL-запроса?
EDIT : я пробовал этот минимальный пример, но теперь он работает:
@Service
public class BinCountryCheckFilterImpl {
@Autowired
@Qualifier("riskFilterService")
private RiskFilterService riskFilterService;
@Autowired
@Qualifier("binlistsService")
private BinlistsService binlistsService;
@Autowired
public BinCountryCheckFilterImpl(
@Qualifier("binlistsService") BinlistsService binlistsService,
@Qualifier("riskFilterService") RiskFilterService riskFilterService) {
this.binlistsService = binlistsService;
this.riskFilterService = riskFilterService;
}
public BinCountryCheckFilterImpl() {
}
public void validateBinCountryCheckFilter() throws JAXBException, JsonProcessingException {
List<RiskFilters> filter_list = riskFilterService.findRiskFiltersByTerminalIdAndType(11, "BinCountryCheckFilter");
}
}
JUnit test:
public class BinCountryCheckFilterImplTest {
@Mock
private BinCountryCheckFilterImpl binCountryCheckFilter;
private BinlistsService binlistsService = Mockito.mock(BinlistsService.class);
@Mock
private RiskFilterService riskFilterService = Mockito.mock(RiskFilterService.class);
@Mock
List<RiskFilters> riskFiltersList = null;
Optional<BinLists> binList = null;
Terminals terminal = null;
@BeforeEach
public void beforeEachTest() throws IOException {
MockitoAnnotations.initMocks(this);
riskFiltersList = new ArrayList<RiskFilters>();
RiskFilters filter = new RiskFilters();
filter.setId(1);
riskFiltersList.add(filter);
BinLists binLists = new BinLists();
binLists.setId(1);
binList = Optional.of(binLists);
}
@Test
public void testBinCountryCheckFilterImpl() throws JsonProcessingException, JAXBException {
binCountryCheckFilter =
new BinCountryCheckFilterImpl(binlistsService, riskFilterService);
doReturn(riskFiltersList).when(riskFilterService).findRiskFiltersByTerminalIdAndType(anyInt(), anyString());
BinCountryCheckFilterImpl binCountryCheckFilterImpl = new BinCountryCheckFilterImpl();
binCountryCheckFilterImpl.validateBinCountryCheckFilter();
}
}
Я снова получаю NPE на той же строке.