"No tests were found" with Junit 5 and IntelliJ - intellij-idea

Q&A-Style question as the existing questions don't match the simple typo I made here:
Goal
Execute simple JUnit tests via the IntelliJ IDE using the UI (right-click: run test)
Problem
IntelliJ tells that "no tests were found"
Code
import org.junit.jupiter.api.Test;
public class Test {
#Test
private void testAMethod() { (...) }
}

change to
public void testAMethod() { (...) }
According to Junit5 document
Test classes, test methods, and lifecycle methods are not required to
be public, but they must not be private.
https://junit.org/junit5/docs/current/user-guide/

Another possibility of this error message is when using a non-void return type. Switch to void and it will work.

I know it is not relevant but if someone else is facing issue like me and none of the above solution works for you then check your target folder does it have classes files in this. I was having this same issue then I checked my target/classes folder and found it empty. Then I build my mvn again using mvn clean install and then it work like charm.
when build was success but class file was not generated as I was using DskipTests

The issue was a simply typo - the test was not visible from outside:
#Test
// Before
private void testAMethod() { (...)
// After
void testAMethod() { (...)

Junit in IntelliJ won't work on Java 10 or above. Try downgrading the project SDK to Java 8 and the tests will work like a charm.

Related

How can I navigate to Lombok generated implementations

I'm wondering if it would be possible to navigate directly to the methods generated by Lombok using Intellij IDEA.
For instance, for this given example:
#Builder
public class AClass {
private String body;
}
trying to go to the implementation of AClass#builder in an instruction like AClass.builder().build() results in Intellij navigating to AClass, instead of taking me to the real compiled method, which is generated under target directory
Please feel free to create an issue here: https://youtrack.jetbrains.com/issues/IDEA
Currently you can't change the behavior.

IntelliJ autocompletion plugin development for Gherkin steps

I've recently started working on IntelliJ plugin development and got stuck a bit with autocompletion implementation.
Don't want to go deep into the details of plugins idea, but I would like to pass some strings through autocompletion into the Gherkin steps.
If Gherkin plugin in IntelliJ is disabled (file is expressed as plain text), there are no problems for passing 'Message' value to step through autocompletion. If plugin is enabled, it does not work.
Example of scenario:
Scenario: Successful message appearance
When user access home page
When user sees message "Message"
Example of completion contributor:
public SimpleCompletionContributor() {
extend(CompletionType.BASIC, PlatformPatterns.psiElement(),
new CompletionProvider<>() {
public void addCompletions(#NotNull CompletionParameters parameters,
#NotNull ProcessingContext context,
#NotNull CompletionResultSet resultSet) {
resultSet.addElement(LookupElementBuilder.create("Message"));
}
});
}
Structure of entire document observed via PsiPlugin:
As I understand, Gherkin plugin changes interpretation of file, and currently I can do nothing about it. What I am missing?
Thanks!

Intellij Idea does not detect and underline non-existing methods in the editor pane

In the idea editor and my java project , idea does not detect non-existing methods and underline in the editor pane, so i could not use alt + enter to generate new methods.
Example screenshot is below :
BTW : powersave mode is disabled, in the project setting sources are selected.
Project window is seen below:
I created demo maven project in idea, still the problem continues.
I created DemoNew Class.
public class DemoNew {
}
Then i created DemoImpl class:
public class DemoImpl {
public static void main(String[] args) {
DemoNew demoObject = new DemoNew();
demoObject.ssss(); // idea does not detect this non-existing method.
}
}
As seen above, idea does not detect the non existing ssss method in DemoNew class.
I uploaded demo project and my settings in intellij idea.
demo project and settings
After compiling project, still idea does not underline non-existing method with red color in the editor pane.
JetBrains Team answered the question
Looks like JDK uses wrong locale:
https://youtrack.jetbrains.com/issue/IDEA-190718 Please try to add
"-Dsun.locale.formatasdefault=true" option to JVM settings.
I added that parameters from intellij idea / help / edit custom vm options.
And parameters are seen below.
-Dsun.locale.formatasdefault=true
-Duser.language=en
-Duser.region=US
-Dfile.encoding=UTF8
I added last four items in the file.
That fixed the problem.

How to select a different module to run when you click the Run button in IntelliJ IDEA?

In my IntelliJ IDEA project, I have 3 modules written in Kotlin:
An HTTP Servlet one.
A desktop swing application; and
A library that contains contracts that the above two listed projects share to talk to each other.
When I click the Run button, it starts the Tomcat server and loads up my servlet project. That is because, and I am guessing here, the new project creation template inside of the IDE created a new Run Configuration for the entire project and it is defined in this run configuration that it must start the module that has the servlet inside it.
Now that the servlet runs fine, I'd like to also run the desktop application written using Swing.
How do I do that? I've done this once before but I have forgotten how I did it.
Do I have to define a new Run Configuation? I tried that this way:
I selected Kotlin from the left pane titled Add New Configuration and specified the name of the class that had the main function, and also the name of the module that had this class.
Here is the source code of my main class.
package bookyard.client;
import javax.swing.SwingUtilities;
public class Program {
public fun main(args : Array<String>) {
SwingUtilities.invokeLater(LoginDialogEventLoop());
}
}
But when I click the Run button after choosing that run configuration's name, the process reports an error that suggests that the class name I specified as having the main function actually does not have the main function, which I am not sure why that is.
The main method needs to be static, and the method you have declared is not. In Kotlin, you can either declare main as a top-level function (outside of a class), or, if you want to keep it inside the class, use the following syntax:
class Program {
companion object {
#JvmStatic fun main(args: Array<String) { ... }
}
}

Ignore all tests from specflow using [BeforeTestRun] hook

My project has 2 default configurations (Debug and Release) and a custom one called UITest.
When the UITest config is selected it changes ioc to use an in memory verson of my database which is perfect for my ui tests.
I am using NUnit and resharper to run my tests and would like to only run the ui tests when my UITest configuration is active.
I decided to go with the [BeforeTestRun] hook to achieve this but I cant get it to ignore the tests.
Throwing an exception will prevent all the tests from running but they show as a fail, i would prefer to have them show as pending or ignored. Is this possible?
[BeforeTestRun]
public static void BeforeTests()
{
#if !UITest
throw new Exception("abort!");
#endif
}
Addendum
If I change it to this:
[BeforeTestRun]
public static void BeforeTests()
{
#if !UITest
Assert.Ignore();
#endif
}
Then each of the scenarios will show as ignored but the feature will fail.
The BeforeTestRun hook is the method that runs only once, before the run starts. If you want to skip this whole testrun, you can use Environment.Exit(); to exit the run.
In MSTest the tests will show up as not run, but I am not sure how NUnit or Resharper handles this.