java.lang.UnsupportedOperationException 测试时
java.lang.UnsupportedOperationException when testing
尝试编写测试,使用模拟操作。发生断言时会出现异常。
CardRecordServiceTest.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 enougthGoodsToAddRecordNew(){
CartAdditionDTO cartAdditionDTO = new CartAdditionDTO(1L, 15L);
Mockito.when(goodsService.enoughQuantity(cartAdditionDTO)).thenReturn(true);
Mockito.when(cartRecords.getRecords()).thenReturn(Map.of(2L,2L));
Assertions.assertTrue(cartRecordService.addRecord(cartAdditionDTO));
verify(cartRecords,times(2)).getRecords();
verify(goodsService).enoughQuantity(any(CartAdditionDTO.class));
}
CartRecordService.java
public boolean addRecord(CartAdditionDTO cartAdditionDTO) {
if(!goodsService.enoughQuantity(cartAdditionDTO)){
return false;
}
if (cartRecords.getRecords().containsKey(cartAdditionDTO.getId())) {
Long newQuantity = cartRecords.getRecords().get(cartAdditionDTO.getId()) + cartAdditionDTO.getQuantity();
if(goodsService.enoughQuantity(new CartAdditionDTO(cartAdditionDTO.getId(), newQuantity))){
cartRecords.getRecords().put(cartAdditionDTO.getId(), newQuantity);
return true;
}
return false;
}
// here is an error at put
cartRecords.getRecords().put(cartAdditionDTO.getId(), cartAdditionDTO.getQuantity());
return true;
}
日志
java.lang.UnsupportedOperationException
在 java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142)
在 java.base/java.util.ImmutableCollections$AbstractImmutableMap.put(ImmutableCollections.java:1072)
在 com.example.store.service.CartRecordService.addRecord(CartRecordService.java:77)
在 com.example.store.ServiceUnitTests.CartRecordServiceTest.enougthGoodsToAddRecordNew(CartRecordServiceTest.java:69)
问题是您的代码试图修改地图,但您的测试代码设置了 Map.of(...)
,其中 returns 是一个不可变的地图。请改用 new HashMap<>(Map.of(....))
。
尝试编写测试,使用模拟操作。发生断言时会出现异常。
CardRecordServiceTest.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 enougthGoodsToAddRecordNew(){
CartAdditionDTO cartAdditionDTO = new CartAdditionDTO(1L, 15L);
Mockito.when(goodsService.enoughQuantity(cartAdditionDTO)).thenReturn(true);
Mockito.when(cartRecords.getRecords()).thenReturn(Map.of(2L,2L));
Assertions.assertTrue(cartRecordService.addRecord(cartAdditionDTO));
verify(cartRecords,times(2)).getRecords();
verify(goodsService).enoughQuantity(any(CartAdditionDTO.class));
}
CartRecordService.java
public boolean addRecord(CartAdditionDTO cartAdditionDTO) {
if(!goodsService.enoughQuantity(cartAdditionDTO)){
return false;
}
if (cartRecords.getRecords().containsKey(cartAdditionDTO.getId())) {
Long newQuantity = cartRecords.getRecords().get(cartAdditionDTO.getId()) + cartAdditionDTO.getQuantity();
if(goodsService.enoughQuantity(new CartAdditionDTO(cartAdditionDTO.getId(), newQuantity))){
cartRecords.getRecords().put(cartAdditionDTO.getId(), newQuantity);
return true;
}
return false;
}
// here is an error at put
cartRecords.getRecords().put(cartAdditionDTO.getId(), cartAdditionDTO.getQuantity());
return true;
}
日志
java.lang.UnsupportedOperationException 在 java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142) 在 java.base/java.util.ImmutableCollections$AbstractImmutableMap.put(ImmutableCollections.java:1072) 在 com.example.store.service.CartRecordService.addRecord(CartRecordService.java:77) 在 com.example.store.ServiceUnitTests.CartRecordServiceTest.enougthGoodsToAddRecordNew(CartRecordServiceTest.java:69)
问题是您的代码试图修改地图,但您的测试代码设置了 Map.of(...)
,其中 returns 是一个不可变的地图。请改用 new HashMap<>(Map.of(....))
。