1

I want to test ShipmentEntityBO method using assertThat & Contains any order. The below test function is not working since list has returned the object. Please advise me.

public class ShipmentEntityBO {
    public void addShipmentEntityToList(List<ShipmentEntity> shipmentEntityList,String shipmentDetails) {
        String splited[] = shipmentDetails.split(",");
        shipmentEntityList.add(new ShipmentEntity(new Integer(splited[0]), splited[1],
                    splited[2], new Long(splited[3]), splited[4]));
    }
}

Junit code

import java.util.ArrayList;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;

public class Junit {

    ShipmentEntityBO shipmentEntityBO;

    @Before
    public void createObjectForShipmentEntity() {
        shipmentEntityBO = new ShipmentEntityBO();  
    }

    @Test
    public void testListofShipmentEntity() {
        ArrayList<ShipmentEntity> list = new ArrayList<ShipmentEntity>();
        String details = "101,pavi,12345,8500,Toronto";
        shipmentEntityBO.addShipmentEntityToList(list, details);
        assertThat(list,containsInAnyOrder("Toronto","pavi",101,"12345",8500)); 
    }
}
glytching
  • 44,936
  • 9
  • 114
  • 120
Pavithra
  • 63
  • 11

1 Answers1

1

The following code ...

String details = "101,pavi,12345,8500,Toronto";
addShipmentEntityToList(list, details);

... populates the given list with 1 entry which is of type ShippingEntry and has been populated from the given details

However your assertion attempts to verify that the list contains 5 entries none of which are of type ShipmentEntity i.e. "Toronto","pavi",101,"12345",8500 so that assertion fails.

It is possible that the following assertion will pass:

assertThat(list,containsInAnyOrder(new ShipmentEntity(101, "pavi", "12345", 8500L, "Toronto")));

However, without seeing the constructor and the equals() method of ShipmentEntity I cannot be certain of that. And, without knowing what you are trying to do it is difficult to know what the correct fix is.

If the above does not work for you then please:

  1. Describe exactly what you are trying to do i.e. describe the purpose of your test.
  2. Update your question to include the constructor and the equals() method of ShipmentEntity
glytching
  • 44,936
  • 9
  • 114
  • 120