PowerMock的expectNew()不是按照预期嘲笑构造函数 [英] PowerMock's expectNew() isn't mocking a constructor as expected

查看:522
本文介绍了PowerMock的expectNew()不是按照预期嘲笑构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习各种嘲笑图书馆和 PowerMock (特别是EasyMock扩展程序)的功能,是下一个列表。我试图嘲笑一个构造函数,提供的示例没有相同的响应,当我尝试复制他们。



这是测试类:



@prepareForTest({Writer.class})
public class FaultInjectionSituationTest($ Writer.class)
@RunWith(PowerMockRunner.class) {

@Test
public void testActionFail()throws Exception {
FaultInjectionSituation fis = new FaultInjectionSituation();
PowerMock.expectNew(Writer.class,test)
.andThrow(new IOException(throwwn from mock));
PowerMock.replay(Writer.class);
System.out.println(fis.action());
PowerMock.verify(Writer.class);
}

}

测试一个EasyMock.isA(String.class),但它产生了相同的结果。



这是FaultInjectionSituation:

  public class FaultInjectionSituation {

public String action(){
Writer w;
try {
w = new Writer(test);
} catch(IOException e){
System.out.println(thrown:+ e.getMessage());
return e.getLocalizedMessage();
}
return返回无throw;
}
}

Writer一个类:



public class Writer {
public Writer(String s)throws IOException {
}

public Writer()throws IOException {
}
}

当测试运行时,它打印出无返回,表示从未抛出异常。

解决方案

您需要准备调用构造函数的类,因此PowerMock知道要等待一个模拟构造函数调用。尝试使用以下代码更新代码:

  @RunWith(PowerMockRunner.class)
@PrepareForTest ,FaultInjectionSituation.class})
public class FaultInjectionSituationTest {
// as before
}


I'm trying to learn the ins and outs of various mocking libraries and PowerMock(specifically the EasyMock extension) is next on the list. I'm attempting to mock a constructor and the examples provided don't have the same response when I try to replicate them. As far as I can tell, it never mocks the constructor and just proceeds as if it were normal.

This is the test class:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Writer.class})
public class FaultInjectionSituationTest {

    @Test
    public void testActionFail() throws Exception {
        FaultInjectionSituation fis = new FaultInjectionSituation();
        PowerMock.expectNew(Writer.class, "test")
           .andThrow(new IOException("thrown from mock"));
        PowerMock.replay(Writer.class);
        System.out.println(fis.action());
        PowerMock.verify(Writer.class);
    }

}

I've tried replacing the "test" with an EasyMock.isA(String.class), but it yielded the same results.

This is the FaultInjectionSituation:

public class FaultInjectionSituation {

    public String action(){
        Writer w;
        try {
            w = new Writer("test");
        } catch (IOException e) {
            System.out.println("thrown: " + e.getMessage());
            return e.getLocalizedMessage();
        }
        return "returned without throw";
    }
}

The "Writer" is nothing more than a shell of a class:

public class Writer {
    public Writer(String s) throws IOException {
    }

    public Writer() throws IOException{
    }
}

When the test is run, it prints out "returned without throw", indicating the exception was never thrown.

解决方案

You need to prepare the class that is calling the constructor as well, so PowerMock knows to expect a mocked constructor call. Try updating your code with the following:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Writer.class, FaultInjectionSituation.class})
public class FaultInjectionSituationTest { 
 // as before
}

这篇关于PowerMock的expectNew()不是按照预期嘲笑构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