2

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?

4

1 回答 1

2

我弄清楚我做错了什么。我需要改变:

@PrepareForTest(HttpClient.class)

@PrepareForTest(PowerMockitoProduction.class)
于 2015-02-23T19:28:20.503 回答