Kotlin - Java code coverage in Android studio showing 0% NOT WORKING - unit-testing

In one of my android project in Android studio, I have some source files written in Kotlin and there corresponding unit tests written in Java, Now when I try to run the test class with coverage it shows me 0%. All tests pass and runs fine but the coverage shows is 0%.
For the scenario where source file written in Java as well as there test class is also in Java it works perfectly fine, only issue is when source class is in kotlin and test class in Java or vice-versa.
Gradle Version : 4.1
Kotlin Version : 1.1.61
play_services = "11.8.0"//9.4.0
support_version = "27.1.0"//23.+
espresso = "3.0.1"
testCompile 'junit:junit:4.12'
//mockito
testCompile "org.mockito:mockito-core:2.8.9" // PowerMockito works on 2.8.9
testCompile "org.powermock:powermock-module-junit4:1.7.3"
testCompile "org.powermock:powermock-api-mockito2:1.7.0RC2"
androidTestCompile "com.android.support:support-annotations:$support_version"
androidTestCompile 'com.android.support.test:runner:1.0.1'
androidTestCompile 'com.android.support.test:rules:1.0.1'
androidTestCompile "com.android.support.test.espresso:espresso-core:$espresso"
androidTestCompile "com.android.support.test.espresso:espresso-intents:$espresso"
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
androidTestCompile 'org.mockito:mockito-android:2.8.9'
Any help is appreciated, Please let me know if anyone knows how to fix this. Thanks

Related

Sonarqube gives 0% coverage on spring boot gradle project

So I am working with SonarQube and I keep getting stuck. My coverage is 0% while it should not be 0% (I made sure with an simple test that always should run correctly and covers at least one method).
I am reading a lot of different gradle.build files on the internet, but I can't find one that works for me.
Also I think the problem is that sonarqube can't find certain files, but I can't find a working directory of someone which I can compare with my own.
Gradle.build
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id "org.sonarqube" version "3.0"
id 'jacoco'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '15'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile 'org.apache.httpcomponents:httpclient:4.5'
implementation('io.jsonwebtoken:jjwt:0.2')
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
compile 'junit:junit:4.12'
implementation 'org.modelmapper:modelmapper:2.4.1'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'mysql:mysql-connector-java'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.eclipse.jgit:org.eclipse.jgit:5.4.2.201908231537-r'
/**
* JUnit jupiter with mockito.
*/
testCompile group: 'org.mockito', name: 'mockito-junit-jupiter', version: '2.19.0'
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.19.0'
testCompile group: 'org.springframework.security', name: 'spring-security-test', version: '5.1.6.RELEASE'
}
sonarqube{
properties{
property 'sonarjava.source', '1.8'
property 'sonar.java.coveragePlugin', 'jacoco'
}
}
test {
useJUnitPlatform()
}
This is the folder where my test reports are in (I think you will need this to help me):
The build directory:
The jacoco test directory
Can anyone help me with my problem? If you want to see directories or other files, just ask and add them to the post.
Thanks in advance!
you need to make sure jacoco runs before sonarqube, so add something like:
tasks["sonarqube"].dependsOn("jacocoTestReport")
Also, side note, compile is deprecated, so use implementation and testImplementation instead.
The answer was in a directory that I had not shown. My project was missing the Jacocoreportxml file. Once I added this with the gradle build jacocoreportxml command in my terminal it worked!

Can we run Spock Testcases and Junit 5 test cases together In one project?

