The title pretty much describes it. If I clean everything, Spek specs don't run at all with "Could not load class with name". If I run compileTestJava, it runs OK but I have to manually recompile every time the test code changes... Using gradle and basic configuration from docs.
By any chance is that an android project (https://github.com/raniejade/spek-idea-plugin/issues/6)?
Well, I have a multi-module project. One of the modules is and Android one. The module I use Spek for is not an Android one, it's a sibling module. And I don't have to rebuild the project sources - these are automatically recompiled - I have to to rebuild only the spek file itself. So, if the spek file hasn't changed, but I changed the code under test - the code under test will be automatically compiled. But if I change the spek - no such luck.
Can you provide more details about the structure? If possible a build script :) Typical gradle java-modules should not need any recompilation (IDE will do it for you)
I know IDEA does that for me. And it works for all the other cases. I've changed my Spek code for regular JUnit for the same module and it worked just fine. Wil provde structure details a bit later, when I am behind my computer.
@raniejade So, here it goes.
The root gradle file:
buildscript {
ext {
kotlin_version = '1.1.0-beta-38'
androidSdk = 25
buildTools = '25.0.1'
androidSupport = '25.1.1'
androidGms = '10.0.1'
anko = '0.9.1'
}
repositories {
jcenter()
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }
// latest is still not at jcenter: https://jcenter.bintray.com/org/jetbrains/anko/anko-sdk15/
maven { url 'https://dl.bintray.com/jetbrains/anko' }
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap-1.1' }
maven { url 'http://dl.bintray.com/jetbrains/spek' }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'io.realm:realm-gradle-plugin:2.3.0-SNAPSHOT'
classpath 'com.google.gms:google-services:3.0.0'
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3'
}
}
allprojects {
repositories {
jcenter()
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }
maven { url 'https://dl.bintray.com/jetbrains/anko' }
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap-1.1' }
maven { url 'http://dl.bintray.com/jetbrains/spek' }
}
}
...
Then I have the gradle build file for app and the module X (ther one in question, the one using Spek). Structured like so:
root
| gradle
|- app\gradle
|- X\gradle
Does it make sense to post the app gradle config? The module is not used in any direct way in module X...
The gradle build file for module X is:
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'
apply plugin: 'org.junit.platform.gradle.plugin'
compileJava {
// this is needed for classes generated by Dagger - otherwise they are compiled to Java 8
targetCompatibility JavaVersion.VERSION_1_7.toString()
sourceCompatibility JavaVersion.VERSION_1_7.toString()
}
junitPlatform {
filters {
engines {
include 'spek'
}
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
// Dagger 2 stuff
compile 'com.google.dagger:dagger:2.8'
kapt 'com.google.dagger:dagger-compiler:2.8'
compile 'com.squareup.okhttp:okhttp:2.7.5'
compile 'com.github.salomonbrys.kotson:kotson:2.3.0'
compile 'com.github.debop:koda-time:1.0.0'
testCompile 'org.assertj:assertj-core:3.6.2'
// Spek
testCompile ('org.jetbrains.spek:spek-api:1.1.0-beta3') {
exclude group: 'org.jetbrains.kotlin'
}
testRuntime ('org.jetbrains.spek:spek-junit-platform-engine:1.1.0-beta3') {
exclude group: 'org.junit.platform'
exclude group: 'org.jetbrains.kotlin'
}
}
The Spek file itself:
...
// FIXME: Right now the only way to make this work is to run compileTestJava before...
class XSpek: Spek({
describe("X service") {
val x = X()
on("getSomeThings") {
val someThings= x.getSomeThings()
it("should return some things") {
assertThat(someThings).isNotEmpty()
}
}
}
})
Also, I am using IntelliJ IDEA 2016.3.4 Ultimate and the latest Spek plug-in
Thanks, I'll take a look when I get home.
By the way, this might be of no importance, but I've noticed this:

Module 'torpedo' (the one I tried using Spek for) is marked as Android-Gradle by IDEA, although it has absolutely no relation to Android.
On the picture miki-android module is root, app is Android app and torpedo is the module that I wanted to use Spek with.
@NeverwinterMoon can you list the tasks for torpedo?
@raniejade Here you go:
------------------------------------------------------------
All tasks runnable from project :torpedo
------------------------------------------------------------
Build tasks
-----------
assemble - Assembles the outputs of this project. [jar]
build - Assembles and tests this project. [assemble, check]
buildDependents - Assembles and tests this project and all projects that depend on it. [build]
buildNeeded - Assembles and tests this project and all projects it depends on. [build]
classes - Assembles main classes.
compileJava - Compiles main Java source.
compileKotlin - Compiles the source set 'main'.kotlin.
kaptKotlin
processResources - Processes main resources.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes. [classes]
testClasses - Assembles test classes. [classes]
compileTestJava - Compiles test Java source.
compileTestKotlin - Compiles the source set 'test'.kotlin.
kaptTestKotlin
processTestResources - Processes test resources.
Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code. [classes]
Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in project ':torpedo'.
components - Displays the components produced by project ':torpedo'. [incubating]
dependencies - Displays all dependencies declared in project ':torpedo'.
dependencyInsight - Displays the insight into a specific dependency in project ':torpedo'.
help - Displays a help message.
model - Displays the configuration model of project ':torpedo'. [incubating]
projects - Displays the sub-projects of project ':torpedo'.
properties - Displays the properties of project ':torpedo'.
tasks - Displays the tasks runnable from project ':torpedo'.
Verification tasks
------------------
check - Runs all checks. [test]
test - Runs the unit tests. [classes, testClasses]
Other tasks
-----------
copyMainKotlinClasses
copyTestKotlinClasses
I replicated the issue, it's a similar problem to https://github.com/raniejade/spek-idea-plugin/issues/6. Since you're using intellij IDEA (and not AS) have you turned on "separate module per source set" (when importing the gradle project)?
I am pretty sure I went with all default settings when importing. Is it on by default then? Should I try re-importing the project with this off?
I'm not sure, I think it's disabled by default.
OK, checked it out - the option "Create separate module per source set" is on by default. So I did use it to start with, as I haven't changed any default setting on project import.
But I recently discovered another issue. In a different project I am involved this, I got the same problem (tests are not automatically compiled and saying class not found if I try to launch from IDE context) with Scala and Scalatest. So, I moved one minor version of IntelliJ IDEA down (to 2016.2.5 from 2016.3.4) and at least the Scalatest things started working.
Going to try Spek with 2016.2.5 later on.
Tried in 2016.2.5 and 2016.3.4 with "Create separate module per source set" and without. Still same problem with Spek.
Will take a look. Have a feeling that this has something to do with the android plugin.
@NeverwinterMoon have you tried to delegate run actions to Gradle in IntelliJ IDEA 2016.3+ ?
@Adambl4 Just did that - worked exactly the same in the end and gave me "Could not load class with name". Unless I manually run "compileTestJava".
This doesn't solve the issue but it can be a workaround until the real issue is fixed. If you add the task to compile the tests in your Configuration settings, the running of the tests will never result in error and it will fail when it is expected to.
Solution for me was to run the testClasses task before the test. This can be added to the run configuration to do it automatically.
Most helpful comment
@Adambl4 Just did that - worked exactly the same in the end and gave me "Could not load class with name". Unless I manually run "compileTestJava".