fabioCollini / DaggerMock

A JUnit rule to easily override Dagger 2 objects
Apache License 2.0
1.16k stars 96 forks source link

run one class vs run all test classes #21

Closed tonilopezmr closed 8 years ago

tonilopezmr commented 8 years ago

When Run only one class the Mock is successfully but when run all test doesn't replace my mock.

This is the test code

This is the inject code

This is the component

I don't know why because i think is the same execute one test or all test. :(

Do you have any idea?

tonilopezmr commented 8 years ago

Could be for DaggerMockRule? @fabioCollini

fabioCollini commented 8 years ago

Hi Antonio, I am sorry but I can't investigate this problem until September. Are you using espresso or plain junit tests? Is it a multidex app?

tonilopezmr commented 8 years ago

Espresso and not multidex.

it's very weird, because when I run only a method or a class work's fine, but when I run a androidTest folder some mocks doesn't work.

My dagger2 configuration is fine, with Subcomponents, using latest DaggerMock and Dagger2 versions.

You can have a look my repository and run only DetailActivityTest and before run all androidTest folder.

plastiv commented 8 years ago

@tonilopezmr Since android Application is singleton, you need to reset its state for every test. Here: https://github.com/tonilopezmr/Game-of-Thrones/blob/master/app/src/main/java/es/npatarino/android/gotchallenge/GotChallengeApplication.java#L49 You are caching characterComponent instance, through effectively using Component which was created by the first test.

Instead, you need to create new instance for every test. For example, reset reference to null at @After.

tonilopezmr commented 8 years ago

@plastiv I will try, but, DaggerMockRule is reset before each test, right?

tonilopezmr commented 8 years ago

Oh good, @plastiv that's right.

I following this post to right dagger2 scope way.

plastiv commented 8 years ago

DaggerMockRule will create new instance of the component for every test method (with new mock dependency instances), but your code should not cache it. And its yours responsibility to reset it. That's why devs hate singletons ;)

tonilopezmr commented 8 years ago

@plastiv Right