We are not able to run test cases written with Junit 5 and Spock framework together in one gardle project?
We tried add dependencies given in https://www.baeldung.com/junit-5-gradle to our gradle file. Gradle version is 4.10.3 and Junit 5. Below is my build.gradle file
apply plugin: 'groovy'
apply plugin: 'java'
repositories {
mavenCentral()
maven {
url "http://repo.fusesource.com/nexus/content/groups/public/"
}
maven {
url "https://repository.jboss.org/nexus/content/groups/public"
}
jcenter()
}
dependencies {
compile group: 'com.google.inject', name: 'guice', version: '4.2.2'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
testCompile(
'org.codehaus.groovy:groovy-all:2.4.8',
'org.spockframework:spock-core:1.0-groovy-2.4',
'org.jmockit:jmockit:1.8',
'junit:junit:4.12'
)
testRuntime(
'cglib:cglib:2.2.2',
'com.athaydes:spock-reports:1.2.7'
)
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testCompileOnly 'junit:junit:4.12'
}
test {
useJUnitPlatform()
testLogging { showStandardStreams = true }
}
I have created two test cases, one is using spock framework and other is using junit 5. But when I do gradlew -test, it runs only test cases written with Junit 5. Below is build path.
You need the Vintage test engine to execute Spock tests since they are based on JUnit 4, and you need the Jupiter test engine to execute the JUnit Jupiter tests.
So you need dependencies on both engines.
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
I'd also recommend that you upgrade to JUnit 5.5.1 (i.e., the latest and greatest).

Where is HibernateTestMixin located in Grails 3?

I am trying to upgrade a Grails 2.5.1 application to Grails 3.0.5. When I try to run the tests I get a compiler error
/Users/xxx/dev/xxx/src/test/groovy/y/xxx/z/PricingSpec.groovy: 5: unable to resolve class grails.test.mixin.hibernate.HibernateTestMixin
# line 5, column 1.
import grails.test.mixin.hibernate.HibernateTestMixin
It seems that grails-plugin-testing does not include that Mixin. Can anyone tell me what dependency I am missing?
EDIT Same goes for grails.test.mixin.gorm.Domain
Have a look at HibernateTestMixin Basics.
You would need this dependency in build.gradle:
dependencies {
testCompile 'org.grails:grails-datastore-test-support:4.0.4.RELEASE'
}
You might not have noticed it but these mixin were already had been moved to grails-data-mapping in Grails 2.4.* apps
If you look closely in BuildConfig.groovy of a newly created Grails 2.4.* or 2.5.*, one would see the same dependency.

Display test coverage using jacoco in gradle

I am using a gradle file to build my project. In this file I am using jacoco to produce a test report. What I want to do is modify the build file so that it displays a message if my test coverage isn't 100%. Or at least just display the test coverage. Is this possible?
Here is my build.gradle:
apply plugin: 'java'
apply plugin: 'jacoco'
sourceSets.main.java.srcDirs = ['src']
sourceSets.test.java.srcDirs = ['test']
dependencies {
testCompile group: 'junit', name: 'junit', version: "4.+"
}
repositories {
mavenCentral()
}
jacocoTestReport {
doFirst {
classDirectories = files('build/classes/main/draw')
}
reports {
xml.enabled false
csv.enabled false
html.destination "build/reports/coverageHtml"
}
}
task(runui, dependsOn: jar, type: JavaExec) {
main = 'gui.Main'
classpath = sourceSets.main.runtimeClasspath
}
defaultTasks 'clean', 'test', 'jacocoTestReport', 'runui'
There is a very simple Gradle plugin called gradle-jacoco-log that simply logs the Jacoco coverage data:
plugins {
id 'org.barfuin.gradle.jacocolog' version '1.0.1'
}
Then after ./gradlew jacocoTestReport, it shows:
Test Coverage:
- Class Coverage: 100%
- Method Coverage: 100%
- Branch Coverage: 81.2%
- Line Coverage: 97.8%
- Instruction Coverage: 95.3%
- Complexity Coverage: 90.2%
There are also some options to customize what is logged.
The other topic of enforcing a certain test coverage is covered by Gradle these days.
Full disclosure: I am the author of this little plugin.
At the moment this is not supported by the gradle jacoco plugin. You can vote for this issue at https://issues.gradle.org/browse/GRADLE-2783 and https://issues.gradle.org/browse/GRADLE-2854. As a workaround you could possibly parse the output file in a custom task.
You can use Gradle plugin gradle-console-reporter to report various kinds of summaries to console. JUnit, JaCoCo and Cobertura reports are supported.

How to run only one unit test class using Gradle

