0

我有以下课程

    public class TestClass{
    ParamObject paramObject = null;

    public TestClass(ParamObject paramObject){
        this.paramObject = paramObject;
    }

    public String getQuery(String field){

        String sql = field + "sometext" + getName();
    }

    private String getName(){
        SomeObject obj = new SomeObject();
        return obj.getName();
    }

}

我想模拟类的上述 getName 方法。构造函数参数在 Test 类中可用。我该怎么做呢。

@Test(groups = { "UNIT", "CS_RAJAN" })
public void TestClass_Unit_Test() throws Exception {
    ParamObject paramObject = get();//some param object

    //some way to pass the paramObject to the class and mock the getName() method so that it returns "ABC"

}
4

1 回答 1

0

我找到了解决方案。在 testClass 中是这样的

ParamObject paramObject = get();//Some instance
TestClass t = PowerMock.createPartialMock(
        TestClass.class, "getName", null, paramObject);

String expected = "ABC";

PowerMock.expectPrivate(t, "getName").andReturn(
        expected);
PowerMock.replay(t);
于 2013-07-03T08:06:35.213 回答