Weird bug with Android Studio 2.2 when running a single test directly in IDE - I'll either have "No tests found" or run my previously compiled test.
Example:
#Test
fun testyTest() {
}
I'll create this test, and get the "No tests found". So I restart, and test is found! Obviously, the above test passes because it's empty. Next, I want my test to fail. So I manually throw an exception and add one line of code, below.
#Test
fun testyTest() {
throw RuntimeException()
}
Now I recompile, and this test passes too, because IntelliJ is for some reason has cached the previous test and reruns that. To get this test to properly fail - I must restart the IDE.
Anyone know what's going on? Note - I'm using Kotlin pretty heavily in this project.
EDIT 2: This is a JVM test (non-android, src/test/java) in an android library project.
EDIT: here's a full test class
package com.example.zak
import org.junit.Before
import org.junit.Test
class ExampleTest {
#Before
fun setUp() {
}
#Test
fun testyTest() {
//throw RuntimeException()
}
}
Found the fix...the issue only occurs when there's a variant selection conflict. Removing the conflict fixed the issue.
Check if you're have all these dependencies:
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile 'junit:junit:4.11'
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
Also as you're using Kotlin please disable entirely Instant Run in File -> Preferences.
EDIT: You're talking about tests in library - do modules and project has these dependencies and proper configuration.
It looks like after Gradle rebuilding, test works fine.
I found this bug description in the JetBrains IntelliJ bug tracking system. Here is the URL:
https://youtrack.jetbrains.com/issue/IDEA-149275
It discusses a change that was made to classloading that may impact Junit test execution. A workaround is suggested by adding a line to the options.xml config file. Here is an example workaround for Windows boxes - change as necessary for your platform. The version of Android below is the current 2.2 release:
1) exit Android studio
2) navigate to the config/options directory for your Android version:
e.g. C:\Users\YourNameHere\.AndroidStudio2.2\config\options
3) then add the "<property name="idea.dynamic.classpath.jar" value="false" />" line inside the component block of the options.xml file:
<component name="PropertiesComponent">
<property name="idea.dynamic.classpath.jar" value="false" />
Start Android Studio and try running local Junit tests.
I guess you use gradle in your project? I had the exact same problem and the solution was to use gradle to execute tests (inside the gradle task list) instead of using the menu (right click on the test file and run tests).
Related
since the last Update (of Android Studio ? or Gradle ?) I cannot import code from prod in my tests classes.
A few months ago, the tests were working without any problems.
I can launch the tests, using gradle in command line, or using AndroidStudio test runner. And it work nice : it passes when the code is ok, it fails when there is a bug.
But the auto-completion isn't working, the class, methods ... are red.
I cannot click on an object to navigate to the declaration ... and so on.
And the AndroidStudio automatic quickfix is : "add dependency on module xxx"
which add a line in the gradle file :
implementation project(path: ':app')
I have the same problem on several project, with gradle 6.1.1 or 7.2.1
AndroidStudio version is :
Chipmunk, 2021.2.1 patch 1
built on May 18 2022
the unit test, in app/src/test/java/com/apackage/ASimpleTest.kt
package com.apackage
import org.junit.Test
import kotlin.test.assertEquals
class ASimpleTest {
#Test
fun aTest(){
val plop = AClass()
assertEquals(1, plop.aMethod())
}
}
the class in app/src/main/java/com/apackage/AClass.kt:
package com.apackage
class AClass {
fun aMethod() = 1
}
I have no problem calling a method in the same folder as the tests.
The problem is test/main folder calling src/main
I am using Ubuntu Linux 16, Gradle 2.10, Groovy 2.4.5, and Java Version 9. I'd like to remove Gradle if I don't need it. I am trying to run this program here from GitHub.
Here is the text of the code:
package timezra.groovy.trampoline_memoize
import spock.lang.Specification
class RecursiveMemoizationSpec extends Specification {
int count
def fib = { n ->
count++
if(n == 0) 0
else if(n == 1) 1
else fib.call(n-1) + fib.call(n-2)
}.memoize()
def "calls should be cached"() {
when:
def actual = fib 10
then:
actual == 55
count == 11
// count == 177 //unmemoized
}
}
When I run it with
groovy RecursiveMemoizationSpec.groovy
I get this error:
/groovy-trampoline-memoize-specs/src/test/groovy/timezra/groovy/trampoline_memoize/RecursiveMemoizationSpec.groovy:
3: unable to resolve class spock.lang.Specification # line 3, column
1. import spock.lang.Specification ^
1 error
This program was quoted on different websites. Those versions produce the same problem. I expected it to work without this error. How do I get the program above to work?
This is a very old project and it doesn't look like it's been touched in 7 years. You shouldn't discard gradle but learn how to use it. checking out the build file you can see that it add the Groovy(1.8.3) and spoc dependencies. Since this project doesn't have a gradle wrapper I would use sdkman to download and install gradle:
https://sdkman.io/install
Then I would just run gradle test from the root of the project.
Another issue you might run into is that you are running Java 9. Groovy 1.8.3, probably won't run on Java 9. I know that Groovy has been working on groovy 9+ compatibility, in the 2.5.x version, and will be adding more syntax support in 3.0. So you can either update the build file to use a never version of Groovy, or downgrade to Java 8. If you install sdkman, it also, gives you an easy way to manage, and switch Java version on the fly.
If you still really want to get rid of gradle then you'll have to use the Groovy #Grab annotations, which will pull in the dependencies similar to gradle:
http://docs.groovy-lang.org/latest/html/documentation/grape.html
But I would recommend that you get to know Gradle, because it is a powerful build system.
Could anyone help me out with the following error. When i clean the project, it doesn't show any error but every time i try to run i get this message.
Error:Execution failed for task ':app:dexDebug'.
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe'' finished with non-zero exit value 2
The application was running with no errors but as my system crashed and restarted android studio i had few updates for Android API 22. After the update the application keep giving me this error message. I downloaded JDK 8 and tried to run but didn't work. Later i downgraded to JDK 7 after this stack post but still has not fixed it.
Is it possible to use Java 8 for Android development?
I have looked through numerous similar question but none works for me. Some similar issues found on stack were
why java.exe exit with value 1 in android studio
Error:Execution failed for task ':app:dexDebug'. > comcommand finished with non-zero exit value 2
Java finished with non-zero exit value 2 - Android Gradle
java.exe finished with non-zero exit value 2
Process 'command' C: \ Program Files \ Java \ jdk1.8.0_31 \ bin \ java.exe '' finished with a non -zero exit value 2
I have looked through dependencies in gradle built to see if there were any conflict but found none.
I even tried to copy the code into a new project and run it but still no success. Anyone who has faced the same issue and solved it please help me out.
After days of trying out finally could fix the issue. The problem with one of my .jar files. I had to remove each jar and check one by one until i found it. I removed the .jar file and cleaned my project and ran successfully. If any one should face similar issue check your jar file one by one.
I had the same error
app:dexDebug ExecException finished with non-zero exit value 2
i solve it by adding this line of code
defaultConfig {
multiDexEnabled true
}
The reason is that i was using too many libraries
Hope this post will help anyone
I had the same problem when I compiled google play services to my dependencies.
My mistake was I was compiling the enitre package like this
compile 'com.google.android.gms:play-services:8.3.0'
Instead when I tried the selective compile, it worked.In my case I was using for google sign in,so it had just had to be
compile 'com.google.android.gms:play-services-auth:8.3.0'.
More details are in the documentaion. https://developers.google.com/android/guides/setup#split
Hope this will be of a little help to someone someday :)
I know it's late.
But that's what actually worked for me.
1. Build > Clean Project
2. Close gradle daemon processes (ps -e| grep gradle //this will list gradle processes)
3. File > Invalidate caches & Restart
I hope this will work for everyone.
For future readers. If you're using your own AARs and upgraded one that has a shared dependency, you can also just try cleaning the project. In Android Studio "Build" -> "Clean Project".
This solved the issue for me, so I'd recommend giving it a quick try before you start digging for dependencies.
For me the solution was to remove an unnecessary/duplicate dependency entries. This is a similar solution others offered here that makes sense, but not exactly the same solution as those offered by others.
Since I was already including *.jar files in the list of files to be compiled, there was no need for additional entries in the dependencies list.
Before:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.parse.bolts:bolts-android:1.+'
compile fileTree(dir: 'libs', include: 'Parse-*.jar')
}
After:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
}
Remove
compile 'com.parse:parse-android:1.+'
add this linecompile fileTree(dir: 'libs', include: ['Parse-*.jar'])
Finally, it will look like this
dependencies {
compile fileTree(dir: 'libs', include: ['Parse-*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.facebook.android:facebook-android-sdk:4.5.0'
compile 'com.android.support:design:22.2.0'
compile 'com.parse.bolts:bolts-android:1.+'
}
I had the same problem, just i did Biuld--> Rebiuld project and it works again ;)
I was using the robolectric dependency in my android project. I removed all robolectric dependencies in gradle and deleted all my robolectric tests, then I built the app and finally I could run the android project. Hope this can work for us.
I faced the same problem when trying to add the SignalR Java client library to my project's dependencies. I first changed the target JDK of my app from 1.8.0_XX to 1.7.0_XX, but it didn't work. Then I changed the SignalR project to target 1.7.0_XX as well, rebuilt and added it to my project's libs folder instead of the first build (which was in 1.8.0_XX), and that miraculously worked.
For me, the problem was that I was using different versions of "Google Play services sub API's"
Before:
Click to see the image
After:
Click to see the image
Check if there are 2 classes with same name. Each class name should be unique.
maybe for this issue you got means you added same liberay file in more than one time
can someone please help me, I can't get the step definitions to work. I've took a screen shot of the Andoid Studio work place, as i thought it be easier then explaining it all. Basically I want to run the feature file myfeature.feature and when i do i want it to pick up the steps that are defined in the MyStepdef Class. I'm sure it an easy fix but this has not worked for me for a number of days
UPDATE!
I downloaded the skeleton class (as suggested) and even managed to run it in its own android workspace. Tried to implement with my program, using the classes from the skeleton, same feature, same set up but cannot run it.
I tried to create the feature file from scratch, the using android to set the stepdefintions from that feature file and still cannot run it
My thoughts are that I have the gradle set up to run an other automated test, in which i had complication, now I got them perfect with the "solution" i posted [//stackoverflow.com/questions/36968728/android-studio-testing-library-dependencies-that-have-been-compiled-using-java/36975041#36975041]
I have a feeling that the automated test setup that works well with expresso will not run well for the cucumber feature tests.
Anytime I add the jvm (0.5) and delete the exclude statements the old error comes bacK
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:1 error; aborting
:app:transformClassesWithDexForDebugAndroidTest FAILED
Error:Execution failed for task ':app:transformClassesWithDexForDebugAndroidTest'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_74\bin\java.exe'' finished with non-zero exit value 1*
Anytime I apply the exclude cucumber-jvm-deps on to a dependecny, I can run the espresso tests but not the cucumber.feature - either it does not recognise steps or if run using the JUnit (as in the skeleton) setup I get a completely new error
"C:\Program Files\Java\jdk1.8.0_91\bin\java" -ea -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files\Android\Android Studio\bin" -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Android\Android Studio\lib\idea_rt.jar;C:\Program Files\Android\Android Studio\plugins\junit\lib\junit-rt.jar;C:\Users\hbill\AppData\Local\Android\sdk\platforms\android-23\android.jar;C:\Users\hbill\AppData\Local\Android\sdk\platforms\android-23\data\res;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\classes\debug;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-html\0.2.3\624a0c986088e32910336dd77aee5191c04a8201\cucumber-html-0.2.3.jar;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support.test\runner\0.5\jars\classes.jar;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support.test\runner\0.5\res;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support.test.espresso\espresso-idling-resource\2.2.2\res;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support.test.espresso\espresso-idling-resource\2.2.2\jars\classes.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-library\1.3\4785a3c21320980282f9f33d0d1264a69040538f\hamcrest-library-1.3.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-integration\1.3\5de0c73fef18917cd85d0ab70bb23818685e4dfd\hamcrest-integration-1.3.jar;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support\support-v4\23.3.0\jars\classes.jar;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support\support-v4\23.3.0\jars\libs\internal_impl-23.3.0.jar;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support\support-v4\23.3.0\res;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\com.google.code.findbugs\jsr305\2.0.1\516c03b21d50a644d538de0f0369c620989cd8f0\jsr305-2.0.1.jar;C:\Users\hbill\AppData\Local\Android\sdk\extras\android\m2repository\com\android\support\support-annotations\23.3.0\support-annotations-23.3.0.jar;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support\support-vector-drawable\23.3.0\jars\classes.jar;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support\support-vector-drawable\23.3.0\res;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support\animated-vector-drawable\23.3.0\jars\classes.jar;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support\animated-vector-drawable\23.3.0\res;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\info.cukes\gherkin\2.12.2\17138631fa20fd0e44a13e50d6b7be59cee1a94\gherkin-2.12.2.jar;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.3.0\res;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support\appcompat-v7\23.3.0\jars\classes.jar;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support.test.espresso\espresso-core\2.2.2\res;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support.test.espresso\espresso-core\2.2.2\jars\classes.jar;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support.test\exposed-instrumentation-api-publish\0.5\jars\classes.jar;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support.test\exposed-instrumentation-api-publish\0.5\res;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support.test\rules\0.5\res;C:\Users\hbill\Documents\TheFoodHunt2\app\build\intermediates\exploded-aar\com.android.support.test\rules\0.5\jars\classes.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\javax.annotation\javax.annotation-api\1.2\479c1e06db31c432330183f5cae684163f186146\javax.annotation-api-1.2.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\javax.inject\javax.inject\1\6975da39a7040257bd51d21a231b76c915872d38\javax.inject-1.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-core\1.2.4\72790b1da44d8d3d2764c6aef29865ee228bbeb1\cucumber-core-1.2.4.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\com.google.guava\guava\17.0\9c6ef172e8de35fd8d4d8783e4821e57cdef7445\guava-17.0.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-junit\1.2.4\8a57b68486cff4da4f10019cb9c62c597a6d2861\cucumber-junit-1.2.4.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-android\1.2.4\cf6fd6b0de808c6b28bf8e9d22f6572a180930a3\cucumber-android-1.2.4.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\com.squareup\javawriter\2.1.1\67ff45d9ae02e583d0f9b3432a5ebbe05c30c966\javawriter-2.1.1.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.3\42a25dc3219429f0e5d060061f71acb49bf010a0\hamcrest-core-1.3.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-java\1.2.4\57cca534b7abe43f6dd7624b90d3d97d33d3023d\cucumber-java-1.2.4.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\junit\junit\4.12\2973d150c0dc1fefe998f834810d68f278ea58ec\junit-4.12.jar;C:\Users\hbill\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-jvm-deps\1.0.3\cccdeff234db8b12e91ae2529812f1240b4d5603\cucumber-jvm-deps-1.0.3.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 boo.thefoodhunt.RunCukesTest
!!! JUnit version 3.8 or later expected:
java.lang.RuntimeException: Stub!
at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
at junit.textui.TestRunner.<init>(TestRunner.java:54)
at junit.textui.TestRunner.<init>(TestRunner.java:48)
at junit.textui.TestRunner.<init>(TestRunner.java:41)
at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:205)
at com.intellij.rt.execution.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:188)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Process finished with exit code -3
Now i hate to be defeated, but I think im just going to have to leave it and go around it somehow (even though its for testing coursework :/) because this has took so much out of my time, and eating in to my revision.
I have took the screen shots of my settings (in case of quick fix, silly mistake)
JUnit - Run Setup
Cucumber Feature Setup
TBH the suggestion below regarding the CLI, I'm not sure what is meant as this is the first time I tried to setup a testing enviroment, I understand it to be the set as a type of Main activity for the test to be ran? , but i do not know how to set it any other way.
The grade build dependcies :
compile 'com.android.support:appcompat-v7:23.3.0'
androidTestCompile('info.cukes:cucumber-java:1.2.4') {
exclude module: 'cucumber-jvm-deps'
}
androidTestCompile('info.cukes:cucumber-junit:1.2.4') {
exclude module: 'cucumber-jvm-deps'
}
androidTestCompile('info.cukes:cucumber-core:1.2.4') {
exclude module: 'cucumber-jvm-deps'
}
androidTestCompile 'junit:junit:4.12'
compile 'com.android.support:support-annotations:23.3.0'
compile 'com.google.guava:guava:17.0'
androidTestCompile 'com.android.support:support-annotations:23.3.0'
compile 'info.cukes:gherkin:2.12.2'
// androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile('info.cukes:cucumber-android:1.2.4') {
exclude module: 'cucumber-jvm-deps'
}
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'info.cukes:cucumber-jvm:1.2.4'
androidTestCompile('info.cukes:cucumber-html:0.2.3') {
exclude module: 'cucumber-jvm-deps'
}
compile 'info.cukes:cucumber-jvm-deps:1.0.3'
The message you see is that the steps are undefined but have implemented the stubs.
This tells me that the place you have implemented the stubs are not in the correct part of the classpath. There is probably something wrong with your glue.
You want the steps to be in the same package, or a sub-package, as the test runner. That is the class where you invoke the Cucumber runner.
I would consider adding a JUnit class annotated as in the https://github.com/cucumber/cucumber-java-skeleton to run Cucumber.
#RunWith(Cucumber.class)
public class RunCukesTest {
}
Note that this class has to be in the test classpath.
This is different from your current approach where you are using the CLI to invoke Cucumber.
I use AS 0.4.2 then imported GMS as described in doc:
https://developer.android.com/google/play-services/setup.html
dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile 'com.android.support:appcompat-v7:+'
compile 'com.android.support:support-v13:13.0.+'
compile 'com.google.android.gms:play-services:4.0.30' }
all what is detected by IDE is R class, but rest of code is invisible.
On the other hand I put following code:
import com.google.android.gms.common.GooglePlayServicesUtil;
(...)
private void connectGms(){
Log.d(MbcConstants.TAG,
"check for gms"+ GooglePlayServicesUtil.
isGooglePlayServicesAvailable(this));
}
And it works, but IDE still shows class name in red, but properly shows the params and returned type for method.
Ok - it seems like a bug in Android studio.
What I've done do fix it:
remove all dependencies (use F4 key) from project and the module
delete files from projectroot/.idea/libraries
open the module gradle.buld
add back all dependencies that are needed by your project to work. In my case:
dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile 'com.android.support:appcompat-v7:19.0.0'
compile 'com.google.android.gms:play-services:4.0.30'
}
Synchronize gradle.build once again
Continue your adventure of love and hate or return to eclipse.
All you needed to do was hit the Sync Project with Gradle Files button after editing the build.gradle file by hand.