Back to Repositories

Testing NoSubscriberEvent Handling in greenrobot/EventBus

This test suite validates the handling of NoSubscriberEvent scenarios in the EventBus library, ensuring proper event handling when no subscribers are present or when subscription-related errors occur.

Test Coverage Overview

The test suite provides comprehensive coverage of NoSubscriberEvent handling scenarios in EventBus.

  • Tests basic NoSubscriberEvent generation when no subscribers exist
  • Verifies event behavior after subscriber unregistration
  • Tests error handling with problematic NoSubscriberEvent subscribers
  • Validates EventBus instance and original event preservation

Implementation Analysis

The testing approach employs JUnit framework features to systematically verify EventBus’s NoSubscriberEvent functionality.

Key patterns include:
  • Subscriber registration/unregistration lifecycle testing
  • Exception handling verification using BadNoSubscriberSubscriber
  • Event tracking through AbstractEventBusTest inheritance
  • Custom event subscription using @Subscribe annotations

Technical Details

Testing infrastructure includes:

  • JUnit 4 testing framework
  • EventBus builder configuration for logging control
  • Custom subscriber classes for edge case testing
  • AbstractEventBusTest base class for common functionality
  • @Subscribe annotation for event handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for specific scenarios
  • Proper test method naming conveying purpose
  • Thorough exception handling verification
  • Clean setup and teardown through inheritance
  • Clear separation of test scenarios and subscriber implementations

greenrobot/eventbus

EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusNoSubscriberEventTest.java

            
/*
 * Copyright (C) 2012-2016 Markus Junginger, greenrobot (http://greenrobot.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.greenrobot.eventbus;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

/**
 * @author Markus Junginger, greenrobot
 */
public class EventBusNoSubscriberEventTest extends AbstractEventBusTest {

    @Test
    public void testNoSubscriberEvent() {
        eventBus.register(this);
        eventBus.post("Foo");
        assertEventCount(1);
        assertEquals(NoSubscriberEvent.class, lastEvent.getClass());
        NoSubscriberEvent noSub = (NoSubscriberEvent) lastEvent;
        assertEquals("Foo", noSub.originalEvent);
        assertSame(eventBus, noSub.eventBus);
    }

    @Test
    public void testNoSubscriberEventAfterUnregister() {
        Object subscriber = new DummySubscriber();
        eventBus.register(subscriber);
        eventBus.unregister(subscriber);
        testNoSubscriberEvent();
    }

    @Test
    public void testBadNoSubscriberSubscriber() {
        eventBus = EventBus.builder().logNoSubscriberMessages(false).build();
        eventBus.register(this);
        eventBus.register(new BadNoSubscriberSubscriber());
        eventBus.post("Foo");
        assertEventCount(2);

        assertEquals(SubscriberExceptionEvent.class, lastEvent.getClass());
        NoSubscriberEvent noSub = (NoSubscriberEvent) ((SubscriberExceptionEvent) lastEvent).causingEvent;
        assertEquals("Foo", noSub.originalEvent);
    }

    @Subscribe
    public void onEvent(NoSubscriberEvent event) {
        trackEvent(event);
    }

    @Subscribe
    public void onEvent(SubscriberExceptionEvent event) {
        trackEvent(event);
    }

    public static class DummySubscriber {
        @SuppressWarnings("unused")
        @Subscribe
        public void onEvent(String dummy) {
        }
    }

    public class BadNoSubscriberSubscriber {
        @Subscribe
        public void onEvent(NoSubscriberEvent event) {
            throw new RuntimeException("I'm bad");
        }
    }

}