Reputation: 1118
I have android app project, working in Android Studio.
My application files are in the my.package
package
My unit tests are in my.package.unittest
package
And my espresso tests are in my.package.androidtest
package
In one of my espresso tests I need to use one class that I have under the unittest package, but I am not able to.
Unittest class, that I need to use is located in app/src/test/java folder:
package my.package.unittest;
public class HelperClass {
...
}
And the file I am trying to use it in is in app/src/androidTest/java folder:
package my.package.androidtest;
import static my.package.unittest.*;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class AppTest {
HelperClass.staticMethod();
}
The error I get is: cannot resolve symbol HelperClass
Additional info:
import my.package.unittest.HelperClass;
This itself gives "cannot resolve symbol" error.
What is the correct way to use this HelperClass
from my UnitTests in my Espresso tests.
Upvotes: 27
Views: 6183
Reputation: 12792
The correct answer was pointed by @Jeremy Kao.
1 -> Create a directory inside app/src. You can call it testShared.
2 -> Put your classes inside this directory.
3 -> inside app/build.gradle put:
android.sourceSets {
test {
java.srcDirs += "$projectDir/src/testShared"
}
androidTest {
java.srcDirs += "$projectDir/src/testShared"
}
}
Put this anywhere outside the android closure.
4 -> Have fun!
Resources:
http://trickyandroid.com/android-test-tricks-sharing-code-between-unit-ui-tests/
Upvotes: 44