user1883212
user1883212

Reputation: 7859

Whennew() call in powermockito not working

I'm using Mockito and PowerMockito to instantiate a mock when a constructor is called:

@RunWith(MockitoJUnitRunner.class)
@PrepareForTest(ConVibe.class)
public class ConVibeTests {

    ConVibe task;

    @Mock ShapeEffect shapeEffect;



    @Test
    public void verify_shape_effect() {

        whenNew(ShapeEffect.class).withAnyArguments().thenReturn(shapeEffect);
        task.call();

        // Omitted
    }

    // Omitted

}

This is the call to the constructor that I wanted to mock, located inside the function call() in the class conVibe:

final ShapeEffect effect = new ShapeEffect(mode, new RepService());

The fact is that the real constructor is called (where there is a DB call that obviously fail) instead of creating a mock.

What's wrong?

Upvotes: 1

Views: 668

Answers (1)

Mureinik
Mureinik

Reputation: 311228

You're using the wrong runner - if you want to use PowerMock, you need to use the PowerMockRunner:

@RunWith(PowerMockRunner.class)
@PrepareForTest(ConVibe.class)
public class ConVibeTests {

Upvotes: 1

Related Questions