如何在Junit中比较两个对象

时间:2018-10-30 04:35:58

标签: junit

我是Java和Junit的新手。请帮助编写Junit测试用例以测试未实现Equals和Hashcode功能的CargoBO方法。基本上,我需要在junit中使用Equalbuilder类比较2个对象。

public class CargoBO {
    public Cargo cargoDetails(String name,String desc,double length,double width) {
        return new Cargo(name,desc,length,width);
    }
}

public class CargoJUnit {
    Cargo cargo;

    @Before
    public void createObjectForCargo() {
        cargo = new Cargo("audi","des",123.00,234.00);
    }

    @Test
    public void testCargoDetails() {
        CargoBO cargoBO = new CargoBO();
        //assertTrue(cargo.equals(cargoBO.cargoDetails("audi","des",123.00,234.00)));
        Assert.assertEquals(cargo, cargoBO.cargoDetails("audi","des",123.00,234.00));
    }
}

1 个答案:

答案 0 :(得分:0)

适合您的方案的测试用例是

@Test
public void testCargoDetails() {
    String name = "test name";
    String desc = "desc";
    double length = 10d;
    double width = 100d;

    Cargo result = cargoBO.cargoDetails(name, desc, length, width);

    Assert.assertEquals(cargo.getName, name);
    Assert.assertEquals(cargo.getDesc, desc);
    Assert.assertEquals(cargo.getLength, length);
    Assert.assertEquals(cargo.getWidth, width);
}

您正在测试一种接受参数并调用传递这些参数的构造函数的方法。 您的测试应该验证给定的参数是否正确地由方法传递。