Back to Repositories

Testing Subscriber Exception Handling Implementation in greenrobot/EventBus

This test suite validates exception handling and error management in the EventBus library, specifically focusing on subscriber exception scenarios. It ensures proper event propagation and error state handling when exceptions occur during event processing.

Test Coverage Overview

The test coverage encompasses subscriber exception handling and error propagation within EventBus.

Key areas tested include:
  • Subscriber exception event generation and propagation
  • Exception event data validation
  • Nested exception handling scenarios
  • Error state management for bad exception subscribers

Implementation Analysis

The testing approach uses JUnit to verify EventBus’s exception handling mechanisms.

Key implementation patterns include:
  • Custom EventBus builder configuration for exception logging
  • Exception event payload validation
  • Nested subscriber exception scenarios
  • Event tracking and assertion validation

Technical Details

Testing infrastructure includes:
  • JUnit test framework
  • EventBus builder API
  • Custom exception event subscribers
  • Runtime exception simulation
  • Event tracking mechanisms

Best Practices Demonstrated

The test suite demonstrates several testing best practices:

  • Isolated test scenarios with controlled configuration
  • Comprehensive exception flow validation
  • Clear test case organization
  • Proper event tracking and verification
  • Edge case handling for nested exceptions

greenrobot/eventbus

EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusSubscriberExceptionTest.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 EventBusSubscriberExceptionTest extends AbstractEventBusTest {

    @Test
    public void testSubscriberExceptionEvent() {
        eventBus = EventBus.builder().logSubscriberExceptions(false).build();
        eventBus.register(this);
        eventBus.post("Foo");
        assertEventCount(1);
        assertEquals(SubscriberExceptionEvent.class, lastEvent.getClass());
        SubscriberExceptionEvent exEvent = (SubscriberExceptionEvent) lastEvent;
        assertEquals("Foo", exEvent.causingEvent);
        assertSame(this, exEvent.causingSubscriber);
        assertEquals("Bar", exEvent.throwable.getMessage());
    }

    @Test
    public void testBadExceptionSubscriber() {
        eventBus = EventBus.builder().logSubscriberExceptions(false).build();
        eventBus.register(this);
        eventBus.register(new BadExceptionSubscriber());
        eventBus.post("Foo");
        assertEventCount(1);
    }

    @Subscribe
    public void onEvent(String event) {
        throw new RuntimeException("Bar");
    }

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

    public class BadExceptionSubscriber {
        @Subscribe
        public void onEvent(SubscriberExceptionEvent event) {
            throw new RuntimeException("Bad");
        }
    }

}