0

I'm taking over a project and have to assume that the tests worked at some point the way I find them (boiled down to what should not be null):

@Test
public void testCoding() {
   assertNotNull(getClass().getResourceAsStream("/myfile.json"));
   //assertNotNull(MyTest.class.getResourceAsStream("/myfile.json"));
   //assertNotNull(MyTest.class.getClassLoader().getResourceAsStream("myfile.json"));
   //...
}

with myfile.json in src/test/resources/ and the test in src/test/java/some/package/.

I tried putting myfile.json into the same folder as the test, in the src/java folder, I tried with and without leading / and with /resources and all the tips I found on SO.

The gradle file is:

    apply plugin: 'java'

    repositories {
        mavenCentral()
    }

    dependencies {
        testCompile "junit:junit:$junitVersion"
    }

What am I missing?


Update:

I realized that the source code is in the open source part of our project and that it does fail in AS, only.

Here is the line that fails if I right-click the lt-api/src/test/java/ folder and select "Run All Tests". The Gradle console prints out this line:

    Executing tasks: [:lt-api:compileJava, :lt-api:testClasses, :mbwlib:compileJava, :mbwlib:testClasses, :bitlib:compileJava, :bitlib:testClasses]

Running ./gradlew clean :lt-api:compileJava :lt-api:testClasses :mbwlib:compileJava :mbwlib:testClasses :bitlib:compileJava :bitlib:testClasses

does not trigger the error but neither does it run tests. (How can I know what exactly AS is doing there? I thought AS was doing gradle all the way now?)

Running

    ./gradlew clean :lt-api:test

I get the tests to run (adding a typo results in failing tests) but it doesn't trigger the issue I originally had and that I'd like to understand.

Where should the file be?

$ sudo updatedb
$ locate test-classes
$ locate ungargasse.json
/path/to/project/lt-api/build/resources/test/ungargasse.json
/path/to/project/lt-api/src/test/resources/ungargasse.json
Giszmo
  • 1,944
  • 2
  • 20
  • 47
  • The acid test is where is it when you execute it? Does it get copied into the head of the class hierarchy? It seems to me the code should say `"/resources/myfile.json"`. – user207421 Jun 09 '17 at 03:53
  • refer it it may help https://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream – Zia Jun 09 '17 at 04:26
  • It depends on how the project is build: is the directory src/test/resources/ in the classpath? or is its content copied in the classpath? – Maurice Perry Jun 09 '17 at 05:45
  • Updated the question. Sorry for slow response with kind of a change of issue towards AndroidStudio vs. Gradlew. – Giszmo Jun 12 '17 at 21:56

1 Answers1

2

Be Sure that myfile.json is in the test-classe after you complied the codes.

(By default,the files in src/test/resources/ will automatically move into test-classes after compling) enter image description here

You can use getResourceAsStream without leading "/" in two cases:

  1. Test.java is in src/test/java/some/package and myfile.json is in /test/resource

    src/test/java/some/package/Test.java
    src/test/resource/myfile.json
    
  2. Test.java and myfile.json are in the same package

    src/test/java/some/package/Test.java
    src/test/java/some/package/myfile.json
    

For example

@org.junit.Test
    public void testCoding() {
        InputStream resourceAsStream = getClass().getResourceAsStream("myfile.json");
        System.out.println(resourceAsStream);

    }
user207421
  • 305,947
  • 44
  • 307
  • 483
lihongxu
  • 754
  • 5
  • 14
  • Thank you for your answer. I believe there is a typo. /test/resources/, not resource/. I updated my question. Essentially I get the issue in Android Studio but not on the console using gradlew. – Giszmo Jun 12 '17 at 21:15