keep mock data accessible only to test and androidTest
Asked Answered
H

1

1

I have mock data that I want to use for both unit and instrumented test. To avoid repetition, I created a common directory testHelper and placed my mock data there. In app/build.gradle I did:

sourceSets {
    androidTest {
        java.srcDirs += 'testHelper'
    }

    test {
        java.srcDirs += 'testHelper'
    }
}

Now i can import my mock data from unit and UI tests but I can also access them from the main app. I want to avoid doing that. I tried a few tips such as adding src/ to the above but it made no difference.

basically, I don't want my main code to access the mock data

import AppMockData // ==> this shouldn't work. currently it does.

@Composable
fun HelloWorld(){}
Hoehne answered 8/3 at 9:26 Comment(0)
P
0

You have to define a new library module (let's call it "common") where you can define all the mock data you need.

The module can be easily created with Android Studio by selecting New Module -> Android Library and filling out all the data like module name, package name etc.

The mocked data will be located under common/src/main/java folder (under your own package name).

Finally, you have to link this new module as dependency in the main app's build.gradle file:

dependencies {
    ...
    testImplementation project(':common')
    androidTestImplementation project(':common')
}

In this way, your mocked data won't be accessible from your app code.

Hope this helps

Predacious answered 9/3 at 12:44 Comment(1)
thanks Lino. So where would I put this mock data? Can you give me a few more details please.Hoehne

© 2022 - 2024 — McMap. All rights reserved.