@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");
}
我在这里做错了吗?