Tests that read property file pass with eclipse but fail with Gradle


(bruceli97) #1

To avoid confusing, I create a new topic for failed gradle test. It looks like when gradle run test, it can not support external file reading. Hope Gradle team can find a solution. Thanks a lot.

Follow standard to create main, test folder.

src\main\java\com\abc\MyTry.java
  src\main\java\com\abc\my.properties
  src\test\java\com\abc\MyTryTest.java

I can run successfully Junit test for MyTryTest.java in Eclipse IDE. But when I run “gradle clean build”, it failed.

build.gradle

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'project-report'
  sourceCompatibility = 1.6
  sourceSets {
   main {
    java.srcDir "main/java"
  }
  test{
 java.srcDir "test/java"
  }
  }
   repositories {
 mavenCentral()
}
  dependencies {
 testCompile group: 'junit', name: 'junit', version: '4.8.2'
}
  eclipseClasspath {
 defaultOutputDir =
file('classes')
}

MyTry.java

package com.abc;
  import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
  public class MyTry {
   public Properties pro;
    public static void main(String[] args) {
    MyTry
one = new MyTry();
    one.good();
    one.bad();
 }
    public void bad(){
   InputStream is=MyTry.class.getResourceAsStream("my.properties");
    pro = new Properties();
      if(is==null){
           System.out.println("properties is not found");
       }
      try {
        pro.load(is);
      }
      catch (IOException ex) {
        ex.printStackTrace();
      }
      System.out.println("This is bad method, but works in eclipse, not working in gradle");
      System.out.println("property a value is :" + pro.toString());
  }
    public void good(){
  System.out.println("This is good method");
  }
}

MyTryTest.java

package com.test;
  import org.junit.Before;
import org.junit.Test;
import com.abc.MyTry;
  public class MyTryTest {
   public MyTry a;
      @Before
 public void newTestInstance() {
  a = new MyTry();
 }
    @Test
 public void badTest(){
  a.bad();
 }
   @Test
 public void goodTest(){
  a.good();
 }
}

my.properties

a=aaaa
b=bbbb

This is error message: java.lang.NullPointerException

at java.util.Properties$LineReader.readLine(Properties.java:418)

at java.util.Properties.load0(Properties.java:337)

at java.util.Properties.load(Properties.java:325)

at com.abc.MyTry.bad(MyTry.java:24)

at com.test.MyTryTest.badTest(MyTryTest.java:18)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)

at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)

at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)

at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)

at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)

at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)

at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)

at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)

at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)

at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)

at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)

at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)

at org.junit.runners.ParentRunner.run(ParentRunner.java:236)

at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:51)

at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:63)

at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:49)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)

at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)

at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:75)

at $Proxy3.processTestClass(Unknown Source)

at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:86)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)

at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)

at org.gradle.messaging.remote.internal.TypeCastDispatch.dispatch(TypeCastDispatch.java:30)

at org.gradle.messaging.remote.internal.WorkerProtocol.handleIncoming(WorkerProtocol.java:53)

at org.gradle.messaging.remote.internal.WorkerProtocol.handleIncoming(WorkerProtocol.java:31)

at org.gradle.messaging.remote.internal.ProtocolStack$ProtocolStage.handleIncoming(ProtocolStack.java:167)

at org.gradle.messaging.remote.internal.ProtocolStack$BottomStage.handleIncoming(ProtocolStack.java:277)

at org.gradle.messaging.remote.internal.ProtocolStack$BottomConnection$1.run(ProtocolStack.java:299)

at org.gradle.messaging.remote.internal.ProtocolStack$ExecuteRunnable.dispatch(ProtocolStack.java:120)

at org.gradle.messaging.remote.internal.ProtocolStack$ExecuteRunnable.dispatch(ProtocolStack.java:116)

at org.gradle.messaging.dispatch.AsyncDispatch.dispatchMessages(AsyncDispatch.java:132)

at org.gradle.messaging.dispatch.AsyncDispatch.access$000(AsyncDispatch.java:33)

at org.gradle.messaging.dispatch.AsyncDispatch$1.run(AsyncDispatch.java:72)

at org.gradle.messaging.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)

at java.lang.Thread.run(Thread.java:662)


(kellyrob) #2

It just looks like the properties file isn’t on the classpath. Try updating your sourceSets block:

main {
    java.srcDir "main/java"
   resources.srcDir "main/java"
  }

(bruceli97) #3

Awesome. It works perfect now. It seems I do not know Gradle magic. Now it will be much easier for me to work on my real project. Thanks. kellyrob.


(Luke Daley) #4

Hi Bruce,

Any non compiled resources (like property files) by convention go in src/main/resources. If you move your property file to this location you will not need to do any extra config.

This is talked about in the user guide here: http://gradle.org/current/docs/userguide/java_plugin.html#N11B33