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
Support AppComponentFactory or ignore BroadcastReceivers with custom constructors #7581
Comments
Note that trying to set |
I am also facing the same issue. |
@ZacSweers @erhiteshkumar Thanks for reporting this issue. Although Robolectric can get parameter types list of constructor, there are some problems can not be processed easily:
I don't have a great idea how to fix this issue or provide potential method from Robolectric perspective, but I can share what I did before when integrating Robolectric and encountering a similar problem: using annotation processor to add empty constructor for testing code. There is an example that I wrote to try to fix a similar issue: https://github.com/utzcoz/robolectric-with-junitparams/blob/master/junitparamsprocessor/src/main/java/com/utzcoz/robolectric/junitparamsprocessor/RobolectricJUnitParamsProcessor.java#L74-L85. |
Is there any reason it couldn't just use appcomponentfactory if available and otherwise refuse to load any receivers that don't have no-arg constructors available? |
@ZacSweers I sent a PR(#7860) to skip initialization for instances that don't have empty constructor. Could you help to check whether it can fix your problem with local maven snapshot building? Thanks. |
I'm on vacation until Jan 8th but I can check again that week 👍 |
Hi @ZacSweers , I try a custom receiver with a non-empty constructor, and the
I also check
Could you provide a sample with this scenario? I can validate its behavior and check whether there are something that Robolectric can do. The previous solution doesn't look like great. If you use injection tool likes hilt, what about using these tools in Robolectric test environment too, e.g. https://dagger.dev/hilt/robolectric-testing.html? |
You should be implementing your own AppComponentFactory that can instantiate those classes and pointing your manifest's Not a broadcast receiver, but similar use with activities: https://github.com/slackhq/circuit/blob/main/samples/star/src/main/kotlin/com/slack/circuit/star/di/StarAppComponentFactory.kt |
@ZacSweers Thanks for providing this information. Looks like the ideal solution is to support |
It does not appear to be necessary any more. The receiver name can be used diretly. Related to #7581 PiperOrigin-RevId: 522107957
It does not appear to be necessary any more. The receiver name can be used diretly. Related to #7581 PiperOrigin-RevId: 522204982
@ZacSweers The fix will be released in 4.10 stable release. Please help to try it and send feedback to us when it is released. Thanks. |
Awesome! |
Description
When using Android 28+'s
AppComponentFactory
, it becomes possible to constructor-injectBroadcastReceiver
instance. However, Robolectric seems to assume that these must all have empty constructors and tries to instantiate them all at test init.I think Robolectric should ignore these or offer a way to handle them.
Steps to Reproduce
Add a custom receiver with a non-empty constructor on the classpath, then try to run any test using
RobolectricTestRunner
. Observe a trace like thisRobolectric & Android Version
4.8.2, android API 28
Link to a public git repo demonstrating the problem:
The text was updated successfully, but these errors were encountered: