Gradle not running tests - java

For some reason gradle is not running my tests. When i execute gradle cleanTest test -i i get:
Skipping task ':compileJava' as it is up-to-date (took 0.262 secs).
:compileJava UP-TO-DATE
:compileJava (Thread[main,5,main]) completed. Took 0.266 secs.
:processResources (Thread[main,5,main]) started.
:processResources
Skipping task ':processResources' as it has no source files.
:processResources UP-TO-DATE
:processResources (Thread[main,5,main]) completed. Took 0.001 secs.
:classes (Thread[main,5,main]) started.
:classes
Skipping task ':classes' as it has no actions.
:classes UP-TO-DATE
:classes (Thread[main,5,main]) completed. Took 0.0 secs.
:compileTestJava (Thread[main,5,main]) started.
:compileTestJava
Skipping task ':compileTestJava' as it has no source files.
:compileTestJava UP-TO-DATE
:compileTestJava (Thread[main,5,main]) completed. Took 0.001 secs.
:processTestResources (Thread[main,5,main]) started.
:processTestResources
Skipping task ':processTestResources' as it is up-to-date (took 0.004 secs).
:processTestResources UP-TO-DATE
:processTestResources (Thread[main,5,main]) completed. Took 0.007 secs.
:testClasses (Thread[main,5,main]) started.
:testClasses
Skipping task ':testClasses' as it has no actions.
:testClasses UP-TO-DATE
:testClasses (Thread[main,5,main]) completed. Took 0.001 secs.
:test (Thread[main,5,main]) started.
:test
file or directory '/Users/jan/2014-2015-groep-05/VoPro/build/classes/test', not found
Skipping task ':test' as it has no source files.
My test are in the folder ./test/. And this is my gradle config:
apply plugin: 'java'
apply plugin: 'eclipse'
test {
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
}
dependsOn 'cleanTest'
}
repositories {
mavenCentral()
}
dependencies {
testCompile("junit:junit")
}
sourceSets {
main {
java {
srcDir 'src'
srcDir 'test'
}
}
}
I can't seem to find what the problem is. Gradle does not recognize any tests, since both test and cleanTest are always up to date, however i did add test/ to my sourceSets.

A note for people using Junit5, we need to include the below to enable it - that is, running junit5 tests is not enabled by default.
test {
useJUnitPlatform()
}

You're saying that the main SourceSet should contain the test directory. This directory should be set in the test SourceSet.

Replace sourceSets with below simple snippet:
sourceSets.main.java.srcDirs = ['src']
sourceSets.test.java.srcDirs = ['tst']

Related

Gradle test filter doesn't work for complex test task

I have a project with unit tests.
We decided to migrate from testng to JUnit 5 but do it in a couple of iterations.
And now in our project tests for both platforms are present.
I handle it using the following build script:
def commonTestConfig =
{
ignoreFailures = true
...
}
task testNGTests(type: Test) {
useTestNG()
configure commonTestConfig
reports.html.destination = file("$buildDir/reports/testng")
}
task junitTests(type: Test) {
useJUnitPlatform()
configure commonTestConfig
reports.html.destination = file("$buildDir/reports/junit")
}
test {
dependsOn testNGTests
dependsOn junitTests
}
But if I try to run specific test from command line then build fails:
call gradle --warning-mode all test --tests my.package.LicenseTests
Task :compileJava UP-TO-DATE
Task :processResources UP-TO-DATE
Task :classes UP-TO-DATE
Task :compileTestJava UP-TO-DATE
Task :processTestResources UP-TO-DATE
Task :testClasses UP-TO-DATE
Task :testNGTests UP-TO-DATE
Task :junitTests UP-TO-DATE
Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong: Execution failed for task ':test'.
No tests found for given includes: [my.package.LicenseTests](--tests filter)
I.e. the command above runs all tests(tasks testNGTests and junitTests) without applying provided filter.
And after that tries to find tests to run in test task where no any one are supposed to run.
Is there a way to run both JUnit 5 and testng tests in Gradle cleanly, in a way preserving possibility to pass test filter from command line?

how to log resulting files from Gradle build task

