【发布时间】:2019-04-08 05:39:25
【问题描述】:
我想使用assertThat & Contains 任何顺序来测试ShipmentEntityBO 方法。由于 list 已返回对象,因此以下测试功能不起作用。请给我建议。
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 代码
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));
}
}
【问题讨论】:
-
containsInAnyOrder方法的代码在哪里?
-
你能告诉我们
ShipmentEntity的构造函数吗?