JUnit testing in Android Studio with Kotlin stopped working - junit4

I have a project in Android Studio where I created a java library module. In there I have a Kotlin class which I want to test. When I create the test in Kotlin and try to run it, the IDE keeps telling me:
Process finished with exit code 1
Class not found: "com.example.CacheTest" Empty test suite.
When I create the same test in Java everything works fine.
I created tests in other projects in Kotlin as well and there it works.
I recently update AS to 2.2.2 and use Kotlin 1.0.4
Does anyone have any suggestions?

Do you have this in your build.gradle?
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
}
dependencies {
testCompile 'junit:junit:4.12'
}
Directory structure like:
and a test class:
import org.junit.Assert
import org.junit.Test
class TestCase {
#Test fun someTest(){
Assert.assertFalse(true)
}
}

Related

How to generate Spock reports with Eclipse

How do I generate html reports using the spock reports extension (https://github.com/renatoathaydes/spock-reports). I've added the dependency to my build.gradle file which, as far as I can tell, is the only thing I need to do. But when I run my tests on Eclipse I can't find any report appearing anywhere.
Here's my build.gradle file, spock reports dependency are at the end.
apply plugin: 'java-library'
apply plugin: 'groovy'
repositories {
jcenter()
}
dependencies {
api 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:22.0'
testImplementation 'org.codehaus.groovy:groovy-all:2.4.11'
testImplementation 'org.spockframework:spock-core:1.0-groovy-2.4'
testImplementation 'junit:junit:4.12'
testCompile( 'com.athaydes:spock-reports:1.3.1' ) {
transitive = false // this avoids affecting your version of Groovy/Spock
}
testCompile 'org.slf4j:slf4j-api:1.7.13'
testCompile 'org.slf4j:slf4j-simple:1.7.13'
}
EDIT: The build.gradle file is wrong.
I generated the build with "gradle init --type java-library --test-framework spock" which worked fine, I added some groovy classes and could run tests successfully on eclipse, but it gave me a "Cannot infer Groovy class path because no Groovy Jar was found on class path [...]" error when I tried to use gradle.build.
I changed the Groovy dependency from "testImplementatiuon" to "compile". It made it so that the project could compile and run tests from the command line. This also generated spock reports.
Running tests on eclipse still doesn't generate test reports.
I build a new project from the command line with this build.gradle file:
apply plugin: 'groovy'
apply plugin: 'java-library'
repositories { jcenter() }
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.11'
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
testCompile( 'com.athaydes:spock-reports:1.3.1' ) {
transitive = false // this avoids affecting your version of Groovy/Spock
}
testCompile 'org.slf4j:slf4j-api:1.7.13'
testCompile 'org.slf4j:slf4j-simple:1.7.13'
}
And copied the same groovy files on it. Then imported the project to eclipse. This one works when I run tests from eclipse (it generates spock reports). I still don't know what the problem was exactly but I guess my issue is solved.

How do you run JMH Benchmarks on Scala Code in a Gradle Project using the Gradle Scala Plugin

I am looking for a way to run JMH benchmarks on Scala code in gradle projects. For SBT there is a plugin. How can you do the same if your are using Gradle perhaps using another plugin or writing a task.
You should take a look at JMH Gradle Plugin. The default configuration should allow to write JMH benchmarks in Java that test Scala code.
If you want to write the benchmarks themselves in Scala you would have to add the plugin and dependency manually like this:
apply plugin: 'scala'
sourceSets {
jmh {
scala.srcDir 'src/jmh/scala'
}
}
jmh {
include = ['.*']
jmhVersion = '1.15'
}
dependencies {
compile group: 'org.scala-lang', name: 'scala-library', version: '2.12.1'
}
And then add the benchmark code in src/jmh/scala.

How to setup JUnit testing in Gluon Project with Gradle

I am trying to setup JUnit testing in my Gluon JavaFX Application. I am using the Gluon Eclipse Plugin with Gradle and Java 8.
My build.gradle file looks like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.0-b10'
}
}
apply plugin: 'org.javafxports.jfxmobile'
repositories {
jcenter()
}
dependencies {
compile 'com.gluonhq:ignite-dagger:1.0.0'
compile 'org.elasticsearch:elasticsearch:1.6.0'
compile 'ch.qos.logback:logback-classic:1.1.5'
testCompile 'junit:junit:4.12'
}
mainClassName = 'com.me.MyApplication'
jfxmobile {
android {
manifest = 'src/android/AndroidManifest.xml'
}
ios {
infoPList = file('src/ios/Default-Info.plist')
}
}
Resolving the dependency is no problem, but when running the 'test' task, gradle throws an error like this:
When running gradle with java 8, you must set the path to the old jdk, either with property retrolambda.oldJdk or environment variable JAVA6_HOME/JAVA7_HOME
Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-2.2.1-all.zip'.
I already tried to add the retrolambda plugin to gradle according to the plugin's README on GitHub, but it doesn't work so far. Could someone tell me what to do to configure my Gluon project so that I am able to run my JUnit tests with Gradle?
Some important addidtions:
For the plugin version it says: Gluon Tools 1.0.0.201508201514
I think I fogot to mention that I want to use Dagger dependency injection with Gluon Ignite which might be the real problem in my case as it requires Java 8 and might conflict with javafxports or something else. However, I'm not able to make full sense of the various error messages I've seen.
My tests are empty, but they aren't even run, because it fails before.
Your problem seems like a retroLambda configuration issue. If you go through the configuration page for the plugin, it states that if you don't have an environment variable set for JAVA6_HOME or JAVA7_HOME than you need to explicitly define oldJdk for the plugin to work properly.

