-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't unit test classes that use EventBus #398
Comments
You are probably trying to use a local unit test. But as you found out, EventBus depends on some Android features. So you have to use an instrumented unit test that runs on an Android device instead. |
Ok, thanks! |
@gsteigert My team and I ran into this exact problem while integrating GreenRobot Eventbus. We wanted to verify that the eventbus was posting a specific event but not the execution of the posted event. You can achieve this via reflection and mockito mocks. Java: Field field = EventBus.class.getDeclaredField("defaultInstance");
if (!field.isAccessible()) field.setAccessible(true);
field.set(null, Mockito.mock(EventBus.class)); Kotlin: val field = EventBus::class.java.getDeclaredField("defaultInstance")
if (!field.isAccessible) field.isAccessible = true
field.set(null, mock<EventBus>()) |
thank @jmholtan for your answer, it works for me! |
Note that as of version |
I want to write some unit tests for classes that have their own EventBus instances, but it won't work because EventBus uses Android's Handler in its constructor. Using Otto I'm able to do it, but I want to use EventBus instead. Any ideas?
The text was updated successfully, but these errors were encountered: