尝试进行模拟测试,但是有一个循环
Try to make mock test, but there is a loop
尝试编写模拟测试,但有一个 'for' 循环遍历映射键。我如何在测试方法中初始化地图。
Test.java
@ExtendWith(MockitoExtension.class)
public class CartRecordServiceTest {
private MockMvc mockMvc;
private ObjectMapper objectMapper = new ObjectMapper();
@Mock
private CartRecord cartRecords;
@Mock
private GoodsService goodsService;
@Mock
private GoodsRepository goodsRepository;
CartRecordService cartRecordService;
@BeforeEach
public void setUp() {
cartRecordService = new CartRecordService(cartRecords, goodsRepository, goodsService);
mockMvc = MockMvcBuilders
.standaloneSetup(cartRecordService)
.build();
}
@Test
public void checkAvailableTest() {
Map<Long, Long> records = new HashMap<>(Map.of(1L, 2L, 2L, 2L));
Mockito.when(goodsService.enoughQuantity(any())).thenReturn(true);
Assertions.assertTrue(cartRecordService.checkAvailable());
verify(goodsService, times(2)).enoughQuantity(any());
}
}
Method.java
@Service
public class CartRecordService {
private final CartRecord cartRecords;
private final GoodsRepository goodsRepository;
private final GoodsService goodsService;
public CartRecordService(CartRecord cartRecord, GoodsRepository goodsRepository, GoodsService goodsService) {
this.cartRecords = cartRecord;
this.goodsRepository = goodsRepository;
this.goodsService = goodsService;
}
public boolean checkAvailable(){
//here is map
final Map<Long, Long> records = cartRecords.getRecords();
for(Long id : records.keySet()){
if(!goodsService.enoughQuantity(new CartAdditionDTO(id, records.get(id)))){
return false;
}
}
return true;
}
}
日志说:
想要但未调用:
goodsService.enoughQuantity();
-> 在 com.example.store.ServiceUnitTests.CartRecordServiceTest.checkAvailableTest(CartRecordServiceTest.java:117)
实际上,与此模拟的交互为零。
想要但没有调用:
goodsService.enoughQuantity();
-> 在 com.example.store.ServiceUnitTests.CartRecordServiceTest.checkAvailableTest(CartRecordServiceTest.java:117)
实际上,与此模拟的交互为零。
尝试在 CartRecord
Class
上嘲笑你的 getRecords()
Map<Long, Long> records = new HashMap<>(Map.of(1L, 2L, 2L, 2L));
Mockito.when(cartRecords.getRecords()).thenReturn(records);
尝试编写模拟测试,但有一个 'for' 循环遍历映射键。我如何在测试方法中初始化地图。
Test.java
@ExtendWith(MockitoExtension.class)
public class CartRecordServiceTest {
private MockMvc mockMvc;
private ObjectMapper objectMapper = new ObjectMapper();
@Mock
private CartRecord cartRecords;
@Mock
private GoodsService goodsService;
@Mock
private GoodsRepository goodsRepository;
CartRecordService cartRecordService;
@BeforeEach
public void setUp() {
cartRecordService = new CartRecordService(cartRecords, goodsRepository, goodsService);
mockMvc = MockMvcBuilders
.standaloneSetup(cartRecordService)
.build();
}
@Test
public void checkAvailableTest() {
Map<Long, Long> records = new HashMap<>(Map.of(1L, 2L, 2L, 2L));
Mockito.when(goodsService.enoughQuantity(any())).thenReturn(true);
Assertions.assertTrue(cartRecordService.checkAvailable());
verify(goodsService, times(2)).enoughQuantity(any());
}
}
Method.java
@Service
public class CartRecordService {
private final CartRecord cartRecords;
private final GoodsRepository goodsRepository;
private final GoodsService goodsService;
public CartRecordService(CartRecord cartRecord, GoodsRepository goodsRepository, GoodsService goodsService) {
this.cartRecords = cartRecord;
this.goodsRepository = goodsRepository;
this.goodsService = goodsService;
}
public boolean checkAvailable(){
//here is map
final Map<Long, Long> records = cartRecords.getRecords();
for(Long id : records.keySet()){
if(!goodsService.enoughQuantity(new CartAdditionDTO(id, records.get(id)))){
return false;
}
}
return true;
}
}
日志说: 想要但未调用: goodsService.enoughQuantity(); -> 在 com.example.store.ServiceUnitTests.CartRecordServiceTest.checkAvailableTest(CartRecordServiceTest.java:117) 实际上,与此模拟的交互为零。
想要但没有调用: goodsService.enoughQuantity(); -> 在 com.example.store.ServiceUnitTests.CartRecordServiceTest.checkAvailableTest(CartRecordServiceTest.java:117) 实际上,与此模拟的交互为零。
尝试在 CartRecord
Class
getRecords()
Map<Long, Long> records = new HashMap<>(Map.of(1L, 2L, 2L, 2L));
Mockito.when(cartRecords.getRecords()).thenReturn(records);