Test task unable to locate the resource under target as target is not getting generatedtill build successful. So gradle build getting failed

Test task is failing. As its unable to find the resource file required (I am trying to load the resource file using ClasspathStoryFinder.class.getClassLoader().getResources("") under target folder. Target is not generated until build is success.

Note: When i ran test testcase using junit it works properly as it can able to find the resource file from the class path.

Can you please help me to fix this issue.

So is the resource you are trying to load in your test generated (or copied) by some other Gradle build step? If so, then you’ll probably have to have the ‘test’ task depend on that step to ensure the resources exist before Gradle attempts to execute your tests.

Sorry to bug you again. I am new to gradle setup. Target will not be generated until build is successful. But build is failing because of :test task is failing.

Below is overview of my scenario.

I want to test jbehave testcase. Which require *.story file under src/test/resources test classes are available @ src/test

below is build.gradle

defaultTasks ‘clean’, ‘build’, ‘test’

apply plugin: ‘java’ apply plugin: ‘idea’ apply plugin: ‘eclipse’

version = ‘1.0.0’ group = ‘com.test.Test’

task wrapper(type: Wrapper){

description = ‘Generates gradlew scripts for NIX and win envs’

gradleVersion = ‘2.0’ }

repositories {

jcenter()

mavenCentral() }

idea.module {

downloadSources = true }

dependencies {

compile ‘org.slf4j:slf4j-api:1.7.7’,

‘commons-lang:commons-lang:2.6’

runtime ‘org.slf4j:slf4j-log4j12:1.7.7’,

‘log4j:log4j:1.2.17’

testCompile ‘junit:junit:4.11’,

‘org.mockito:mockito-all:1.9.5’,

‘commons-io:commons-io:2.4’,

‘org.jbehave:jbehave-core:3.8’ }

Now how i need to add the dependency on :test task.

I’m not sure what you mean when you say that “target” won’t be built unless the build is successful. The ‘test’ task should depend on ‘compileTestJava’ which is responsible for copying all of your classpath resources, including those in src/test/resources.

Assuming a file that exists src/test/resources/foo.story, you should be able to access that resource from a test class via the syntax in your original post, for example:

this.getClass().getClassLoader().getResources(“foo.story”);

Simply, any part of your test source set will be automatically placed on your testing classpath.

Target means, target folder which will be generated by gradle build.

debug statement from gradle build…

Searching for stories called teststory in [C:\opt\OpsIT-berlin-clock-b773c806fcc9\target\classes\test, C:\opt\OpsIT-berlin-clock-b773c806fcc9\target\resources\test, C:\opt\OpsIT-berlin-clock-b773c806fcc9\target\classes\main]

but by the time it’s checking, resources under target is not available.

Thanks

Just to confirm, are you changing the default build directory to target? By default, Gradle puts build output in a folder called build.