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.
When should you use the androidTest directory to store your test classes? when the tests consist only of unit tests. when the number of tests to run is large(500+).
Note: Instrumented test, also known as instrumentation tests, are initialized in a special environment that gives them access to an instance of Instrumentation. This class provides access to the application context and APIs to manipulate the app under test and gives instrumented tests their name.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With