Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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 easymock如何模拟/设置公共最终变量_Java_Unit Testing_Testing_Junit_Easymock - Fatal编程技术网

Java easymock如何模拟/设置公共最终变量

Java easymock如何模拟/设置公共最终变量,java,unit-testing,testing,junit,easymock,Java,Unit Testing,Testing,Junit,Easymock,我有一个接一个的类来测试模拟对象 public class FordFulkerson { FlowNetwork network; Search searchMethod; public FordFulkerson (FlowNetwork network, Search method) { this.network = network; this.searchMethod = method; } public boolean compute () { b

我有一个接一个的类来测试模拟对象

public class FordFulkerson {

FlowNetwork network;
Search searchMethod;    

public FordFulkerson (FlowNetwork network, Search method) {
    this.network = network;
    this.searchMethod = method;
}

public boolean compute () {
    boolean augmented = false;
    while (searchMethod.findAugmentingPath(network.vertices)) {
        processPath(network.vertices);
        augmented = true;
    }
    return augmented;
}

protected void processPath(VertexInfo []vertices) {
    int v = network.sinkIndex;

    // Determine the amount. Goal is to find the smallest 
    int delta = Integer.MAX_VALUE;
    while (v != network.sourceIndex) {
        int u = vertices[v].previous;

        // Over a forward edge, 
        int flow;
        if (vertices[v].forward) {
            flow = network.edge(u, v).capacity - network.edge(u, v).flow;
        } else {
            flow = network.edge(v, u).flow;
        }

        if (flow < delta) { delta = flow; }

        v = u;  // follow reverse path to source
    }

    // push minimal increment over the path
    v = network.sinkIndex;
    while (v != network.sourceIndex) {
        int u = vertices[v].previous;

        if (vertices[v].forward) {
            network.edge(u, v).flow += delta;
        } else {
            network.edge(v, u).flow -= delta;
        }

        v = u;  // follow reverse path to source
    }

    Arrays.fill(network.vertices, null);   // reset for next iteration.
}
}
Test01工作正常,但在Test02中我遇到了问题。 在Test02中,需要调用processPath方法。它使用mockNetwork公共最终变量。我不知道把它们放在哪里。它导致了空异常。在上面的代码中,我试图更改此字段的可访问性并对其进行设置,但现在我在消息“sinkIndex”中出错

如何在mockNetwork中模拟公共最终变量?
我正在使用Easymock。

您说您修改了
FlowNetwork
的源代码,因此您可以执行以下操作,从而实现更稳健的设计和更容易的测试性:

  • 封装这两个变量(即
    private int-sinkIndex
    int-getSinkIndex()
  • expect(mockNetwork.getSinkIndex()).andReturn(0.anyTimes()
  • 猜测:
    mockNetwork.getClass()
    正在返回一个没有状态(即没有字段)的cglib EasyMock代理,您可以在调试器中检查它。因此
    getDeclaredField()
    返回
    null


    如果您确实想使用
    public
    s,可以尝试使用
    FlowNetwork.class.getDeclaredField

    不,我无法更改源代码。这就是问题所在。我试图在Test02中更改它们的可访问性。啊,我明白了,你刚才的意思是
    setAccessible
    ,你试过我上次的编辑吗?你也可以试着不模仿网络,然后
    f.set
    必须工作。我需要模仿网络,它的抽象。我试过你上次的编辑。现在我在Arrays.fill(network.vertices,Null)上有空指针异常;(与不改变变量相同)好的,我已经处理好了。我还需要设置与这两个变量相同的顶点。感谢您,从
    createMock
    返回的任何对象的每个字段都是
    null
    (或默认原语)。
    public class FordFulkersonMockTest {
    private FordFulkerson classUnderTest;
    private FlowNetwork mockNetwork;
    private Search mockSearch;
    
    @Before
    public void setUp() {
        mockNetwork = createMock(FlowNetwork.class);
        mockSearch = createMock(Search.class);
        classUnderTest = new FordFulkerson(mockNetwork, mockSearch );
    
    }
    
    @Test
    public void test01() {
        expect(mockSearch.findAugmentingPath(null)).andReturn(false);
    
        replay(mockSearch);
        boolean res = classUnderTest.compute();
        assertEquals(false, res);
        verify(mockSearch);
    }
    
    
    @Test
    public void test02() {
        expect(mockSearch.findAugmentingPath(null)).andReturn(true);
        try{
            Field f = mockNetwork.getClass().getDeclaredField("sinkIndex");
            f.setAccessible(true);
            f.set(mockNetwork, 0);
            f = mockNetwork.getClass().getDeclaredField("sourceIndex");
            f.setAccessible(true);
            f.set(mockNetwork, 0);
        }catch(Exception e)
        {
            fail(e.getMessage());
        }
        replay(mockNetwork);
        replay(mockSearch);
        boolean res = classUnderTest.compute();
        assertEquals(true, res);
        verify(mockSearch);
    }
    
    }