0
我使用了PowerMock来模拟Constructor.Afer启动应用程序,我认为所有行都应该是绿色的。但实际上所有行都是红色的。
我认为模仿构造函数会导致这种现象。因为嘲笑其他人,比如最终类,确定。如何解决这个问题?使用PowerMock模拟构造函数时JaCoCo/EclEmma的源高亮函数不起作用
//code:
public class People {
public String sayHello(){
return "hello";
}
}
public class Family {
public String doEvent() {
People p = new People();
String str = p.sayHello();
System.out.println(str);
return str;
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest(Family.class)
public class FamilyTest {
@Test
public void test() throws Exception {
Family f = new Family();
String str = "hello mock";
People p = PowerMock.createMock(People.class);
PowerMock.expectNew(People.class).andReturn(p);
EasyMock.expect(p.sayHello()).andReturn(str);
PowerMock.replay(p, People.class);
String strActual = f.doEvent();
Assert.assertEquals(str, strActual);
PowerMock.verify(p, People.class);
}
}
我'嘲笑人类的构造函数,所以有必要在类级使用'@ PrepareForTest'。你可以看到[MockConstructor](https://github.com/jayway/powermock/wiki/MockConstructor) – niaomingjian
但你并没有嘲笑家庭课...... – Lencalot
虽然我没有嘲笑家庭课,但我认为它需要基于[MockConstructor](https://github.com/jayway/powermock/wiki/MockConstructor)文件。在本文中,它说**我们没有准备File.class的原因是,一个新的在执行expect之前永远不会创建File实例newNew,PowerMock只是拦截来自新的类的新的调用,因此您需要在此示例中准备PersistenceManager类**我正在嘲笑在类中使用的People类family class,所以我在类级别添加了'@PrepareForTest(Family.class)'。 – niaomingjian