Unit testing PowerMock调用real方法
我试图用PowerMock监视私有方法,但在定义调用私有方法时应返回的内容时,它调用了该方法,我得到了一个空指针异常。什么PowerMock在这一行调用real方法Unit testing PowerMock调用real方法,unit-testing,mockito,powermockito,Unit Testing,Mockito,Powermockito,我试图用PowerMock监视私有方法,但在定义调用私有方法时应返回的内容时,它调用了该方法,我得到了一个空指针异常。什么PowerMock在这一行调用real方法 myService= PowerMockito.spy(new MyService(myParam)); ..... PowerMockito.when(myService, "getCLientBy", anyString(), anyString(), anyString()).thenRetur`n(Client.o
myService= PowerMockito.spy(new MyService(myParam));
.....
PowerMockito.when(myService, "getCLientBy", anyString(), anyString(), anyString()).thenRetur`n(Client.of(setName, new HashSet<>())); // here it calls real method
myService=PowerMockito.spy(新myService(myParam));
.....
PowerMockito.when(myService,“getCLientBy”、anyString()、anyString()、anyString())。thenRetur`n(Client.of(setName,new HashSet());//这里它称为实方法
通过添加@PrepareForTest(MyService.class)
@RunWith(PowerMockRunner.class)
//我们为测试准备MyService,因为它是最终的
//或者我们需要模拟私有或静态方法
@PrepareForTest(MyService.class)
公共类测试用例{
//...
@试验
使用PowerMock()进行公共无效间谍活动{
MyService classUnderTest=PowerMockito.spy(新MyService(myParam));
//.....
//使用PowerMockito设置您的期望值
PowerMockito.doReturn(Client.of(setName,new HashSet()))
.when(classUnderTest,“getClientBy”,anyString(),anyString(),anyString());
//...
还要确保提供要调用的正确方法名。@user1474111和@Nkosi 我对你的例子做了一个小的模拟。 也许您还需要在PrepareForTest注释中添加客户机类
@RunWith(PowerMockRunner.class)
@PrepareForTest({ MyService.class, Client.class })
public class Example1Test {
@Test
public void testPowerMockito() throws Exception {
MyService myService = PowerMockito.spy(new MyService("myParam"));
PowerMockito.when(myService, "getClientBy", ArgumentMatchers.anyString(), ArgumentMatchers.anyString(),
ArgumentMatchers.anyString()).thenReturn(Client.of("setName", new HashSet<String>()));
myService.run();
Assert.assertEquals("setName", myService.getClient().getName());
}
}
public class MyService {
private Client client;
public MyService(String param) { }
private Client getClientBy(String a, String b, String c) {
return new Client(a + b + c);
}
public Client getClient() {
return this.client;
}
public void setClient(Client client) {
this.client = client;
}
public void run() {
setClient(getClientBy("A", "B", "C"));
}
}
public class Client {
private final String name;
public Client(String name) {
this.name = name;
}
public static Client of(String name, HashSet<String> hashSet) {
return new Client(name);
}
public String getName() {
return name;
}
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({MyService.class,Client.class})
公共类示例1测试{
@试验
public void testPowerMockito()引发异常{
MyService MyService=PowerMockito.spy(新MyService(“myParam”);
当(myService,“getClientBy”、ArgumentMatchers.anyString()、ArgumentMatchers.anyString()时,
ArgumentMatchers.anyString())。然后返回(Client.of(“setName”,new HashSet());
myService.run();
Assert.assertEquals(“setName”,myService.getClient().getName());
}
}
公共类MyService{
私人客户;
公共MyService(字符串参数){}
私有客户端getClientBy(字符串a、字符串b、字符串c){
返回新客户(a+b+c);
}
公共客户端getClient(){
将此文件退还给客户;
}
公共void setClient(客户端){
this.client=client;
}
公开募捐{
setClient(getClientBy(“A”、“B”、“C”);
}
}
公共类客户端{
私有最终字符串名;
公共客户端(字符串名称){
this.name=名称;
}
的公共静态客户端(字符串名称,哈希集){
返回新客户(名称);
}
公共字符串getName(){
返回名称;
}
}
I添加了@PrepareForTest(MyService.class)但它仍然是一样的。还检查了方法名称。@Josetepideno,这是针对我还是OP的解释?@Nkosi,这是针对你们两个的,但我在前面的评论中犯了错误,因为我指的是Mockito;然后我注意到问题是关于监视一个私有方法。对不起,我已经建立了一个小的模拟并发布了它在这个线程中。你能添加更多关于你试图测试的类和测试本身的信息吗?例如,当调用方法时,实际代码使用的参数是什么?我已经尝试添加它,但结果是一样的。实际上我认为设计也有问题,但目前无法更改它。
@RunWith(PowerMockRunner.class)
@PrepareForTest({ MyService.class, Client.class })
public class Example1Test {
@Test
public void testPowerMockito() throws Exception {
MyService myService = PowerMockito.spy(new MyService("myParam"));
PowerMockito.when(myService, "getClientBy", ArgumentMatchers.anyString(), ArgumentMatchers.anyString(),
ArgumentMatchers.anyString()).thenReturn(Client.of("setName", new HashSet<String>()));
myService.run();
Assert.assertEquals("setName", myService.getClient().getName());
}
}
public class MyService {
private Client client;
public MyService(String param) { }
private Client getClientBy(String a, String b, String c) {
return new Client(a + b + c);
}
public Client getClient() {
return this.client;
}
public void setClient(Client client) {
this.client = client;
}
public void run() {
setClient(getClientBy("A", "B", "C"));
}
}
public class Client {
private final String name;
public Client(String name) {
this.name = name;
}
public static Client of(String name, HashSet<String> hashSet) {
return new Client(name);
}
public String getName() {
return name;
}
}