27

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.

Jeni
  • 1,088
  • 12
  • 30
  • 5
    http://blog.danlew.net/2015/11/02/sharing-code-between-unit-tests-and-instrumentation-tests-on-android/ – IuriiO Sep 15 '16 at 20:00
  • Thx for your comment! Unfortunately I was not able to make it work as described in your blog, although it gave me very good direction. Finally I made it work using: `sourceSets { androidTest { java.srcDirs = ['src/androidTest/java', 'src/test/java'] } }`, which I took from skoush's comment from this [question](http://stackoverflow.com/questions/29243787/sharing-resources-across-unit-tests-and-instrumentation-tests-in-android). Your approach seems more neat, but as I am not good with gradle, I was not able to find out why it didn't work. – Jeni Sep 16 '16 at 12:42
  • well, how about this one: http://trickyandroid.com/android-test-tricks-sharing-code-between-unit-ui-tests/ – IuriiO Sep 19 '16 at 01:31
  • 2
    [Android Test tricks - Sharing code between UI & unit tests](http://trickyandroid.com/android-test-tricks-sharing-code-between-unit-ui-tests/) – Jeremy Kao Apr 23 '17 at 11:06

1 Answers1

44

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/

Leandro Borges Ferreira
  • 12,422
  • 10
  • 53
  • 73
  • Instead of `java.srcDirs` I did `main.java.srcDirs`, otherwise it wasn't working. – azizbekian Feb 07 '20 at 11:13
  • 5
    Both `java.srcDirs` and `main.java.srcDirs` worked for me. Also, the closure does not need to be put outside of the `android` closure. It can be put inside it, but you just need to change `android.sourceSets` to just `sourceSets` – Martin Melka Apr 17 '20 at 22:16
  • 1
    indeed, your observation is very correct @MartinMelka. Thanks! – Leandro Borges Ferreira Apr 20 '20 at 16:59
  • any idea how to prevent Android Studio from removing the "mark as test sources" marker? really annoying to re-mark after a gradle sync.. – Andre Thiele Aug 18 '21 at 09:51
  • The resources link does not work, could you update it? – MeLean Nov 19 '21 at 10:57
  • 1
    This no longer works as of AndroidStudio 2021.2.1, as IntelliJ does not support the same file being in two different roots. It seems to be an old problem for IntelliJ, so maybe its just something that got reintroduced and will be fixed in a patch. https://youtrack.jetbrains.com/issue/IDEA-210311#focus=streamItem-27-3407352.0-0 – arberg May 13 '22 at 09:46
  • Anyone who stumbled upon this post and may get the warning "Duplicating content roots detected" might have a look at https://stackoverflow.com/questions/72358843/sharedtest-got-warning-duplicate-content-root-detected-on-android-studio-chipm – Aaron Jun 24 '22 at 17:33