Back to Repositories

Testing EventBus Inheritance and Subclass Event Handling in greenrobot/EventBus

This test suite examines inheritance behavior in EventBus by extending the EventBusInheritanceTest class. It specifically tests event subscription and handling in subclasses, focusing on method overriding patterns and event class hierarchies.

Test Coverage Overview

The test suite validates EventBus inheritance mechanisms and event handling in subclass contexts.

Key areas covered include:
  • Event subscription method overriding
  • Event handling in inherited contexts
  • Counter verification for overwritten event handlers
  • Event class hierarchy preservation

Implementation Analysis

The testing approach leverages JUnit framework to verify EventBus’s inheritance capabilities. The implementation uses method overriding with the @Subscribe annotation to test event handler inheritance patterns. The test class extends EventBusInheritanceTest and overrides specific methods to validate subclass behavior.

Technical patterns include:
  • Subscription method overriding
  • Event counter tracking
  • Selective test ignoring using @Ignore

Technical Details

Testing tools and configuration:
  • JUnit testing framework
  • EventBus annotation processing (@Subscribe)
  • Android test runner compatibility considerations
  • Inheritance-based test structure
  • Event counting mechanism for verification

Best Practices Demonstrated

The test implementation showcases several testing best practices in the EventBus context. It demonstrates proper inheritance testing patterns, careful handling of overridden methods, and appropriate use of test annotations.

Notable practices include:
  • Clear method overriding documentation
  • Explicit event counting
  • Proper test isolation
  • Strategic use of @Ignore for incomplete tests

greenrobot/eventbus

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

            
package org.greenrobot.eventbus;

import org.junit.Ignore;

// Need to use upper class or Android test runner does not pick it up
public class EventBusInheritanceSubclassTest extends EventBusInheritanceTest {
    int countMyEventOverwritten;

    @Subscribe
    public void onEvent(MyEvent event) {
        countMyEventOverwritten++;
    }

    @Override
    @Ignore
    public void testEventClassHierarchy() {
        // TODO fix test in super, then remove this
    }
}