简体   繁体   中英

Powermock can't mock static class

I have a class with code similar to :

public class class1{
    private static final ConfigurationService config = Util.getInstance(ConfigurationService.class);

    private SendQueueMessages sender;

    public void start() throws LifecycleException{
        LOGGER.info("Starting");

        final ActiveMq activemq = config.getConfiguration().getActiveMq();
        sender = new SendQueueMessages(activemq.getQueueName());
    }
}

Elsewhere in the program Guice is being used to bind the configuration service and Util like so:

Util.register(new ThingICantChange(){
    @Override
    protected void configure (){
        super.configure();

        bind(ConfigurationService.class).to(ConfigurationServiceImpl.class).asEagerSingleton();
   }
});

Is this possible to unit test? I was initially trying to use JUnit 5 and mockito, but it became apparent that I needed to mock static classes/methods (IoCUtils) and switched to JUnit4 for PowerMock.

I have tried:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Util.class)
public class Class1Test{
    @Test
    public void canStart(){
        mockStatic(Util.class);
        when(Util.getInstance(ConfigurationService.class)).thenReturn(new ConfigurationService);
        Class1 class = new Class1();
        class.start();
        //etc.
    }
}

However this just gives me an error about Util not prepared for test. Changing mockStatic() to PowerMockito.spy() did get me to the when, but then throws a null pointer error.

I found a solution, though I have mixed feelings about it.

Using the Util.register (the 2nd code block) I registered a configuration service implementation that created the mock objects. This worked and let me test the start() method, but feels kind of against the idea of a unit test.

public class ConfigServiceTest implements ConfigurationService{

    @Override
    public Configuration getConfiguration() {
        Configuration conf = mock(Configuration.class);
        ActiveMq amq = mock(ActiveMq.class);

        when(amq.getQueueName()).thenReturn("test");
        when(amq.getBrokerUrl()).thenReturn("http://127.0.0.1:61616?soTimeout=1000");
        when(conf.getActiveMq()).thenReturn(amq);

        return conf;
    }
    //other methods just allowed to return null
 }

Then in the test:

Util.register(new thingICantChange(){
    @Override
    protected void configure (){
        super.configure();
        bind(ConfigurationService.class).to(ConfigServiceTest.class).asEagerSingleton();
    }
});

class1 service = new class1();

service.start();
Assert.assertEquals(true, true);

start is void and not int a new thread so Assert.assertEquals(true,true) is the best anyone around me knew to check that start ran. Mockito/PowerMock times(1) would require a mock of class1 which seems rather counter to a unit test to see IF it can run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM