How do I get Eclipse to respect the #FixMethodOrder JUnit directive? - eclipse

I’m using Eclipse Kepler (Java 8). with my Maven (v 3.2.3) project. I’m using JUnit 4.11, as declared in my pom.xml file …
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
I’m trying to run some JUnit tests and I have this at the top of my tests
#FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class MyTest
{
However, when I run the test in Eclipse (by right clicking the name of the file and selecting “Run As”, “JUnit Test”), Eclipse is not running the tests in ascending order by name, but instead running the tests one by one as they are listed in the file.
My question is, how can I get Eclipse to respect the “FixMethodOrder” directive in my file? Note that I’m not interested in lectures about why it shouldn’t matter what order you run JUnit tests in.

Open your project build path (right-click on project > Build Path > Configure Build Path...) and go to the Libraries tab. Do you have "JUnit 4" on your Eclipse classpath or just "Maven Dependencies":
Remove it by selecting "JUnit 4" and hitting the remove button to the right. Maven specifies all your dependencies so you don't need Eclipse adding to that.
In my experience Eclipse seems to prefer its own JUnit library at runtime over the JUnit library provided by "Maven Dependencies": I get different results in test execution order between Eclipse and direct Maven when using #FixMethodOrder if the Eclipse "JUnit 4" library is on the classpath, but if I remove that library then Eclipse and Maven are in agreement.

Related

How does Eclipse apply an Expose Package configuration to a Maven build?

How does Eclipse propagate Java 11 "Expose Package..." to the Maven target?
I am exposing a Java 11 module to ALL-UNNAMED using Eclipse: Properties -> Java Build Path -> Module Dependencies -> Expose Package...
Nothing is added to pom.xml with this configuration yet the project builds successfully.
Using Eclipse 2020-06, m2e's maven 3.6.3, maven-compiler-plugin 3.8.1 (this is the only explicit override in pom.xml). The rest is the "EMBEDDED" maven.
When I look at the Maven console and effective pom I do not see any compilerArgs (--add-exports...) added. When I run mvn from the command line I do get compiler failure as expected.
How does m2e propagate --add-exports to javac without it being visible to help:effective-pom target or Maven console?
In Eclipse when I click "Show JPMS Options..." it does display "--add-exports ...", but nowhere in the m2e debug output does this appear.

Cucumber .feature file is not getting linked to stepDefinition.java in Eclipse

I have installed Natural 0.7.6 plug-in from eclipse marketplace and I have Windows 10.
Eclipse version: Eclipse IDE for Java Developers
Version: 2020-03 (4.15.0)
Build id: 20200313-1211
Maven dependencies related to Cucumber:
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>5.7.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
.feature file
step definition file
[1]: [https://i.stack.imgur.com/e22x3.png][1]
[2]: https://i.stack.imgur.com/cAfLY.png
The .feature file complains about "No definition found for ".
CTRL+CLICK on .feature file step is not navigating to stepDefinition file (screenshots attached).
Am I missing any plugin? Is there any compatibility issues with Eclipse IDE version and the Cucumber plug-in I am using? I appreciate your help guys.
Steps to make it work.
Clear out all the cucumber related plugins(e.g. Natural 0.9, cucumber eclipse plugin 1.0.0.xxxxx). Please do follow the suggestion of restarting your eclipse.
Go to Help --> Eclipse Market place --> Install 'cucumber-eclipse' plugin. Please do follow the suggestion of restarting your eclipse.
Right click on your project --> Configure --> Convert to Cucumber project.
Now try to navigate to your step definition by 'Ctrl+click' on test step in feature file
Edit: I also changed JRE system library to 1.8( I am not sure it has any influence to make cucumber work )
I had same problem that CTRL+Click was not working in feature files when I installed Natural plugin in Eclipse IDE.
Later, I uninstalled Natural plugin, and installed "Cucumber Eclipse" plugin, and now I can perform ctrl+click on step name of feature file to navigate to the step definition file.
You can follow below stpes:
Un-install Natural plugin
Install Cucumber Eclipse plugin
Note: Below are my Eclipse version details. "Cucumber Eclipse" plugin may not be available for latest Eclipse version
Version: 2019-06 (4.12.0)
Build id: 20190614-1200
With the Cucumber Eclipse plugin, cleaning the Cucumber project (Project > Clean...) made the Gherkin steps Ctrl+click-able again.
You need a cucumber runner file first and inside that you need to glue the step definition file with the cucumber feature. The thing is that in cucumber if you place the feature file, step definition file and runner file in the same package then automatically, it will be able to map the steps from the step definition file with the feature file. But as per the screenshots, I can see that feature and step definition is present in the different packages. Please create a runner file as shown below
>
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
features = "<Directory path containing features>"
,glue={"<Step definition package name>"}
) // example: Features: src/test/resources/features, Glue: com.package.name.stepdefs
public class TestRunner {
}
TestRunner.java
If you are not able to get to the relevant step definition method from Feature file by either F3 or CTRL+ Left Mouse Click, change the default editor of Feature file as below
image to select correct Cucumber Editor

Eclipse maven not adding dependencies

I am new to maven and am experiencing difficulties while trying to mavenise a Java project.
Setting:
IDE: Eclipse Oxygen.2 Release (4.7.2)
Java: 8
m2e: 1.8.2
What I did:
- copy-pasted the entire original java project and renamed it
- right-click in eclipse: Configure > Convert to Maven project
- in java build path, deletion of libraries import from original local lib repo. The build path shows the Maven Dependencies folder, with the only junit library.
- maven install => downloaded things in the user/.m2/repository/, but not all.
What does not work:
When I try to add a dependency right from a file:
,
nothing pops up in the artifact selection windows, even though there is a commons-logging/ folder in m2/repository
When I try to add the dependency manually in the pom.xml:
<dependency>
<groupId>org.kie.modules</groupId>
<artifactId>org-apache-commons-configuration-main</artifactId>
<version>6.5.0.Final</version>
<type>pom</type>
</dependency>
but the package resolution error still appears in the java file, and I get this warning after Maven install
`[WARNING] The POM for org.kie.modules:org-apache-commons-configuration-main:pom:6.5.0.Final is invalid, transitive dependencies (if any) will not be available, enable debug logging for more details`
I did Maven Update project, eclipse project clean, nothing changes.
My goal for now is just that eclipse understands (at least for one library), that it has to take it from maven repository. I still have many other dependencies to solve (intra-project), but that will be the next step.
Thanks for your help.
The cause of the issue is stated in the warning message :
[WARNING] The POM for
org.kie.modules:org-apache-commons-configuration-main:pom:6.5.0.Final
is invalid, transitive dependencies (if any) will not be available,
enable debug logging for more details
It means that the pom.xml downloaded in your local maven repository exists but is not valid.
Delete the folder of the dependency downloaded in your local maven repository and try again.
If you still have the same problem, check that your central repository that provides the dependency provides also correctly the pom.xml for that.
You can do it by browsing the directory of the dependency from a web browser.
For example we can see that the maven central repository provides a valid pom :
http://central.maven.org/maven2/org/kie/modules/org-apache-commons-collections-main/6.5.0.Final/org-apache-commons-collections-main-6.5.0.Final.pom

How do I force Maven/Eclipse to download a JAR dependency?

I'm using Maven 3.3.3, Java 8 on Windows 7. I"m using Eclipse Mars. I'm trying to run a JUnit test in my Eclipse editor by right clicking on the class name, selecting, "Run As" -> "JUnit Test", and I'm getting this error
The archive: C:/Users/myuser/.m2/repository/commons-fileupload/commons-fileupload/1.2/commons-fileupload-1.2.jar which is referenced by the classpath, does not exist.
I have this dependency in my project's pom.xml file
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
What is odd is that if I run the test directly from the command line (using bash shell on Cygwin), the test runs ...
$ mvn clean test -Dtest=MyControllerIT
Hoewver, when I look in my repository, there is no JAR file downloaded. All it has is
$ ls /cygdrive/c//Users/myuser/.m2/repository/commons-fileupload/commons-fileupload/1.2/
_remote.repositories commons-fileupload-1.2.pom.lastUpdated
commons-fileupload-1.2.pom commons-fileupload-1.2.pom.sha1
How do I force the downloads of these JARs, or at least, how do I get Eclipse to stop complaining and being such a baby?
After updating the POM.xml file, all you have to do is, right click and refresh the Maven Dependencies.

Maven surefire arguments when running JUnit test via eclipse

A newbie question on Maven - Surefire - Eclipse - JUnit
I have configured the maven-surefire-plugin in the pom file of my project to pass some additional JVM arguments as below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<argLine>-d64 -Xms128m -Xmx4096m -XX:PermSize=512m -Duser.timezone=UTC -XX:-UseSplitVerifier</argLine>
</configuration>
</plugin>
When I run a test case of this project from Eclipse as Run As->JUnit Test, though the classpath is correctly set, the additional arguments specified in the argLine are not included in the invocation. I have to go and manually key in the arguments under the relevant Debug Configurations. I don't quite understand how JUnit is aware that it needs to put jars of the test scope on the classpath and in some way means that JUnit tool in Eclipse is aware of Maven via M2E? If so, how can we make it also read argLine. I know this sounds very specific - but how do others manage in similar situations?
Thanks in advance!
Eclipse JUnit Launcher (choose Run As -> JUnit Test) is a independent test runner which has its own pre-defined build and running life cycle and has nothing to do with Maven, it will not pick up your pom magically and read in the surefire configuration and use them to drive the test running.
If your project is imported as an existing Maven project, use Maven (choose Run as -> Maven test) launch your JUnit test which will pick up and use the surefire configuration. This is exactly same as running mvn test from commandline, it only output run log in console and you will not able to use the nice red & green JUnit UI window.
Hope this make sense.