如何使用PowerMock或Easy Mock或Mockito或全部模拟以下情况



我有以下类

    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"
}

我找到了解决方案。在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);

相关内容

  • 没有找到相关文章

最新更新