lundi 23 février 2015

Why isn't PowerMockito mocking this class properly?

I am using PowerMockito and this is my test:



import com.PowerMockitoProduction;
import org.apache.commons.httpclient.HttpClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;


@RunWith(PowerMockRunner.class)
@PrepareForTest(HttpClient.class)
public class PowerMockitoTest {

@Test(expected = UnsupportedOperationException.class)
public void test() throws Exception {

PowerMockito.whenNew(HttpClient.class).withNoArguments().thenThrow(new UnsupportedOperationException());

new PowerMockitoProduction().createClient();

}
}


This test is failing.



java.lang.AssertionError: Expected exception: java.lang.UnsupportedOperationException


Here's what PowerMockitoProduction does:



package com;

import org.apache.commons.httpclient.HttpClient;

public class PowerMockitoProduction {

public void createClient() {
HttpClient client = new HttpClient();
System.out.println(client);
}

}


I expect this code to create a mock HttpClient based on this line in my test:



PowerMockito.whenNew(HttpClient.class).withNoArguments().thenThrow(new UnsupportedOperationException());


But it doesn't seem to be effecting my production code. What am I doing wrong?


Aucun commentaire:

Enregistrer un commentaire