Mocking a List and attempting to iterate over it

后端 未结 3 953
一生所求
一生所求 2021-01-21 02:25

Currently using Mockito to Test a method from one of my classes. My class contains a List, and the method takes in an object of the same class. The issue is when I attempt to it

3条回答
  •  广开言路
    2021-01-21 02:55

    You can't add values to the Mocked element. you can remove @Mock from the list of data and use new keyword to initilize it.

    private Shipment shipment;
    private Shipment shipment2;
    @Mock
    private Order mockOrder1;
    @Mock
    private Order mockOrder2;
    @Mock
    private Order mockOrder3;
    
    private ArrayList mockShipmentOrders;
    
    private ArrayList mockShipmentOrders2;
    
    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);
        mockShipmentOrders = new ArrayList<>();
        mockShipmentOrders2 = new ArrayList<>();
        mockShipmentOrders.add(mockOrder1);
        mockShipmentOrders.add(mockOrder2);
        mockShipmentOrders2.add(mockOrder3);
        shipment = new Shipment(1, mockShipmentOrders);
        shipment2 = new Shipment(2, mockShipmentOrders2);
    }
    
    @Test
    public void test_mergeShipments_increasesByOneWhenAShipmentOfOneAddedToAShipmentORderSizeOfTwo(){
        System.out.println(shipment);
        System.out.println(shipment2);
        shipment.mergeShipments(shipment2);
    
        assertEquals(3, shipment.getShipmentOrders().size());
    }
    

提交回复
热议问题