Back to Repositories

Testing EventBusBuilder Configuration and Error Handling in greenrobot/EventBus

This test suite evaluates the EventBusBuilder configuration and error handling capabilities in the greenrobot EventBus library. It verifies various builder patterns for customizing event bus behavior, exception handling, and event inheritance features.

Test Coverage Overview

The test suite provides comprehensive coverage of EventBusBuilder functionality including:
  • Exception handling and propagation
  • Subscriber exception event management
  • No-subscriber event handling
  • Default EventBus installation
  • Event inheritance configuration
Tests verify both positive and negative scenarios with explicit validation of expected behaviors.

Implementation Analysis

The testing approach utilizes JUnit framework features for systematic verification of EventBus builder configurations. Each test case isolates specific builder parameters and validates their effects through controlled event publishing and subscription patterns.

The implementation leverages custom event trackers and throwing subscribers to simulate real-world usage scenarios and error conditions.

Technical Details

Testing Infrastructure:
  • JUnit 4 testing framework
  • Custom AbstractEventBusTest base class
  • Event tracking mechanisms
  • Specialized subscriber implementations
  • Runtime exception validation

Best Practices Demonstrated

The test suite exemplifies strong testing practices through:
  • Isolated test cases for each builder configuration
  • Explicit exception handling verification
  • Clear test method naming conventions
  • Proper test inheritance structure
  • Comprehensive edge case coverage

greenrobot/eventbus

EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusBuilderTest.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.Assert;
import org.junit.Test;

import static org.junit.Assert.fail;

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

    @Test
    public void testThrowSubscriberException() {
        eventBus = EventBus.builder().throwSubscriberException(true).build();
        eventBus.register(new SubscriberExceptionEventTracker());
        eventBus.register(new ThrowingSubscriber());
        try {
            eventBus.post("Foo");
            fail("Should have thrown");
        } catch (EventBusException e) {
            // Expected
        }
    }

    @Test
    public void testDoNotSendSubscriberExceptionEvent() {
        eventBus = EventBus.builder().logSubscriberExceptions(false).sendSubscriberExceptionEvent(false).build();
        eventBus.register(new SubscriberExceptionEventTracker());
        eventBus.register(new ThrowingSubscriber());
        eventBus.post("Foo");
        assertEventCount(0);
    }

    @Test
    public void testDoNotSendNoSubscriberEvent() {
        eventBus = EventBus.builder().logNoSubscriberMessages(false).sendNoSubscriberEvent(false).build();
        eventBus.register(new NoSubscriberEventTracker());
        eventBus.post("Foo");
        assertEventCount(0);
    }

    @Test
    public void testInstallDefaultEventBus() {
        EventBusBuilder builder = EventBus.builder();
        try {
            // Either this should throw when another unit test got the default event bus...
            eventBus = builder.installDefaultEventBus();
            Assert.assertEquals(eventBus, EventBus.getDefault());

            // ...or this should throw
            eventBus = builder.installDefaultEventBus();
            fail("Should have thrown");
        } catch (EventBusException e) {
            // Expected
        }
    }

    @Test
    public void testEventInheritance() {
        eventBus = EventBus.builder().eventInheritance(false).build();
        eventBus.register(new ThrowingSubscriber());
        eventBus.post("Foo");
    }

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

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

    public class ThrowingSubscriber {
        @Subscribe
        public void onEvent(Object event) {
            throw new RuntimeException();
        }
    }

}