Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

java.lang.UnsupportedOperationException when testing

Try to write test, operating with mocks. When assertion happens there is an exception.

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    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;
    }

Log

java.lang.UnsupportedOperationException
at java.base/java.util.ImmutableCollections.uoe(ImmutableCollections.java:142)
at java.base/java.util.ImmutableCollections$AbstractImmutableMap.put(ImmutableCollections.java:1072)
at com.example.store.service.CartRecordService.addRecord(CartRecordService.java:77)
at com.example.store.ServiceUnitTests.CartRecordServiceTest.enougthGoodsToAddRecordNew(CartRecordServiceTest.java:69)

>Solution :

The problem is that your code attempts to modify a map, but your test code is setup with Map.of(...), which returns an immutable map. Use new HashMap<>(Map.of(....)) instead.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading