[java]代码库
package cn.outofmemory.junit.jmockit;
/**
* 测试private method mock
*/
public class MockTarget{
public Object privateMethod(String arg) {
return "Hello Obkkject " + arg;
}
public String publicMethod() {
return (String)this.privateMethod("abc");
}
}
//源代码片段来自云代码http://yuncode.net
package cn.outofmemory.junit.jmockit;
import mockit.NonStrictExpectations;
import mockit.Injectable;
import mockit.MockUp;
import mockit.Mocked;
import mockit.Mockit;
import mockit.NonStrict;
import mockit.Tested;
public class MockTargetTest {
@BeforeClass
static public void beforeClass() {
Mockit.setUpMocks();
}
@Mocked
private MockTarget mt;
@Test
public void testPrivateMethodMock() {
new NonStrictExpectations(mt) {
{
// 对私有方法进行mock
this.invoke(mt, "privateMethod", anyString);
result = "Hello Object";
}
};
Object actual = mt.publicMethod();
assertEquals(actual,"Hello Object");
}
}
//源代码片段来自云代码http://yuncode.net