我有以下课程
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"
}
答案 0 :(得分: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);