You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi! Thanks for a great library, this is going to make my testing much, much easier 😃
I am trying to test a class which has its dependencies configured by Dagger by calling an inject() method on the Dagger component and passing itself in. Does DaggerMock support mocking of dependencies when a class is initialised like this? If so, how should I do this?
In case it isn't clear what I'm asking, here are some relevant code excerpts from my project to illustrate my configuration. This is my first use of Dagger so it's entirely possible I may be misusing it, leading to my current problems.
I want to use DaggerMock to provide the values for @Named("mealsTableName") and @Named("awsRegion") because these are defined in InfrastructureModule.java by referencing system variables which of course aren't set in unit tests:
The above configuration gives me a NullPointerException when calling the handler on this line:
final ApiGatewayResponse response = handler.handleRequest(request, context);
How should I set up Dagger/DaggerMock to support what I want to do?
Thanks again for this library and in advance for any help you can offer 😀
Edit
I have just switched to using JUnit 4 instead of 5 to run the tests and I'm getting a different exception from Dagger code, a NullPointerException from the code below:
Hi @stuartleylandcole ,
I have never used DaggerMock with JUnit 5, I'll try to use it soon. Thanks for the report!
Based on your example I think that the problem is that you create the component and inject in the ListMealsHandler constructor. So even in the test you create the real component and use it to inject your object. Then you'll inject it again with the DaggerMock component but it's too late, you have already got the error.
I think you can use something similar to what we do in Android, you can store the component in an external singleton. In ListMealsHandler constructor you can retrieve the component from this singleton and inject the object. From the test you manually replace the real component with the DaggerMock component (you need to pay attention and avoid the real component creation if it's already been set by the test).
Hope this helps you, let me know if it works in this way
Hi! Thanks for a great library, this is going to make my testing much, much easier 😃
I am trying to test a class which has its dependencies configured by Dagger by calling an
inject()
method on the Dagger component and passing itself in. Does DaggerMock support mocking of dependencies when a class is initialised like this? If so, how should I do this?In case it isn't clear what I'm asking, here are some relevant code excerpts from my project to illustrate my configuration. This is my first use of Dagger so it's entirely possible I may be misusing it, leading to my current problems.
ListMealsHandler.java
- class under testAppComponent.java
- Dagger configurationListMealsHandlerTest.java
- unit test for handlerI want to use DaggerMock to provide the values for
@Named("mealsTableName")
and@Named("awsRegion")
because these are defined inInfrastructureModule.java
by referencing system variables which of course aren't set in unit tests:InfrastructureModule.java
The above configuration gives me a
NullPointerException
when calling the handler on this line:How should I set up Dagger/DaggerMock to support what I want to do?
Thanks again for this library and in advance for any help you can offer 😀
Edit
I have just switched to using JUnit 4 instead of 5 to run the tests and I'm getting a different exception from Dagger code, a
NullPointerException
from the code below:ListMealsHandler_MembersInjector.java
From this I can maybe learn two things:
Any help you can provide on the above would be greatly appreciated!
The text was updated successfully, but these errors were encountered: