Junit 我们可以用powermockito和testng模拟一个方法吗
我正在编写一个扩展atgdustcase的TestNG测试用例。我需要模拟类B中的一个方法。这个类B使用ATG中的属性文件被注入到类a中。类C是用于测试类a功能的测试类。D是扩展AtgDustCase的测试类Junit 我们可以用powermockito和testng模拟一个方法吗,junit,testng,atg,powermockito,atg-dust,Junit,Testng,Atg,Powermockito,Atg Dust,我正在编写一个扩展atgdustcase的TestNG测试用例。我需要模拟类B中的一个方法。这个类B使用ATG中的属性文件被注入到类a中。类C是用于测试类a功能的测试类。D是扩展AtgDustCase的测试类 @RunWith(PowerMockRunner.class) @PrepareForTest({A.class, B.class}) Public class c extends D{ @InjectGlobalComponent(Path for component A)
@RunWith(PowerMockRunner.class)
@PrepareForTest({A.class, B.class})
Public class c extends D{
@InjectGlobalComponent(Path for component A)
@InjectMocks
private A aClass;
public void testMethod(){
String string = "abc";
Map map = new HashMap();
map.put("a", "c");
aClass = getRequest.resolveName(Component A path);
B b = PowerMockito.mock(B.class);
A a = PowerMockito.spy(new A());
PowerMockito.whenNew(A.class).withNoArguments().thenReturn(a);
a.setB(B);
PowerMockito.when(a.getB()).thenReturn(b);
Mockito.stub(b.getMethodToMock(string)).toReturn(map);
Mockito.verify(b).getMethodToMock(string);
aClass.invokeTestMethod();// This method calls the b.getMethodToMock()
}
}
我需要模拟getMethodToMock()。执行invokeTestMethod()时,将调用getMethodToMock()。此时,应返回地图。相反,它正在执行getMethodToMock()并抛出一个错误(执行过程中的问题是我需要从DB中获取一些记录。我需要模拟此方法并返回一个包含从DB中检索到的信息的映射)。我不确定模拟是否正确发生,因为在调试模式下,我可以看到getMethodToMock()正在被调用。请帮助我如何模拟此方法并跳过此方法的执行
注意:我尝试使用Powermockito,但我的测试用例是TestNGsuite。我需要运行TestNGSuite,而不是作为Junit运行它 为什么要混合使用AtgDust和PowerMockito?您完全可以使用PowerMockito和TestNG来模拟ATG测试。这里有几个例子