Running a gradle jersey 2.0 web app without a web.xml in eclipse

I have a gradle project that I import in eclipse. The project is a web app and I manage to build and deploy it to the tomcat but the jersey REST services are not being loaded and hence I get 404 when trying to reach these services.
I'm using tomcat7 which has support for servlet-api 3.0.
please note that I'm not using a web.xml. I have a class that extends Application and should load the right classes and set the root for the REST services.
Here's my class:
import java.util.Set;
import javax.ws.rs.core.Application;
#javax.ws.rs.ApplicationPath("api")
public class JerseyApplicationConfig extends Application
{
#Override
public Set<Class<?>> getClasses()
{
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
addRestResourceClasses(resources);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources)
{
resources.add(com.cisco.gemini.LoggingRESTService.class);
}
}
The problem is that this class does not get created by jersey and therefore the REST services are not set up.
Thanks,
Gal
Update:
I managed to solve the problem so I'm posting here the solution for everyone's benefit.
Here's my gradle file that got things working. I can now build and run the services on tomcat7 from Eclipse and the jersey servlets load properly.
Note: I also wanted to be able to run the service from command line (for testing purposes) for this reason I applied the jettyEclipse plugin. The jetty plugin that comes with gradle does not support servlet-api 3.0. this isn't working for me yet (because of some conflict on slf4j which I'm using) but it might work for others.
Thanks,
Gal
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'jetty'
apply plugin: 'eclipse-wtp'
repositories {
mavenCentral()
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath (group: 'com.sahlbach.gradle', name: 'gradle-jetty-eclipse-plugin', version: '1.9.+')
}
}
apply plugin: 'jettyEclipse'
dependencies {
compile 'javax.servlet:javax.servlet-api:3.0.1'
compile 'com.sun.jersey:jersey-server:1.18.1'
compile 'com.sun.jersey:jersey-core:1.18.1'
compile 'com.sun.jersey:jersey-json:1.18.1'
compile 'com.sun.jersey:jersey-servlet:1.18.1'
compile 'javax.ws.rs:jsr311-api:1.1.1'
compile fileTree(dir: 'libs', include: '*.jar')
compile 'org.codehaus.jettison:jettison:1.3.5'
}
eclipse {
wtp {
component {
contextPath = 'MyService'
deployName = 'MyService'
facet {
facet name: 'jst.web', version: '3.0'
facet name: 'java', version: '1.7' }
}
}
}
If you want to make it working from command-line you can try to use gretty plugin - https://github.com/akhikhl/gretty - it supports newer versions of Jetty including Servlet 3.0 compatible versions. Or the other way is to make your application 2.5 compatible: add web.xml and define various components here. I'd prefer the first solution.

SBT: Plugin dependencies and project classpath

How does one add a external dependency to a SBT plugin and make it available on both the project and plugin classpath ?:
Specifically I have a simple plugin that should run our TestNG test suites and do some post processing. Here is a simplified version:
import sbt._
import java.util.ArrayList
import Keys._
import org.testng._
object RunTestSuitesPlugin extends Plugin {
lazy val runTestSuites = TaskKey[Unit]("run-test-suites", "runs TestNG test suites")
lazy val testSuites = SettingKey[Seq[String]]("test-suites", "list of test suites to run")
class JavaListWrapper[T](val seq: Seq[T]) {
def toJavaList = seq.foldLeft(new java.util.ArrayList[T](seq.size)) { (al, e) => al.add(e); al }
}
implicit def listToJavaList[T](l: Seq[T]) = new JavaListWrapper(l)
def runTestSuitesTask = runTestSuites <<= (target, streams, testSuites) map {
(targetDirectory, taskStream, suites) =>
import taskStream.log
log.info("running test suites: " + suites)
runSuites(suites)
}
private def runSuites(testSuites: Seq[String]) = {
var tester = new TestNG
tester.setTestSuites(testSuites.toJavaList)
tester.run()
}
def testSuiteSettings = {
inConfig(Compile)(Seq(
runTestSuitesTask,
testSuites := Seq("testsuites/mysuite.xml"),
libraryDependencies += "org.testng" % "testng" % "5.14"))
}
}
The problem is that when I add this plugin to a project and run it with run-test-suites then it fails with java.lang.NoClassDefFoundError: org/testng/TestNG even though show full-classpath shows that testng.jar is on the classpath.
So somehow the classpath used when executing the plugin differs from the one in my project, so how do I make a plugin dependency appear in both places ?
I'll try an answer, but I'm not very familiar with the inner details of sbt.
Normally, the path for the build system (as opposed to your program) is under project, as explained here. That would typically be in a project/plugins.sbt. Sounds right, as there is no reason that the application you develop should be concerned by what libraries your build system uses, nor the other way round.
When your plugin run the application code, that may not be so simple and there could well be classpath/classloader issues. I'm not sure that it will work. Normally, your plugin should implement a testing Framework rather than define its own task. Documentation of testing for sbt is limited.
A testing framework should implement org.scalatools.testing.Framework, in test-interface. Your build will take it into account after you add
testFrameworks += new TestFramework("full.class.name")
When you run the normal test command, it let every framework recognize the test classes it deals with (two criteria available: extending some base class or having some annotation) and run them. The framework run in the build, it is given a class loader to access the application code.
You may have a look at the framework implementation for junit (shipped with sbt). Also there is a TestNG implementation. I don't know it, according to its doc, it is a little bit unorthodox, hopefully it will work for you.
The error was fixed by adding TestNG directly to unmanagedJars in Compile in the project that uses the plugin.
I have not found any resources explaining the structure of the SBT class path during plugin execution so any attempt at explaining why this step is necessary will be greatly appreciated.