I'm trying to write a BitBucket Pipeline script for my repository, but so far without much luck, because Gradle seems a pain to debug.
Is there any way to show the resulting files (from compiling the jar for example) in the console when it finishes the : build task?
Preferably I'd like to see as much information as possible, rather too much than too little.
Adding logging.captureStandardOutput LogLevel.DEBUG in my build.gradle file didn't seem to do much, still getting the same output:
:compileJavaNote: -snip-\src\main\java\com\-snip-\atlas\utility\SchematicUtil.java uses or overrides a deprecated API.
Note: Recompile with -Xlint: deprecation for details.
:processResources
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
BUILD SUCCESSFUL
Total time: 1.16 secs
Here's one way to do it (e.g. jar for a simple Java project). The build.gradle is:
apply plugin: 'java'
jar {
doLast {
new File("${buildDir}/libs").eachFileRecurse { file ->
println "TRACER: ${file.getAbsolutePath()}"
}
}
}
It should be straight-forward to tailor for other needs.

No error still Android Studio says "no tests were found"

I am trying to write an instrumentation test for my MainActivity. I followed the answers given here. Still Android Studio cannot find any tests. I have the class ApplicationTest.java in the androidTest folder. Here's the contents of the class:
package com.example.monicamarcus.mymusicplayer
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import com.example.monicamarcus.mymusicplayer.MainActivity;
public class ApplicationTest extends ActivityInstrumentationTestCase2<MainActivity> {
public ApplicationTest() {
super(MainActivity.class);
}
public void testNextTrackButton() throws Exception {
Activity activity = getActivity();
Button nextButton = (Button) activity.findViewById(R.id.nextTrackBt);
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
nextButton.performClick();
assertTrue(currentPosition < songList.size());
}});
activity.finish();
}
}
What is wrong with this test class? Or with the tests? I don't get any error, it just does not find any test to run. After I run the ApplicationTest the output ends with the following lines:
Running tests
Test running startedFinish
Empty test suite.
Here's the build.gradle file for the app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.monicamarcus.mymusicplayer"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
testApplicationId "app.src.androidTest.java.com.example.monicamarcus.mymusicplayer"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
debug {
minifyEnabled false
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
android {
useLibrary 'org.apache.http.legacy'
}
android {
sourceSets {
androidTest {
java.srcDirs = ['androidTest/java']
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile 'junit:junit:4.12'
}
Here's the output of the test run:
Testing started at 1:56 PM ...
Target device: gt_i8190n-4790068ee9a750c6
Installing APK: /Users/monicamarcus/AndroidStudioProjects/MyMusicPlayer/app/build/outputs/apk/app-debug.apk
Uploading file to: /data/local/tmp/com.example.monicamarcus.mymusicplayer
Installing com.example.monicamarcus.mymusicplayer
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/com.example.monicamarcus.mymusicplayer"
pkg: /data/local/tmp/com.example.monicamarcus.mymusicplayer
Success
Installing APK: /Users/monicamarcus/AndroidStudioProjects/MyMusicPlayer/app/build/outputs/apk/app-debug-androidTest-unaligned.apk
Uploading file to: /data/local/tmp/app.src.androidTest.java.com.example.monicamarcus.mymusicplayer
Installing app.src.androidTest.java.com.example.monicamarcus.mymusicplayer
DEVICE SHELL COMMAND: pm install -r "/data/local/tmp/app.src.androidTest.java.com.example.monicamarcus.mymusicplayer"
pkg: /data/local/tmp/app.src.androidTest.java.com.example.monicamarcus.mymusicplayer
Success
Running tests
Test running startedFinish
Empty test suite.
After I made some changes (I can publish them if anybody is interested) the result of running the instrumentation test class read as follows: "Running tests. Test running started. Test running failed: No test results. Empty test suite." Nobody has experience with these kind of tests?
In build.gradle, testApplicationId looks very strange. I would remove this and testInstrumentationRunner. The default values should be fine. Specifically, if you do not specify testApplicationId, it will be created by appending ".test" to your applicationId.
not sure if the problem has been found. Running into similar but only if added assertTrue() it got the "Empty test suite.".
but replace with assertNotNull() the test is fine, still cannot use assertTrue though (not know why).
(I'm using kotlin and modify the Android studio generated default test in the AndroidTest folder, and the test is coded in kotlin's companion object, if this make any difference).
Update: using assertTrue() outside the companion object is fine.
i just created a new project in android studio 2.2.2, made an android test fail did a gradle cC, and got the output below.
you can see the test failing on each device at the bottom of the output.
this question may be related.
the reports are in So35426990/app/build/reports/androidTests/connected/index.html
d:\AndroidStudioProjects\So35426990>gradle cC
Incremental java compilation is an incubating feature.
The TaskInputs.source(Object) method has been deprecated and is scheduled to be
removed in Gradle 4.0. Please use TaskInputs.file(Object).skipWhenEmpty() instea
d.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2421Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72421Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2421Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCompat2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCoreUi2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportCoreUtils2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportFragment2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportMediaCompat2421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42421Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2421Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:incrementalDebugJavaCompilationSafeguard UP-TO-DATE
:app:compileDebugJavaWithJavac UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources UP-TO-DATE
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:transformClassesWithDexForDebug UP-TO-DATE
:app:mergeDebugJniLibFolders UP-TO-DATE
:app:transformNative_libsWithMergeJniLibsForDebug UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE
:app:validateSigningDebug
:app:packageDebug UP-TO-DATE
:app:assembleDebug UP-TO-DATE
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareComAndroidSupportTestEspressoEspressoCore222Library UP-TO-DATE
:app:prepareComAndroidSupportTestEspressoEspressoIdlingResource222Library UP-TO-
DATE
:app:prepareComAndroidSupportTestExposedInstrumentationApiPublish05Library UP-TO
-DATE
:app:prepareComAndroidSupportTestRules05Library UP-TO-DATE
:app:prepareComAndroidSupportTestRunner05Library UP-TO-DATE
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl UP-TO-DATE
:app:processDebugAndroidTestManifest UP-TO-DATE
:app:compileDebugAndroidTestRenderscript UP-TO-DATE
:app:generateDebugAndroidTestBuildConfig UP-TO-DATE
:app:generateDebugAndroidTestResValues UP-TO-DATE
:app:generateDebugAndroidTestResources UP-TO-DATE
:app:mergeDebugAndroidTestResources
:app:processDebugAndroidTestResources UP-TO-DATE
:app:generateDebugAndroidTestSources UP-TO-DATE
:app:incrementalDebugAndroidTestJavaCompilationSafeguard UP-TO-DATE
:app:compileDebugAndroidTestJavaWithJavac
:app:compileDebugAndroidTestNdk UP-TO-DATE
:app:compileDebugAndroidTestSources
:app:mergeDebugAndroidTestShaders UP-TO-DATE
:app:compileDebugAndroidTestShaders UP-TO-DATE
:app:generateDebugAndroidTestAssets UP-TO-DATE
:app:mergeDebugAndroidTestAssets UP-TO-DATE
:app:transformClassesWithDexForDebugAndroidTest
:app:mergeDebugAndroidTestJniLibFolders UP-TO-DATE
:app:transformNative_libsWithMergeJniLibsForDebugAndroidTest UP-TO-DATE
:app:processDebugAndroidTestJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDebugAndroidTest UP-TO-DATE
:app:validateSigningDebugAndroidTest
:app:packageDebugAndroidTest
:app:assembleDebugAndroidTest
:app:connectedDebugAndroidTest
acme.so35426990.ExampleInstrumentedTest > useAppContext[KFFOWI - 5.1] FAILED
java.lang.AssertionError: failing
at org.junit.Assert.fail(Assert.java:88)
acme.so35426990.ExampleInstrumentedTest > useAppContext[KFFOWI - 5.1.1] FAILED
java.lang.AssertionError: failing
at org.junit.Assert.fail(Assert.java:88)
acme.so35426990.ExampleInstrumentedTest > useAppContext[Nexus 7 - 5.1.1] FAILED
java.lang.AssertionError: failing
at org.junit.Assert.fail(Assert.java:88)
:app:connectedDebugAndroidTes
t FAILEDng 96% > :app:connectedDebugAndroidTest
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:connectedDebugAndroidTest'.
> There were failing tests. See the report at: file:///D:/AndroidStudioProjects/
So35426990/app/build/reports/androidTests/connected/index.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.
BUILD FAILED
Total time: 35.227 secs
d:\AndroidStudioProjects\So35426990>

IntelliJ 14 Java project not aware of Gradle dependancy

I have an IntelliJ idea project which has a number of dependencies in build.gradle.
However just recently after adding dependencies to buid.gradle, IntelliJ doesn't seem to be aware of them (despite Gradle building and running the applicaition fine).
My build.gradle:
apply plugin: 'java'
version = '1.0'
repositories {
mavenLocal()
maven { url * } // Company's proxy maven repository
}
dependencies {
compile("wsdl4j:wsdl4j:1.6.2")
}
task wrapper(type: Wrapper) {
gradleVersion = '1.0-milestone-4'
distributionUrl = * // Company gradle distrubution URL
}
apply plugin: 'application'
mainClassName = "main.HelloWorld"
But when I use a class that is dependant, for example: javax.wsdl.xml.WSDLReader, IntelliJ looks like this
Despite it compiling and running fine.
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build
:run
Hello, World
BUILD SUCCESSFUL
How do I make IntelliJ aware of the classes?
I managed to fix this myself:
Under the toolbar I went View->Toolbars->Gradle. Then a sub window opened on the right. I then clicked on the refresh button, shown here:
And after a few moments is was all good!

Gradle build - non java project

My project has one .zip file in the source code that i want to put in appserver.zip file under folder 1.1.0/deployment. This .zip file again gets bundled in a .tar file.
NOTE: THIS project is not a JAVA/Groovy project i.e. there is no java programs or groovy programs
Following is my build.gradle, which is creating the correct .zip (with correct folder/ source .zip file in it) and .tar file containing the appserver.zip in it:
// Let say $projName, $folderArtifactoryVersion or etc variables used in the following script - are set correctly to a valid value.
task createZipFile( type: Zip) {
// Create artifact directory
def dirName = "build/folderDist"
def dirDist = new File( dirName )
dirDist.mkdirs()
destinationDir dirDist
archiveName "appserver.zip"
println ''
println 'bundleArchiveName: ' + archiveName
into( '1.1.0/deployment' ) {
from( "cognos/Integration/Deployment" )
include( 'SomeCognos_deploy.zip' )
}
}
task createTarFile( type: Zip) {
dependsOn createZipFile
def projName = "dircognosreporting"
def dirParent = "build/folderArts"
// Create artifact directory
def dirName = "$dirParent/com/truven/folder/$projName/$folderArtifactoryVersion"
def dirDist = new File( dirName )
dirDist.mkdirs()
destinationDir dirDist
archiveName "dirCognosReporting-${folderArtifactoryVersion}.tar"
println ''
println 'bundleArchiveName: ' + archiveName
println ''
into( '' ) {
from( "build/folderDist" )
include( 'appserver.zip' )
}
}
build {
dependsOn clean
dependsOn createTarFile
}
Gradle build log shows the following:
-bash-3.2$ /production/gradle/AKS/gradle-1.6/bin/gradle clean build
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheduled to be removed in Gradle 2.0. Please read http://gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on the replacement for dynamic properties.
Deprecated dynamic property: "folderArtifactoryVersion" on "root project 'dirCognosReporting'", value: "1.1.0.5".
bundleArchiveName: appserver.zip
bundleArchiveName: dirCognosReporting-1.1.0.5.tar
:clean
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:checkstyleMain UP-TO-DATE
:findbugsMain UP-TO-DATE
:pmdIntegrationTest UP-TO-DATE
:pmdMain UP-TO-DATE
:pmdTest UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:createZipFile
:createTarFile
:build
BUILD SUCCESSFUL
Total time: 5.117 secs
-bash-3.2$
My 2 ?s:
Why the println message defined within createZipFile and createTarFile were printed at the very first stage, even before clean. If I use task build << { ... } then - as expected, i get task build already exist. If I use just build << { .... }, I get a different behaviour.
As build result is giving me what i need under build/folderDist and build/folderArts folders, I'm not much worried about the above bullet at this time, but WHAT should I do, so that I don't see the following lines in the output (as i dont have anything related to java in my source code). Why, gradle is just not doing only - createZipFile, createTarFile and then exit out gracefully?
*
:clean
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:assemble UP-TO-DATE
:checkstyleMain UP-TO-DATE
:findbugsMain UP-TO-DATE
:pmdIntegrationTest UP-TO-DATE
:pmdMain UP-TO-DATE
:pmdTest UP-TO-DATE
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:createZipFile
:createTarFile
:build
*
ad 1. Because you are printing in the configuration phase, rather than the execution phase. If you want the println statement to be executed as part of executing the task, wrap it with doFirst { ... } or doLast { ... }.
ad 2. Someone (e.g. a parent build script) is applying the java, pmd, checkstyle, and findbugs plugins to this project. Get rid of all that. To nevertheless keep the clean task, apply the base plugin. To keep the build task, declare it yourself (change build { ... } to task build { ... }).
Seems like it's due to the reason as my /init.d folder has a somename.gradle build where I have defined apply plugin: 'java' within allprojects { ..... } section.
If I comment that out, I get an error saying build() ...not there or absent.

Categories

Resources