For some reason, I'm really having a hard time getting display names to actually be respected in JUnit 5 with Kotlin.
Here's a test file I created for the purpose of example:
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
#DisplayName("Example Test")
class ExampleTest {
#Test
#DisplayName("test name")
fun myTest() {
Assertions.assertThat(false).isTrue()
}
}
But instead of these names being used, it's showing the actual class/method name as if they weren't annotated with #DisplayName at all. Here's the output from ./gradlew test:
project.path.ExampleTest > myTest() FAILED
org.opentest4j.AssertionFailedError at ExampleTest.kt:12
25 tests completed, 1 failed
> Task :test FAILED
I keep thinking there must be something wrong with my Gradle configuration, but the setup is pretty simple so I don't know what I could be doing wrong.
Here's my build.gradle.kts:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.3.60"
id("org.jmailen.kotlinter") version "2.1.2"
id("org.jetbrains.kotlin.plugin.serialization") version "1.3.60"
}
version = "0.1"
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.14.0")
testImplementation("org.junit.jupiter:junit-jupiter:5.5.2")
testImplementation("org.assertj:assertj-core:3.14.0")
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
tasks.test {
useJUnitPlatform()
}
Really scratching my head at this point, so any ideas would be appreciated. It's not using the names I give to my dynamic tests either (in conjunction with #TestFactory), which is particularly annoying.
Finally figured this out. It was an IntelliJ configuration issue. (The display names are never displayed in the command line anyway apparently.)
Turns out I had it configured to use the Gradle test runner instead of the IntelliJ one, which doesn't show custom display names. The solution was to go into IntelliJ settings -> Build, Execution, Deployment -> Gradle and under "Run tests using" select "IntelliJ IDEA"
Thanks go to JBNizet for pointing out that display names are supposed to show up in the HTML test report that Gradle generates but not in the command line, which helped me determine that this was an IntelliJ-specific issue.
It's because IntelliJ uses gradle test runner by default and it does not allow custom display name.
The solution is really simple you just need to change the default test runner from "Gradle" to "IntelliJ Idea".
Preferences
Build, Execution, Deployment
Build Tools
Gradle
Run tests using = IntelliJ Idea
How to change default test runner in IntelliJ Idea
Truth is, for me, after I undid the changes suggested on this post and selected the check icon to the right of the green play button, the description of the tests started to show.
I managed to get it working in surefirereports by using the configuration
in
https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html#Surefire_Extensions_and_Reports_Configuration_for_.40DisplayName
it displays output like this
"name" is from #DisplayName
Related
I would like to enable by default junit extension autoDetection on my project
if i understantd I should use -Djunit.jupiter.extensions.autodetection.enabled=true when i launch my build.
But i want to enable by default without extra parameters in the command line and in all my subModule.
I thing something like that should be good :
allprojects {
test {
//put an option here
useJUnitPlatform()
}
}
But i've no clue of which option to put in my build.gradle.
In Gradle you can use the system properties extension:
test {
// ...
systemProperty("junit.jupiter.extensions.autodetection.enabled", true)
}
See JUnit 5 User Guide for further details.
Let's assume my build.gradle file contains task generateSources which as name suggests generates additional java files. It's easy to ensure that generateSources is executed before compileJava: compileJava.dependsOn generateSources. How can I make sure generateSources is called when importing project into Intellij Idea as well?
To elaborate on #vladimir-sitnikov's answer: I added the idea-ext-plugin to my root project:
apply plugin: 'org.jetbrains.gradle.plugin.idea-ext'
// ...
buildscript {
dependencies {
classpath "org.jetbrains.gradle.plugin.idea-ext:org.jetbrains.gradle.plugin.idea-ext.gradle.plugin:0.7"
}
}
Because without that I wasn't able to use it in my sub project, but now it works like this:
idea.project.settings.taskTriggers {
beforeSync tasks.getByName("generateSources")
}
Adding the plugin to the sub-project only didn't do it.
Note: The plugin's documentation is kind of limited, but in "DSL spec v. 0.2" is stated
beforeSync - before each Gradle project sync. Will NOT be executed on initial import
Didn't try that, but it works with existing projects.
This can be done via id("org.jetbrains.gradle.plugin.idea-ext") plugin (https://github.com/JetBrains/gradle-idea-ext-plugin).
See sample code in Gradle sources: https://github.com/gradle/gradle/blob/135fb4751faf2736c231636e8a2a92d47706a3b9/buildSrc/subprojects/ide/src/main/kotlin/org/gradle/gradlebuild/ide/IdePlugin.kt#L147
You can set the task in Gradle tool window: Execute Before Sync:
I try to run my BDD scripts via gradle getting the following error message after updating IntelliJ to 2016.2
No implementation for net.thucydides.core.webdriver.WebdriverManager was bound.
while locating net.thucydides.core.webdriver.WebdriverManager
The code raising the error is this:
#Before
public void jeffCanBrowseTheWeb() {
givenThat(jeff).can(BrowseTheWeb.with(theBrowser));
}
The binaries to the browser are linked like this:
test {
System.setProperty("webdriver.chrome.driver","D:\\lib\\chromedriver.exe")
/* Pass all system properties: */
systemProperties System.getProperties()}
The compile dependencies for selenium-java are pointing to the version '2.53.1'
The gradle command: clean test aggregate
I cannot figure out what is wrong since I did nothing else but updating the IDE. Maybe someone has a hint?
Thanks in advance,
Martin
I ran into the same problem when I was following the example in the article mentioned in your comment. In my case (without using an IDE) it seemed to be a out-of-date dependency (which was renamed).
Try changing the dependency 'net.serenity-bdd:browse-the-web' to 'net.serenity-bdd:serenity-screenplay-webdriver' in build.gradle.
I'm trying to add a .dll file to the "java.library.path" system property via gradle on my Spring Boot project. I'm using Gradle 2.1 on STS.
This is the small piece of groove code within my build.gradle:
tasks.withType(JavaCompile) {
systemProperty "java.library.path", file("./src/main/resources/META-INF/opencv-2.4.9/windows_bin/x64")
}
And I'm getting the following error:
Could not find method systemProperty() for arguments [java.library.path, D:\GitHub\TFG_1\GuiaTV\src\main\resources\META-INF\opencv-2.4.9\windows_bin\x64] on root project 'GuiaTV'
That path does exists, so I don't know where the problem is.
Any help? Thank you!
UPDATE 1:
#Amnon Shochot
What I try to do is to add a native library (.dll) to the project. I took the idea from some sites (for example, http://zouxifeng.github.io/2014/07/17/add-system-property-to-spring-boot.html, https://github.com/cjstehno/coffeaelectronica/wiki/Going-Native-with-Gradle).
The first one is using what you suggested:
tasks.withType(JavaExec) {
systemProperty "java.library.path", file("./libs")
}
The second one is using:
run {
systemProperty 'java.library.path', file( 'build/natives/windows' )
}
None of them are working for me.
The first one (with JavaExec) is failing gradle test throwing:
java.lang.UnsatisfiedLinkError: no opencv_java249 in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1865)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
If you follow the trace, it's crashing at runtime in sentence: System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
And the second one is failing on gradle build with the following message:
Could not find method run() for arguments [build_24sfpo0st6dokeq7fn3ad7r34$_run_closure7#2652c3da] on root project 'GuiaTV'.
Luckily you know exactly what I try to achieve and you can solve my problem.
Thank you for your interest!
UPDATE 2:
Finally, I ended up adding these lines to my build.gradle script:
// The following makes "gradle build", "gradle test" work
test {
jvmArgs = ['-Djava.library.path=./src/main/resources/META-INF/opencv-2.4.9/windows_bin/x64']
}
// Thw following makes "gradle run" work
run {
jvmArgs = ['-Djava.library.path=./src/main/resources/META-INF/opencv-2.4.9/windows_bin/x64']
}
By the way, I'm also using "spring-boot" gradle plugin. That's where the run task comes from.
So, I can execute "gradle build", "gradle test" and "gradle run" sucessfully. This is, that native library is correctly added.
However, since I'm also using "eclipse" gradle plugin, I would like to add the native library simply by executing "gradle eclipse". Instead, I must create the library on Eclipse manually, and add it to my project.
Thank you #Amnon for your collaboration. I'll be posting a new solution in the case I found it.
The problem is that you do not set the context for the systemProperty method thus Gradle tries to locate it in the project object where it does not exist which is the reason for the error you got.
If you wanted to apply this configuration for all tasks of type JavaCompile your code should have been looked like:
tasks.withType(JavaCompile) { JavaCompile t ->
t.systemProperty "java.library.path", file("./src/main/resources/META-INF/opencv-2.4.9/windows_bin/x64")
}
However, the JavaCompile task type also does not contain a systemProperty so this code wouldn't work either.
You can define CompileOptions for a JavaCompile task using its options property, i.e.:
tasks.withType(JavaCompile) { JavaCompile t ->
t.options "java.library.path", file("./src/main/resources/META-INF/opencv-2.4.9/windows_bin/x64")
}
However, I'm not sure whether you can define this specific system property.
One last note - the systemProperty method does exist for tasks of type JavaExec in case that this is what you intended to do.
Me and my team are working on a project with a lot of modules. We are using gradle for the project and everyone is new to gradle. We have a Main parent project i.e, parent build with the details of project dependencies. We want to add the integration_test task configuration to all the modules so that we can call the command gradle integration_test. So is there any way or concept of writing the configuration in the main module and make the child projects import the same configuration.
FYI: I tried it by directly adding it to the main project but got an error saying the classpath for the files which I specified does not exists. Any help or thought would be appreciated. Thanks in advance.
Is there a particular reason to split "integration tests" from the standard test task?
If so, you can run the same script for all subprojects from the main project's build file: https://docs.gradle.org/current/userguide/multi_project_builds.html#sec:subproject_configuration
For instance:
subprojects {
task integrationTest {
// whatever you need
}
}
I am note sure if this is what you talk about in the last paragraph. If so, please attache the error message you get.
It is also possible to "import" some configuration by subprojects, but the above definition is better in most scenarios.