Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 测试修改模拟类字段的方法_Java_Junit_Field_Void_Easymock - Fatal编程技术网

Java 测试修改模拟类字段的方法

Java 测试修改模拟类字段的方法,java,junit,field,void,easymock,Java,Junit,Field,Void,Easymock,有很多问题已经问了,但没有真正有用的信息为我的具体情况 我想测试一个方法,它在类a中,它是一个void,它从类B中调用一些方法,然后修改或检查它的字段——多个字段 以下是一个例子: public class A { private B b; public A(B b){ this.b = b; } public void checkStageOne(Worksheet ws){ if( long statement){ if(B.checkStorage(ws

有很多问题已经问了,但没有真正有用的信息为我的具体情况

我想测试一个方法,它在类a中,它是一个void,它从类B中调用一些方法,然后修改或检查它的字段——多个字段

以下是一个例子:

public class A {
private B b;

public A(B b){
    this.b = b;
}

public void checkStageOne(Worksheet ws){
    if( long statement){
        if(B.checkStorage(ws)){
            ws.setThing("Nasty");
        }
        else { ws.setStatus("Not so nasty"); }
    }
}
让我们假设LinkedList以某种方式被填满了。类checkStageOne()的参数包含在类B的列表中

public class B {
private LinkedList<Worksheet> sheetList = new LinkedList<>();
private LinkedList<UsedParts> upList = new LinkedList<>();
private LinkedList<UsedParts> matList = new LinkedList<>();


public boolean checkStorage(int id){
     for(UsedParts up : upList){
        if(up.getSheetID()== id){
            for(Material mat : matList){
                if(up.getMatID() == mat.getMatID()){
                    if(mat.getQuantity() - up.getQuantityNeeded() < 0){
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
公共B类{
私有LinkedList sheetList=新建LinkedList();
私有LinkedList upList=新LinkedList();
私有LinkedList matList=新LinkedList();
公共布尔校验存储器(int-id){
用于(使用部分向上:向上){
if(up.getSheetID()==id){
用于(材料垫:材料清单){
if(up.getMatID()==mat.getMatID()){
if(mat.getQuantity()-up.getQuantityNeeded()<0){
返回false;
}
}
}
}
}
返回true;
}
我想模拟类B,但我完全不知道如何伪造B的字段,然后甚至测试它们的结果。我所知道的是,我可以使用EasyMock.expectLastCall()测试一个void;但是我需要检查它是否真的使用了断言

你有什么建议吗

VBR,
assaultpig

为了回答您的评论,您对EasyMock也做了同样的事情

但是我想你要模拟B,那不是你应该做的。只要模拟
B
。然后,你只关心
checkStorage
返回
true
false
来测试
A

大概是这样的:

B b = mock(B.class);
Worksheet ws = mock(Worksheet.class);
A a = new A(b);
expect(b.checkStorage()).andReturn(true); // expect one call to checkStorage to return true
ws.setThing("Nasty"); // expect setThing to be called

replay(b, ws);

a.checkStageOne(ws); // will call checkStorage that will return true

verify(b, ws); // will make sure setThing was called

您可以为B创建接受3个LinkedList的构造函数,然后为B的测试实例使用mockito创建:LinkedList mock1=mock(LinkedList.class);LinkedList mock2=mock(LinkedList.class);LinkedList mock3=mock(LinkedList.class);new B(mock1,mock2,mock3);然后,您可以检查是否与那些模拟汉克进行了交互,这是一个好主意。您知道如何在EasyMock中检查这一点吗?