0
votes

I'm trying to create unit tests for my Spring EventListeners that toggle SimpleMessageListenerContainer (start/stop).

The code I'm trying to test:

@Configuration
public class SpringEventsListeners {
    private static CPLocation cpLocation = CPLoggingFactory.createCPLocation(SpringEventsListeners.class);

    private ConfigurationServiceImpl configService;

    //Constructor Injection for Testability.
    public SpringEventsListeners(final ConfigurationServiceImpl configService) {
        this.configService = configService;
    }

    /**
     * Rabbit listeners might be started/stopped after a refresh depending on a property.
     * In case a change is needed, we perform it.
     */
    @EventListener(classes= { ApplicationReadyEvent.class, EnvironmentChangeEvent.class })
    public void handleRabbitListenerToggle() {
        String method = "handleRabbitListenerToggle";
        cpLocation.info(method, "Received refresh event. Determining action.");
        
        boolean shouldActivateRabbit = configService.shouldActivateRabbitListeners();
        for (SimpleMessageListenerContainer container : configService.getAllRabbitSimpleMessageListenerContainers()) {
            if (shouldActivateRabbit && !container.isActive()) {
                container.start();
                cpLocation.info(method, "Starting container: "+Arrays.toString(container.getQueueNames()));
            }
            else if (!shouldActivateRabbit && container.isActive()) {
                container.shutdown();
                cpLocation.info(method, "Shutting down container: "+Arrays.toString(container.getQueueNames()));
            }
        }
    }
    
}

Minimal test (tests nothing):

@RunWith(MockitoJUnitRunner.class)
public class SpringEventsListenersTests {

    @InjectMocks
    private SpringEventsListeners classUnderTest;

    @Mock
    private ConfigurationServiceImpl mockConfigurationService;

    @Mock
    private SimpleMessageListenerContainer mockSimpleMessageListenerContainer;

    @Test
    public void testToggleTrue() {
        //Given
        given(mockConfigurationService.shouldActivateRabbitListeners()).willReturn(true);
        given(mockConfigurationService.getAllRabbitSimpleMessageListenerContainers()).willReturn(Arrays.asList(mockSimpleMessageListenerContainer));
        given(mockSimpleMessageListenerContainer.isActive()).willReturn(false);
        //When
        classUnderTest.handleRabbitListenerToggle();
        Assert.assertThat(true, is(true));
    }
}

The tests won't run as I receive a NullPointerException on given(mockSimpleMessageListenerContainer.isActive()).willReturn(false);:

java.lang.NullPointerException at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.isActive(AbstractMessageListenerContainer.java:1271) at com.cp.order.listener.SpringEventsListenersTests.testToggleTrue(SpringEventsListenersTests.java:36) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:79) at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:85) at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39) at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

I will also need to assert that .shutdown or .start were called on the container, so it needs to be mocked.

1

1 Answers

0
votes

Mockito can't mock final methods (isActive() is final).

You could use reflection (e.g. DirectFieldAccessor) to check the active field instead.

That said, mocking the listener container is rather unusual; it is a complex piece of code; perhaps you could explain exactly what you are trying to test?