Cannot find JUnit and Robolectric using Gradle - java

I'm trying to create a simple unit test project using JUnit and Robolectric. The build.gradle file is attached below.
evaluationDependsOn(':blitzen')
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
//compile fileTree(dir: 'libs', include: ['*.jar'])
def blitzenModule = project(':blitzen')
compile blitzenModule
testCompile blitzenModule.android.applicationVariants.toList().first().javaCompile.classpath
testCompile blitzenModule.android.applicationVariants.toList().first().javaCompile.outputs.files
testCompile files(blitzenModule.plugins.findPlugin("com.android.application").getBootClasspath())
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
}
Where "blitzen" is the app this test project is testing. Gradle was able to download junit and robolectric. However, I still got compiler errors on test code which complains about not being able to find various junit and robolectric packages. Do I need to add anything else in the build script so that the junit and robolectric jars can be used correctly?
Thanks.

Turns out I have put the test code inside "src/main/..." instead of "src/test/...". Relocating the test source files resolves the issue.

Related

Gradle DSL method not found: exclude()

i am using a lot of library in my project. And some libraries using same jar file therefore i writed this on build.gradle :
dependencies {
compile fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.twotoasters.jazzylistview:library:1.2.1'
compile 'com.google.firebase:firebase-core:10.2.4'
compile 'com.google.firebase:firebase-database:10.2.4'
compile 'com.orhanobut:dialogplus:1.11#aar'
compile 'com.github.recruit-lifestyle:FloatingView:2.2'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.nineoldandroids:library:2.4.0'
compile ('com.specyci:residemenu:1.6+'){
exclude group: 'com.nineoldandroids', module: 'library' }
compile files('libs/poppyview.jar'){
exclude group: 'com.nineoldandroids', module: 'library' }
}
And i am getting error :
Error:(54, 0) Gradle DSL method not found: 'exclude()'
Possible causes:The project 'DopingEng' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
Upgrade plugin to version 2.3.1 and sync projectThe project 'DopingEng' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper fileThe build file may be missing a Gradle plugin.
Apply Gradle plugin
Gradle already update , how can i solve this problem ?
Here's the problem
compile files('libs/poppyview.jar'){
exclude ...
}
A file based dependency does not work in the same way as a dependency coming from a repository. There is no meta data associated with it (eg no dependency information) so there's also nothing to exclude (since there's no transitive dependencies).
Do you have the pom/ivy meta-data for libs/poppyview.jar? If so then you shouldn't declare it like this (I suggest a local maven repository folder). If you don't then there's nothing to exclude

conflicting dependencies - android studio

I am trying to add a dependency giphy4j in my project and this dependency is using junit 4.8.1 but my project is using the latest junit 4.12.
build.gradle(module:app):
androidTestCompile 'junit:junit:4.12'
compile 'at.mukprojects:giphy4j:1.0.1'
This configuration is giving me error on gradle sync.
When I change androidTestCompile to compile and vice versa, It works. I am not getting this point. I dig into dependency stuff compile, apk, TestCompile etc. but couldn't get the proper idea.( As I am a Freshman). And, this conflicting error is also not comprehensible.
point 1: Is compiling the junit(to release with apk) wrong? junit 4.12 is set by default when I create a new project.
point 2: I don't want to configure my third-party-dependency with androidTestCompile becuase It does not show up in release configuration when I run ./gradlew app:androiddependencies.
./gradlew app:androiddependencies output
Error: Error:Conflict with dependency 'junit:junit' in project ':app'. Resolved versions for app (4.8.1) and test app (4.12) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Exclude junit from library.
compile ("at.mukprojects:giphy4j:1.0.1") {
exclude group: 'junit', module: 'junit'
}

How do I include another project's class libraries in my jar using Gradle?

I am developing for Android, and I am trying to include another project's class files in my .jar file.
My build.gradle file for library project looks like this:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.3'
compile project(':xyz')
}
My build.gradle file for xyz project looks like this:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
}
The issue is that when I create another project that includes my jar library, it has unresolved references that exist in my xyz project. When I run that project, it complains about not being able to find a class that exists in the xyz project.
Right click on app , open module settings (F4), Dependencies, Green + and select library file. build.gradle is updated automatically :)

How to fix "Couldn't load shared library 'libgdx64.so' for target: Linux, 64-bit"

I am trying to use the headless LibGDX for unit testing, but when I run the test I get this error:
Couldn't load shared library 'libgdx64.so' for target: Linux, 64-bit
I read here that I need to add gdx-natives.jar. Is this correct, and where can I find this file?
Also, where in my project should I add the file?
I found the answer on this BitBucket repo. The README gives a nice explanation of how to implement this using Gradle.
Basically, you just add GdxTestRunner.java from that repo, and then add a #RunWith to each of your test files:
#RunWith(GdxTestRunner.class)
public class MyClassTest {
...
}
Then in your root level build.gradle file, add something like this to your core dependencies:
testCompile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion"
testCompile "com.badlogicgames.gdx:gdx:$gdxVersion"
testCompile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
testCompile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
testCompile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-desktop"
testCompile "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
testCompile "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-desktop"
Obviously the box2d and bullet dependencies are only necessary if you are using those libraries.
On the BitBucket repo README, the example includes both
testCompile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion"
and
compile "com.badlogicgames.gdx:gdx-backend-headless:$gdxVersion"
I don't think it is necessary to include this for compile, and if I correctly understand how Gradle works, it will actually slow down your build.

Gradle testCompile with mockito doesn't find the package

When I add
dependencies {
testCompile 'org.mockito:mockito-all:1.9.5'
}
to my build.gradle, the JAR file gets downloaded, but the compilation fails with
error: package org.mockito does not exist
and a bunch of follow-up errors. When I replace testCompile by compile, it works. I'm still rather beginner concerning Gradle, but testCompile sound just right to me. Moreover,
testCompile 'junit:junit:4.+'
testCompile 'com.google.guava:guava-testlib:18.+'
work just fine. Can someone explain what's going on here?
With that configuration (and it's correct), the code that uses Mockito needs to be in src/test/java rather than src/main/java. testCompile defines compile dependencies for src/test/java.

Categories