mardi 1 décembre 2015

Using PowerMock with Cucumber

I've written a JUnit test that uses Mockito and PowerMock to mock some classes. I'm trying to convert it a Cucumber test, but the static PowerMock features don't work.

Extracts of the two relevant Cucumber classes:

Runner

@RunWith(Cucumber.class)
public class JWTValidatorBDDTest {
}

Steps Class

public class JWTValidatorCukeTest {
String tokenValue;
JWTValidator jwtValidator;
MockHttpServletRequest mockRequest;

@Before
public void before() throws IOException {
    this.mockRequest = new MockHttpServletRequest();
    PowerMockito.mockStatic(JWTAuthConnectionManager.class);
    BDDMockito.given(JWTAuthConnectionManager.postToken(anyString(), anyString(), anyString())).willReturn(200);
    Mockito.doReturn(200).when(JWTAuthConnectionManager.postToken(anyString(), anyString(), anyString()));
}

@Given("^a JWT token with the value (.*)")
public void a_JWT_token_with_the_value_(String token) {
    this.jwtValidator = new JWTValidator("http://ift.tt/1O1f6en", "Authorization", "Bearer");
    this.tokenValue = token;
}

Whilst this code works within the JUnit test, it fails here - it enters the JWTAuthConnectionManager.postToken() method that should be mocked and then fails by executing code within there. I've tried adding the lines:

@RunWith(PowerMockRunner.class)
@PrepareForTest(JWTAuthConnectionManager.class)

to both of the above classes (although of course I can't use RunWith in the Runner class as it already has one RunWith annotation), but this doesn't change anything.

How do I get PowerMock to work within Cucumber?

Aucun commentaire:

Enregistrer un commentaire