JUnit - Every annotation works except the #Test annotation - maven

I've cloned the sample project which JetBrains uses for demonstration purposes (https://github.com/JetBrains/intellij-samples.git) from GitHub since I had an issue with JUnit and wanted to see whether those issues persist when using an official project (to make sure that I haven't messed something up). Unfortunately the issue persists and after hours of research on Google, StackOverflow or last but not least the JetBrains Support Board I can't manage to get this issue resolved.
I can't get JUnit to work with the sample project as well as my own projects. Whenever I add JUnit to my classpath (using Maven), it imports all the required libraries. After that, literally everything works perfectly fine except for the #Test annotation. I can use other annotations like org.junit.jupiter.api.Disabled (#Disabled) or org.junit.jupiter.api.ParameterizedTest (#ParameterizedTest) but whenever I try to use the org.junit.jupiter.api.Test (#Test) annotation I get a "Cannot resolve symbol 'Test'" error.
When hovering above the #Test annotation it suggests to add JUnit4 or JUnit5 to the classpath. If I select that option I still get the same error and when hovering above the #Test annotation afterwards it still suggests the same thing. I've already tried to do "Maven > Reload Project", "File > Invalidate Caches / Restart", reinstalling IntelliJ Ultimate and importing the JUnit API manually by adding it as an external Java library to the project without using Maven or Gradle. I also made sure that the directory in which I am trying to use the #Test annotation in is a "Test Source Folder". However, once again, all the other JUnit annotations work perfectly fine. Only #Test is not working and throws a "Cannot resolve symbol 'Test'" error.
I am using the following versions:
IntelliJ: 2020.3.2 (Ultimate Edition) [latest]
Java: 1.8 "1.8.0_144"
JUnit: 5.7.1 (But I've also tried to use plenty of other versions [from 4.0 to 5.8] instead without success.)
OS: Windows 10 Home 64 bit
I've tried setting up JUnit with Eclipse using Maven and it immediately worked so I am not sure whether this has something to do with IntelliJ specifically. At this point I start to run out of ideas as to why everything except the #Test annotation is working perfectly fine. I've already tried plenty of suggestions from aforementioned sites, hence I really hope that we can get this issue resolved here.
I've appended a screenshot of one of the classes where this issue occurs. All of the code as well as the pom.xml file / etc is the same as in https://github.com/JetBrains/intellij-samples. I haven't modified any of the files locally and only tried "Maven > Reload Project" as well as "File > Invalidate Caches / Restart" with the sample project. If you need any further info in order to have a look at this issue make sure to let me know.
Thanks in advance.
Max
Attachments:
Screenshot of test/com.jetbrains.code.JavaAt25Test

Please see if deleting the system directory helps while the IDE is not running. It looks like file system cache related issue. Should be fixed in 2021.1 release.
The issue is not reproducible in 2020.3.2 with the clean system directory:

Related

How does IntelliJ decide what bytecode (classes) to use when running a maven app?

Questions
My questions are:
(Q) When running an app in the IntelliJ debugger, how does IntelliJ decide which bytecode to use ?
(Q) Does IntelliJ use whatever maven build plugin is being used or simulate it?
(Q) If multiple conflicting versions of code are being used (see mvn dependency:tree -Dverbose), how does IntelliJ decide which version of the code to use?
(Q) Where is the JAR file that IntelliJ creates so I could inspect it to see what versions of classes were used?
(Q) Is the process of selecting classes to use any different when running vs debugging?
Note: This question is being directed to IntelliJ's support staff as well as the SO community.
Background
I have an app that has been very problematic with IntelliJ with regards to picking the desired classes when running the application. 
The app is a Java Executable Jar that has been packaged with the maven-assembly-plugin.  It is not a SpringBoot application.
The problem stems from IntelliJ picking different class files than the maven-assembly-plugin which copies all of these classes (from all of the dependent jar files) into a single jar file. Naturally, only one version of a class can exist in the jar app (I believe the first class copied wins and the others are ignored).
IntelliJ (AFAIK) has no way of knowing what version of what class will be put into the JAR without knowing how the maven-assembly-plugin is implemented; So I'm looking for this answer.
Understanding the Question
To explain, see the example I created to understand how the maven-shade-plugin works. See Is there a sample project in github that shows how to use the maven-shade-plugin?. It's not a perfect fit for my problem/question, but it is in the public domain and it's close enough.
I'll frame my question based on the helloworld example. The helloworld.pom depends directly on loglib:2.0.0 so I would expect IntelliJ to select and use that version when compiling and running the application.  
The problem (code changes not being picked up)
The problem I ran into was when I changed the helloworld/pom.xml file to depend on loglib:2.1.0-SNAPSHOT and IntelliJ did not pick up the code changes and used a previous version of the dependency even though the source code was loaded inside of IntelliJ and the loglib/pom.xml file was changed to be version 2.1.0-SNAPSHOT.
The application is run by specifying the main class of the application com.steranka.play.HelloWorldApp. The Run/Debug configuration is:
NOTE: I've done this exact thing many/many times and it's always worked, so I was puzzled. I also tried to reproduce the problem with a sample example and could not reproduce it. IntelliJ worked flawlessly picking up changes as it has always done in the past.
Trying to fix the problem (bytecode not matching source code)
I went through the list of things I've done in the past but after they were all completed, the problem remained.  The list of steps I followed included:
(1) Rebuilt the project (which included helloworld and loglib packages). Rebuild was done inside of IntelliJ. Nope.
(2) Verify that the correct maven dependency loglib:2.1.0-SNAPSHOT was being used in helloworld module, and that it shows up in the Maven Tools dependency list. 
(3) Clicked Reload all Maven Projects - run from Maven Tool. 
After IntelliJ restart, I made a change to a log.info() statement in the loglib:2.1.0-SNAPSHOT module ran my program and my changes to the log statement did not appear, only the previous version of the log message appeared.
This is the crux of the problem, and reason for my question.  Where is IntelliJ getting the bytecode that it is running? Is it from loglib/target/classes/* directory (this is my belief), or from maven .m2/repository/ ... / loglib:2.0.0-SNAPSHOT.
(3) I put a breakpoint on the log statement (that I changed) and ran the app in Debug mode and IntelliJ stopped on the log statement when I stepped over the statement the wrong log message was printed.  This is when I knew the bytecode IntelliJ was executing did not match the source code.
Detailed steps that reproduced the problem
To be clear I'll give timestamp versions because I don't know exactly what POM versions were being used:
T1: Starting with line that says: log.info("orig message");
T2: I changed the message to log.info("orig message - change1");
T3: IntelliJ did not pick up my change and log/console shows orig message
T4: I added some lines to the source code (multiple log statements)
     log.info("this is strange")
     log.info("why isn't this working")
T5: I put a breakpoint on the line why isn't this working and the breakpoint symbol appeared
T6: However, when I ran the program and IntelliJ stopped at the first breakpoint (orig message - change1) and when it stopped the 2nd breakpoint symbol was greyed out (indicating that the debugger could not stop on that line). 
T7: I stepped and IntelliJ was obviously running different code because the original message appeared instead of the changed message.
More Questions
(Q) When the IntelliJ debugger shows code that doesn't match what is being actually executed, how can I track down what code is actually being used? The answer to my previous questions should answer this.
The observable things I know to look at are:
The Project Settings modules.
The maven tool modules list and the dependencies.
The External Libraries list in the Project Tool.
The target/.jar and target/.classes files.
Settings - java compiler used, maven used,
Common Reasons for this problem (bytecode not matching source code in debugger)
Reason 1: The most common reason for the above problem (in my past) has been making a code change in some module (say loglib when the loglib/pom.xml is at version 2.1.0-SNAPSHOT) and forgetting to update helloworld/pom.xml to refer to the -SNAPSHOT version. In my case I performed this check.
Reason 2: Years ago debuggers did not automatically compile code before the code was run, and this let to the problem whereby the bytecode being used matched the last time the file was compiled. The fix was/is simple, recompile the code and update the *.class file contains your code changes and the debugger code matches the bytecode that is being executed.
How to Reproduce the problem
The maven-shade-example (referred to above) can be used to reproduce the problem where the code shown in the debugger is not used at runtime.
git clone git#github.com:steranka/maven-shade-example.git
cd maven-shade-example
git checkout feature/fixing-problem-another-way
mvn clean install
java -jar helloworld\target\helloworld-1.0.0.jar
The output is:
Hello World!
What's up, Sam
Hello, Sam
Goodbye, Sam
But if you try to open this project inside IntelliJ and build and run it, you'll get an error. To run this, I right clicked on the hello word application and selected Run HelloWorldApp.main() (or Debug).
Hello World!
What's up, Sam
Exception in thread "main" java.lang.NoSuchMethodError: 'java.lang.String com.steranka.play.LogIt.sayHello(java.lang.String)'
at com.steranka.play.GoodFeature.sayGoodbye(GoodFeature.java:6)
at com.steranka.play.HelloWorldApp.main(HelloWorldApp.java:15)
What makes this un-nerving is the debugger jumps to the correct code if you click on the method, it just doesn't use that method at runtime.
The runtime configuration inside IntelliJ is:
#CrazyCoder points out that IntelliJ will run the JAR file created using mvn clean install in the helloworld\target\helloworld-1.0.0.jar if you create a run configuration that says to use Java.
When I created a run configuration to use the Jar file, then IntelliJ's debugger properly executed the application and stepped into the correct files. That configuration looks like this:
When I tried this solution on the actual code causing problems, it also worked. Strangely enough (to me) after switching to the JAR configuration and successfully running, I switched back to my Main class configuration and it also worked! So toggling between the two configurations might work for others as it did for me.
#CrazyCoder also points out that there is an outstanding bug/feature request with IntelliJ that asks for better support of the maven-shade-plugin. See https://youtrack.jetbrains.com/issue/IDEA-266746
#Thorbjørn Ravn Andersen states that IntelliJ emulates Maven so in general the behavior between building and running inside IntelliJ should match what happens with Maven.

Unable to find Groovy method when building Rhino with Intellij IDEA

While trying to build https://github.com/mozilla/rhino, I'm getting Unable to find method on org.codehaus.groovy.runtime.StringGroovyMethods.
so the build fails, and I'm unable to test Rhino built from source code, which is the goal.
Steps Taken
From the opening screen in IntelliJ, I check out the project:
I get this error Unable to find method ''java.lang.String org.codehaus.groovy.runtime.StringGroovyMethods.capitalize(java.lang.String)'' right away, which I have not been able to fix:
There are two suggestions in the above Re-download dependencies and sync project and Stop Gradle build processes. I have tried both with no improvement.
When I look at the build.gradle file, I see another suggestion ...configure Gradle wrapper to use distribution with sources.... This also does not get past the missing Groovy method problem.
I have looked on S.O. for similar issues and tried various things under File > Settings > Build, Execution, Deployment, but I realized I'm over my head since, conceptually, I'm not sure what I need and where that would go in the settings.
The command line items from the readme work as expected, but going back to File > Build still fails (added after tim_yates comment).
What changes are required to the IDE or to the build definitions to allow Mozilla Rhino to build properly?
I installed the latest (2022.1) version of IntelliJ IDEA Community Edition. During the install, it asked if you wanted this IDE to be associated with Groovy file types, and I answered in the affirmative.
On this new install, the Unable to find method on org.codehaus.groovy.runtime.StringGroovyMethodserror did not appear, so the problem was solved by upgrading the IDE (and also that required updating GIT).

Inconsistent run of springboot application when run in Eclipse

I have observed an inconsistent way properties are being read in Eclipse. I have a very simple Springboot web project with typical property files. Here is my project layout:
Notice the two property files: testapplication.properties and application.properties. They are identical at this point, the intention is to use them for test and non test environments.
When I try to run this application in Eclipse, I am getting an error about missing expected property values, for example:
Could not resolve placeholder 'min.thread.count' in value "${min.thread.count}"
When I run the same setup using gradle's bootRun task, it works fine.
When I run the same setup in InteliJ it works fine.
If I rename the testapplication.properties to application.properties the application runs fine in Eclipse. As such it is using property form test folder.
In addition, I am pretty sure when I started Eclipse this morning I was able to run the application with a proper application.properties and testapplication.properties files. I was working on a unit test and renamed the testapplication.properties to application.properties, did some work, then renamed it back to testapplication.properties the application refused to start. I have attempted to replicate it: shut down eclipse with two different property files (application and testapplication), start it again and run application. However, at this moment I have the same issue (complaining about missing property value).
As you can see this i weird behaviour. As it stands I find that I cannot use Eclipse as I am not sure what it will do. I've switch to community edition of InteliJ as it seems to be working correctly. But as a long time fan of Eclipse I am heartbroken :)
Does anyone have any clue what could be causing this?
EDIT:
I have checkin in my test project here:
https://github.com/twolak2003/CamelSpringBootEureka.git in branch PropertyFileIssue. Simply clone, switch to PropertyFileIssue branch, import to eclipse as gradle project.
It is a simple vanilla spring cloud boot project running a eureka server and eureka client. For the purpose of this issue we'll just concentrate on eureka-service.
I am using latest Eclipse Oxygen. I did notice the same issue in Eclise Neon. I switched to Oxygen hoping the issue will go away. Only plugin is the Spring IDE. Using Java 8 to run this.
Test 1: Start the EurekaService/src/main/hello.java as Java application.
It starts fine and will read the src/main/resources/application.property file.
Test 2: Rename the EurekaService/src/main/test/resources/restapplication.properties to EurekaService/src/main/test/resources/application.properties. Start the EurekaService application again.
Findings: It starts fine as well BUT it uses property file in the /src/test/resources. This to me is issue as well, it should not be using /test/ for running the application.
Test 3: Now rename the /src/test/resources/application.properties to /src/test/resources/testapplication.properties again. (this is repeat of test1). Once again run EurekaService/src/main/hello as java application.
Finding Despite this being a repeat of test1 the test failed due to "Could not resolve placeholder 'tomek.prop' in value "${tomek.prop}"".
Am I doing something really stupid or is there an issue?
For now with heavy heart I am switching to InteliJ to keep my project going. The behaviour of Eclipse is just too unpredictable for me to stomach for now :(
So far I don't have a correct answer for this other than stop using Eclipse :). But there is workaround.
DO NOT use more than one application.property file.
If you need test properties to override actual properties then name your file as anything else but "application.property". Otherwise you'll get unpredictable behaviour and your Eclipse runtime get's confused.

Spring Roo, Mvn, & Eclipse -- Understanding the Entity Manager

So I've been running into this for a while and I've seen other questions / answers pertaining close to the subject but I don't feel I understand what's going on yet.
I'm using Spring Roo 1.1.15, Eclipse 3.6.0 & Maven 2.2.1.
I have found that if I have had a successful run of tests (running from within Eclipse) and then make any modification to a RooEntity class object that any/all Roo-centric tests will fail on the next run with the following:
Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)
This will continue until I do the following:
Enable "Build Automatically" in Eclipse's Project menu
Flip over to a terminal window and at the project root issue:
mvn -o clean package
Once mvn is finished, then flip back over to Eclipse and refresh the project
Let Eclipse refresh and then rebuild
At this point I can run the test suite and it will report accurate information. (Tests pass or fail based on actual results and not complaining of the Entity manager.)
I have not had the time to upgrade this project to the latest version of Roo and I admit that may be the proper "fix" but I was wondering if anyone else has seen this behavior and can explain what's happening in the rebuild process that would cause the manager to "disappear"? If so or you have found a way to allow Eclipse to act successfully independent of the terminal workaround, feedback would be great.
Thanks as always.

IntelliJ forcing me to restart my app on every code change

I am on Grails 1.3.5 and IntelliJ 9.0.4 on a Mac with the latest JDK
I have the simplest of Grails projects: a helloworld that simply renders a string directly from a controller. I created it through the New Project wizard in IntelliJ. That went fine and IntelliJ picks up the correct grails SDK.
The problem is that IntelliJ makes me restart the app to see any changes I make to my code, (e.g. changing the "hello world" string.
If I edit the same controller with a text editor (eg TextMate) and run the app from the command line with grails run-app I do get hot code replacement, which is obviously what I want...
Anyone got a clue?
Some points:
I strongly recommend using the latest IntelliJ X EAP (http://confluence.jetbrains.net/display/IDEADEV/IDEA+X+EAP) since Grails support has been improved a lot since 9.0.x
If your IntelliJ config files got messed up, you can easily recreate them with 'grails integrate-with --intellij'. N.B. this recreates the config files in and old format and IntelliJ suggest to upgrade them - follow this procedure
Make sure your run configuration has uses at least the same memory settings than Grails uses when run from the command line, I'm fine with setting the 'VM parameters' field to '-XX:MaxPermSize=256m -Xmx1G'
If build problems occur (in rare cases the IntelliJ's internal compiler is more strict than plain Groovy), disable the 'Make' checkbox in the run config dialog.
If the problem persists, paste a screenshot of the run configuration you're using.

Resources