Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerMock's expectNew() isn't mocking a constructor as expected

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.

like image 280
AdamSpurgin Avatar asked Sep 10 '12 22:09

AdamSpurgin


2 Answers

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
}
like image 169
rrufai Avatar answered Sep 22 '22 23:09

rrufai


You need to first create a mock object:

Writer mockWriter = PowerMock.createMock(Writer.class)
PowerMock.expectNew(Writer.class, "test").andReturn(mockWriter)
like image 29
fo_x86 Avatar answered Sep 26 '22 23:09

fo_x86