I am using:
Android Studio 2.1.3
Gradle 2.14.1 (I tried with 2.14 also)
OpenJDK version "1.8.0_91"
I want to write some Unit tests with Groovy and Spock for sample Android application.
I have already read about RoboSpock.
When I am trying to run simple test:
package a.b.regex
class TestSum extends spock.lang.Specification {
def "test adding some numbers"() {
when:
def a = 5 + 4
then:
a == 9
}
}
When I try to run this test in Android Studio I have an error:
Process finished with exit code 1
Class not found: "a.b.regex.TestSum"Empty test suite.
Configurations that I used:
1)
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
}
}
apply plugin: 'groovyx.grooid.groovy-android'
// ...
dependencies {
testCompile 'org.robospock:robospock:1.0.0'
}
2)
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.0.0'
}
}
apply plugin: 'groovyx.android'
dependencies {
testCompile "org.codehaus.groovy:groovy-all:2.4.1"
testCompile "org.spockframework:spock-core:1.0-groovy-2.4"
testCompile 'org.codehaus.groovy:groovy:2.4.6:grooid'
}
From the console no tests are run at all.
With testing Java applications I have no problem.
Here is the project code where I want to use Spock: GitHub repository
Thankfully to Pieces I found the answer.
You should use the following configuration:
apply plugin: 'groovyx.android'
buildscript {
repositories {
jcenter() // or mavenCentral, etc.
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.0.0'
}
}
testCompile 'org.codehaus.groovy:groovy:2.4.7:grooid'
testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
exclude group: 'org.codehaus.groovy'
exclude group: 'junit'
}
1)
This should work great other than you are using an outdated version of the groovy android plugin. The current version is 1.0.0. The error you are seeing is that you included your tests in androidTest source folder, when they should be included in the test source folder.
2)
You do not want groovy-all, and want to exclude that from the spock transitive dependencies as well.
This would look similar to
dependencies {
testCompile 'org.codehaus.groovy:groovy:2.4.7:grooid'
testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
exclude group: 'org.codehaus.groovy'
exclude group: 'junit'
}
}
Same as the problem with #1 you probably have the source under androidTest folder instead of the test folder.
The androidTest folder is for test that will run on the device, and the test folder if for tests that will run on your machines JVM.
If you reached here trying to configure for gradle 6.6 this will help you:
I stepped multiple times into this while trying to configure Spock in android having gradle 6.6, there has been multiple changes in gradle, so they made this plugin deprecated 'groovyx.android': https://github.com/groovy/groovy-android-gradle-plugin
Deprecated: This plugin has been deprecated in favor of Kotlin which has the full support of JetBrains and Google. The changes that go into each Android Plugin Version make it really hard for this plugin to keep up. As of Gradle 6.0 this plugin does not work.
Found out that gradle have a separate plugin for groovy support:
https://plugins.gradle.org/plugin/org.codehaus.groovy.android
This is the configuration for gradle 6.6 using groovy DSL for gradle
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "gradle.plugin.org.codehaus.groovy:groovy-android-gradle-plugin:3.0.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
And then in the app configuration:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "org.codehaus.groovy.android"
def groovyVersion = "3.0.5"
def spockVersion = "2.0-M3-groovy-3.0"
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0'
testImplementation 'junit:junit:4.13'
testImplementation("org.codehaus.groovy:groovy:${groovyVersion}")
testImplementation("org.codehaus.groovy:groovy-all:${groovyVersion}")
testImplementation("org.spockframework:spock-core:${spockVersion}")
testImplementation("org.spockframework:spock-spring:${spockVersion}")
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Related
I have been working on multi module gradle project for spring boot devtools. Here is the github repo - GitHub Repo
-spring-boot-dev-tools
-src/main
-java/com/jhooq/springboot/devtools
-resources
-spring-boot-dev-tools.gradle ====- subproject gradle
-.gitignore
-build.gradle ====- main gradle
-gradlew
-gradlew.bat
-settings.gradle
This how my build.gradle(main gradle)looks like : -
buildscript {
ext {
springBootVersion = '2.1.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
allprojects {
group 'com.jhooq'
version '1.0-SNAPSHOT'
}
subprojects{
repositories {
mavenCentral()
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile ("org.springframework.boot:spring-boot-starter")
compile ("org.springframework.boot:spring-boot-starter-test")
}
}
project(':spring-boot-dev-tools'){
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
dependencies {
compile project(':spring-boot-app')
compile ("org.springframework.boot:spring-boot-starter-web")
developmentOnly("org.springframework.boot:spring-boot-devtools")
}
}
So as you can see if i put compile ("org.springframework.boot:spring-boot-starter-web") inside project(':spring-boot-dev-tools') my spring boot application starts on port 8000 and keeps running
But i face issue when i move following gradle scripts inside spring-boot-dev-tools.gradle, then my spring boot application starts and shutdown just like normal spring boot application.
project(':spring-boot-dev-tools'){
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
dependencies {
compile project(':spring-boot-app')
compile ("org.springframework.boot:spring-boot-starter-web")
developmentOnly("org.springframework.boot:spring-boot-devtools")
}
So if i summarize my issue when i move spring spring-boot-starter-web and spring-boot-devtools dependencies inside submodule, spring boot doesn't work/run on port:8000 but instead it starts and shutdown like a normal spring boot application.
Is there a reason why you have defined main class in every separate Java package?
I recently made a modular monolith example which might help you:
modular monolith example
Also some tips to consider:
define a common gradle configuration instead of "allprojects" and "subprojects" keywords. Difference between these two comes down to composition over inheritance
use keyword implementation instead of compile. That way your dependencies do not leak into the compile classpath of consumers anymore. Otherwise use keyword api
I managed to get it working but still the solution is apparently does not feel good to me. But anyways here is what i did -
I moved sub project/module dependency to its own gradle file and removed it from build.gradle(main project gradle)
Instead of "compile project" i switched to "implementation"
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
dependencies {
implementation {
'org.springframework.boot:spring-boot-devtools'
':spring-boot-app'
'org.springframework.boot:spring-boot-starter-web'
}
}
After I attempted to add the AssertJ library to my project for testing, some configuration in my project changed and I cannot import any classes from my main package into my JUnit test package.
My Main Class runs as expected, but I am unable to run any tests requiring Classes from the main package.
I've tried the following:
Removing AssertJ import
Rebuild Project
Refresh All Gradle Projects
Invalidate IntelliJ cache an Restart from Settings
Deleting project locally and re-downloading from GitHub
Removing testing directory and re-adding
Looking at another project that is working it appears to be an issue with my build.gradle file
group 'carpecoin'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.2.41'
ext.junitJupiterVersion = '5.0.3'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.3'
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testImplementation group: 'junit', name: 'junit', version: '4.12'
// JUnit Jupiter API and TestEngine implementation
testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
// To avoid compiler warnings about #API annotations in JUnit code
testCompileOnly('org.apiguardian:apiguardian-api:1.0.0')
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
implementation 'io.reactivex.rxjava2:rxjava:2.1.1'
implementation 'com.google.firebase:firebase-admin:6.0.0'
implementation 'com.google.firebase:firebase-database:15.0.0'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
The line below in the build.gradle file was failing causing the issue above.
implementation 'com.google.firebase:firebase-database:15.0.0'
I updated Firebase to the following and now it is still causing an issue when running tests. It appears to run fine in production:
implementation 'com.google.firebase:firebase-core:16.0.1'
I am on a Linux machine with Idea IntelliJ and I would like to create a Dropwizard project with gradle. With maven archetypes this job would be very easy.
mvn archetype:generate
-DarchetypeGroupId=io.dropwizard.archetypes
-DarchetypeArtifactId=java-simple
-DarchetypeVersion=[REPLACE ME WITH A VALID DROPWIZARD VERSION]
For gradle I am struggling to get going. As I checked out other post I was wondering if what I did was correct:
// Needed Instead of the Shade plug in
plugins {
id 'com.github.johnrengelman.shadow' version '2.0.1'
}
version '1.0-SNAPSHOT'
group 'org.example.sampleName'
description 'Sample Dropwizard REST '
apply plugin: 'java'
apply plugin: 'application'
repositories {
mavenCentral()
}
dependencies {
compile 'io.dropwizard:dropwizard-core:1.2.0'
testCompile 'junit:junit:4.12'
}
mainClassName = 'org.example.sampleName.SampleApplication'
run {
args = ['server', 'config.yml']
}
shadowJar {
mergeServiceFiles()
exclude 'META-INF/*.DSA', 'META-INF/*.RSA', 'META-INF/*.SF'
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
Then I just run gradle shadowJar and then run java -jar build/SampleApplication.jar server config.yml. Is this correct? And is the line testCompile 'junit:junit:4.12' enough for the unit test?
You could use this Yeoman generator:
https://www.npmjs.com/package/generator-dropwizard-gradle
If you have npm installed.
The line testCompile 'junit:junit:4.12' is sufficient, yes.
You can also search on github for examples, like this one:
https://github.com/kishaningithub/dropwizard-gradle
I have a Java module that generates some metadata for my Android app and needs to run as a regular Java application. After updating to Android Studio 3.0 (in 2.3.3 it worked as expected) I get a NoClassDefFoundError for the dependencies (in the case below for com/google/gson/GsonBuilder, the main method itself can be found).
I even tried putting the GSON Jar into the libs folder but it still throws the NoClassDefFoundError.
I assume the dependencies are not properly included. Any idea how to fix this?
The build.gradle looks like this:
apply plugin: 'java'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.code.gson:gson:2.8.1'
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
My class looks like this:
import com.google.gson.GsonBuilder;
public class myClass {
public static void main(String[] args) {
GsonBuilder builder = new GsonBuilder();
}
}
Use api instead of implementation.
From the docs
When your module configures an implementation dependency, it's
letting Gradle know that the module does not want to leak the
dependency to other modules at compile time. That is, the dependency
is available to other modules only at runtime.
Using this dependency
configuration instead of api or compile can result in significant
build time improvements because it reduces the amount of projects that
the build system needs to recompile. For example, if an implementation
dependency changes its API, Gradle recompiles only that dependency and
the modules that directly depend on it. Most app and test modules
should use this configuration.
See difference between api and implementation here for more info.
This is a problem probably related with Android Gradle plug-in. Try using compile instead of implementation in yourbuild.gradle`:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.8.1'
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
The above is working in Android Studio 3.0 but with Android Gradle plug-in 2.3.3 in root build.gradle:
classpath 'com.android.tools.build:gradle:2.3.3'
UPDATE
I manage to make it works by adding google() to repositories block in root build.gradle. The following build.gradle is for the Java module:
apply plugin: 'java'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.code.gson:gson:2.8.1'
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
And this is my root build.gradle:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}
allprojects {
repositories {
jcenter()
google()
}
}
Tested on Android Studio 3.0 with Gradle 4.1.
How to control H2 driver version in Grails/Gradle project?
Having problems with running Grails 3 app with H2 I found this answer: Grails accessing H2 TCP server hangs stating it may be caused by driver version difference.
My IDE reports Grails app uses 1.3.176 version of H2, while my server has 1.4.190. So, I would like to upgrade app's H2, but can't find where it is defined. I sought all project files and found no version definition.
UPDATE
My current build.gradle:
buildscript {
ext {
grailsVersion = project.grailsVersion
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath 'com.bertramlabs.plugins:asset-pipeline-gradle:2.5.0'
classpath "org.grails.plugins:hibernate:4.3.10.5"
}
}
plugins {
id "io.spring.dependency-management" version "0.5.2.RELEASE"
}
version "0.1"
group "multipleparentsgrails"
apply plugin: "spring-boot"
apply plugin: "war"
apply plugin: "asset-pipeline"
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: "org.grails.grails-web"
apply plugin: "org.grails.grails-gsp"
ext {
grailsVersion = project.grailsVersion
gradleWrapperVersion = project.gradleWrapperVersion
}
assets {
minifyJs = true
minifyCss = true
}
repositories {
mavenLocal()
maven { url "https://repo.grails.org/grails/core" }
}
dependencyManagement {
imports {
mavenBom "org.grails:grails-bom:$grailsVersion"
}
applyMavenExclusions false
}
dependencies {
compile "org.springframework.boot:spring-boot-starter-logging"
compile "org.springframework.boot:spring-boot-starter-actuator"
compile "org.springframework.boot:spring-boot-autoconfigure"
compile "org.springframework.boot:spring-boot-starter-tomcat"
compile "org.grails:grails-dependencies"
compile "org.grails:grails-web-boot"
compile "org.grails.plugins:hibernate"
compile "org.grails.plugins:cache"
compile "org.hibernate:hibernate-ehcache"
compile "org.grails.plugins:scaffolding"
runtime "org.grails.plugins:asset-pipeline"
testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
// Note: It is recommended to update to a more robust driver (Chrome, Firefox etc.)
testRuntime 'org.seleniumhq.selenium:selenium-htmlunit-driver:2.44.0'
console "org.grails:grails-console"
}
task wrapper(type: Wrapper) {
gradleVersion = gradleWrapperVersion
}
You should be able to specify it like any other dependency
runtime "com.h2database:h2:1.4.190"
There are many dependencies which may use H2. grails, hibernate, others.
I would go into your project. Let's say it's $HOME/projects/myproj.
1) Do a dependency report. Pipe it into grep so you don't have to wade through a 1,000 line report, and see what versions of H2 are being used.
cd $HOME/projects/myproj
./gradlew dependencies | grep 'H2'
2) Find the highest version number, and then explicitly include this in your build.gradle to force every dependency to use the most current version:
dependencies {
// all the other dependencies
runtime "com.h2database:h2:1.4.190" // where 1.4.190 is the most
// current version. as i
// type this it is 1.4.191
// according to maven central
}