java - easymock 如何模拟/设置公共(public)最终变量

标签 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 () {
    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.
}
}

我的测试:

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);
}

}

Test01 工作正常,但在 Test02 中我遇到了问题。 在 Test02 中,需要调用方法 processPath。它使用 mockNetwork 公共(public)最终变量。我不知道在哪里设置它们。它导致空异常。在上面的代码中,我尝试更改此字段的可访问性并设置它们,但现在我有消息“sinkIndex”的错误。

如何在 mockNetwork 中模拟 public final 变量? 我正在使用 Easymock。

最佳答案

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

  1. 封装这两个变量(即 private int sinkIndexint getSinkIndex())
  2. expect(mockNetwork.getSinkIndex()).andReturn(0).anyTimes();

猜测:mockNetwork.getClass() 正在返回一个没有状态(即没有字段)的 cglib EasyMock 代理,您可以在调试器中检查它。因此 getDeclaredField() 返回 null

如果你真的想使用public,你可以试试FlowNetwork.class.getDeclaredField

关于java - easymock 如何模拟/设置公共(public)最终变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20937055/

相关文章:

java - 可重入读写锁同步上是 Write Locked

java - 在 Maven/Surefire 中,无论单元测试是否通过,清理后的清理工作有什么好方法?

testing - 如何使用 ColdFusion 进行 A/B 测试?

c# - Winforms 测试应用指南

angularjs - 如何在指令中对 $emit 进行单元测试?

java - 在 while 循环中验证 isDisplayed() WebElement 属性

java - java代码中在哪里插入try/catch

java - JSP:如何通过按下按钮来更新表

java - 如何在 ORMLITE android On Upgrade 方法中添加主键?

unit-testing - 如何使用 Gradle 在 grails-core 项目中运行单个测试?