I am new to Gradle. I use Gradle 1.10 and Ubuntu 13.
I want to know if there's any command to execute only one unit test class, similar to testOnly in SBT.
To run a single test class Airborn's answer is good.
With using some command line options, which found here, you can simply do something like this.
gradle test --tests org.gradle.SomeTest.someSpecificFeature
gradle test --tests '*SomeTest.someSpecificFeature'
gradle test --tests '*SomeSpecificTest'
gradle test --tests 'all.in.specific.package*'
gradle test --tests '*IntegTest'
gradle test --tests '*IntegTest*ui*'
gradle test --tests '*IntegTest.singleMethod'
gradle someTestTask --tests '*UiTest' someOtherTestTask --tests '*WebTest*ui'
From version 1.10 of gradle it supports selecting tests, using a test filter. For example,
apply plugin: 'java'
test {
filter {
//specific test method
includeTestsMatching "org.gradle.SomeTest.someSpecificFeature"
//specific test method, use wildcard for packages
includeTestsMatching "*SomeTest.someSpecificFeature"
//specific test class
includeTestsMatching "org.gradle.SomeTest"
//specific test class, wildcard for packages
includeTestsMatching "*.SomeTest"
//all classes in package, recursively
includeTestsMatching "com.gradle.tooling.*"
//all integration tests, by naming convention
includeTestsMatching "*IntegTest"
//only ui tests from integration tests, by some naming convention
includeTestsMatching "*IntegTest*ui"
}
}
For multi-flavor environments (a common use-case for Android), check this answer, as the --tests argument will be unsupported and you'll get an error.
In versions of Gradle prior to 5, the test.single system property can be used to specify a single test.
You can do gradle -Dtest.single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest.single=ClassName*Test test you can find more examples of filtering classes for tests under this link.
Gradle 5 removed this option, as it was superseded by test filtering using the --tests command line option.
In case you have a multi-module project :
let us say your module structure is
root-module
-> a-module
-> b-module
and the test(testToRun) you are looking to run is in b-module, with full path : com.xyz.b.module.TestClass.testToRun
As here you are interested to run the test in b-module, so you should see the tasks available for b-module.
./gradlew :b-module:tasks
The above command will list all tasks in b-module with description.
And in ideal case, you will have a task named test to run the unit tests in that module.
./gradlew :b-module:test
Now, you have reached the point for running all the tests in b-module, finally you can pass a parameter to the above task to run tests which matches the certain path pattern
./gradlew :b-module:test --tests "com.xyz.b.module.TestClass.testToRun"
Now, instead of this if you run
./gradlew test --tests "com.xyz.b.module.TestClass.testToRun"
It will run the test task for both module a and b, which might result in failure as there is nothing matching the above pattern in a-module.
Please note that --tests option may not work if you have different build types/flavors (fails with Unknown command-line option '--tests'). In this case, it's necessary to specify the particular test task (e.g. testProdReleaseUnitTest instead of just test)
After much figuring out, the following worked for me:
gradle test --tests "a.b.c.MyTestFile.mySingleTest"
For multi modules projects it's necessary to use module's name and buildType:
./gradlew :module_name:testDebugUnitTest --tests com.package_name.TestsClass.*
To run some test method the same command, but with test's name:
./gradlew :module_name:testDebugUnitTest --tests com.package_name.TestsClass.test
Below is the command to run a single test class using gradlew command line option:
gradlew.bat Connected**your bundleVariant**AndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.TestClass
Below example to run class com.example.TestClass with variant Variant_1:
gradlew.bat ConnectedVariant_1AndroidTest -Pandroid.testInstrumentationRunnerArguments.class=com.example.TestClass
Run a single test called MyTest:
./gradlew app:testDebug --tests=com.example.MyTest
You should try to add asteriks (*) to the end.
gradle test --tests "com.a.b.c.*"
In my case, my eclipse java compiler warnings were set too high, and eclipse was not recognizing my class as valid for execution. Updating my compiler settings fixed the problem. You can read more about it here: annotation-nonnull-cannot-be-resolved
This worked for me
Release case:
gradle testReleaseUnitTest --tests testClass
Debug case:
gradle testDebugUnitTest --tests AsyncExecutorTest
You can see de projects with: gradle -q projects and move to the project where is the class to test