使用whenNew时的Powermock问题

时间:2018-03-21 12:49:05

标签: java junit mockito powermock

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application-context.xml")
@PrepareForTest({})
public class test 
{

B b = new B();

b.Calculate("Test","Test");

//some assertions
}


public class B {

public boolean Calculate(String x, String y){

Return true

}

}

这里我试图为类B的方法计算编写JUnit。所以我创建了类B的对象并调用了calculate方法。我的问题是我想在不使用新对象的情况下测试计算方法。我尝试下面它没有工作

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application-context.xml")
@PrepareForTest({Test.class})
public class Test {

B myObjectMock  = Mockito.mock(B.class);

PowerMockito.whenNew(B.class).withAnyArguments().thenReturn(myObjectMock);

B b = new B();

b.Calculate("Test","Test");

}

我在这里做错了吗?

0 个答案:

没有答案