koral-- / gradle-pitest-plugin

Gradle plugin for PIT Mutation Testing in Android projects
Apache License 2.0
74 stars 9 forks source link

Pitest + mock context #24

Closed BulatMukhutdinov closed 6 years ago

BulatMukhutdinov commented 6 years ago

Hi! I am injecting Context through Dagger 2. On testing I am overriding component with providing context like this @Provides @Singleton Context provideContext() { Context contextMock = mock(Context.class); when(contextMock.getString(R.string.api_base_url)).thenReturn("http://192.168.0.1:5632/api/v0_0_8/"); return contextMock; }

When running JUnit tests it works well - when calling context.getString() it returns what needed and test suite is green.

But when run pitest it says that INFO : MINION : Exception: Stub! at android.content.Context.getString(Context.java:30) at jp.co.soramitsu.yuna.domain.injection.TestModule.provideContext(TestModule.java:83)

Please help to properly mock Context for pitest Thanks!

koral-- commented 6 years ago

To solve that you can enable returnDefaultValues, like this:

android {
    testOptions {
        unitTests.returnDefaultValues = true
    }
}
BulatMukhutdinov commented 6 years ago

Of course first of all I tried this solution but it doesn't work. Have you another idea how to fix it?

koral-- commented 6 years ago

Could you share a project with minimal reproducible sample?

BulatMukhutdinov commented 6 years ago

https://github.com/BulatMukhutdinov/PitestExample

I created minimal structure to reproduce error

koral-- commented 6 years ago

OK, I'll check that ASAP.

koral-- commented 6 years ago

Workaround is to add mock-maker-inline: https://github.com/koral--/PitestExample/commit/6ffdc0e081f278c46929a90581a83f2ef86d47cf

BulatMukhutdinov commented 6 years ago

Now there is no previous exception and it even generates pitest report, but now I caught this app/src/main/java/bulat/example/pitestexample/Pitest.java:16: error: cannot find symbol return context.getString(R.string.api_base_url); ^ symbol: variable api_base_url location: class string 1 error

Did you have this error?

koral-- commented 6 years ago

cannot find symbol return context.getString(R.string.api_base_url)

That's because you defined resValue "string", "api_base_url", "http://192.168.0.1:8080" inside debug block only. https://github.com/BulatMukhutdinov/PitestExample/blob/master/app/build.gradle#L37 So pitestRelease or pitest task will fail due to this error but pitestDebug will work.

I'll keep this issue open until some better solution to the 1st issue is found.