I am trying to write a test following this documentation:
https://doc.akka.io/docs/akka/current/fsm.html#overview
This looks like to extend the AbstractJavaTest I need the scalatest dependency so I added the following to my pom:
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
From more googling I found that this class only extends the JUnitSuite, I am able to import it in my class, but I cant seen to extend it
THis works
import org.scalatest.junit.JUnitSuite;
But this doesn't
public class MyTest extends JunitSuite {}
How do I get the JunitSuite class?
You picked a truly antediluvian version of ScalaTest from 2010, whereas your Akka version is very recent.
Just go to Maven Central, scalatest and pick some newer version, you could try for example
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_2.12</artifactId>
<version>3.2.0-SNAP10</version>
<scope>test</scope>
</dependency>
Just make sure that you use either 2.11 or 2.12 consistently.
Related
I have the test that leads to error. I tried to execute it in the IntelliJ Idea 2018.3.2. All jupiter and junit dependencies have version RELEASE
The full text of error:
Dec 26, 2018 1:17:17 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(Ljava/lang/String;)Lorg/junit/platform/commons/function/Try;
at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.createAbortedExecutionPredicate(OpenTest4JAndJUnit4AwareThrowableCollector.java:40)
at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.<clinit>(OpenTest4JAndJUnit4AwareThrowableCollector.java:30)
at org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector(JupiterThrowableCollectorFactory.java:34)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:68)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
the test has the following view
import biz.Services.msg.BookTimeMsgService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertTrue;
#ExtendWith(MockitoExtension.class)
public class MsgReceiverTest {
#Mock
BookTimeMsgService bookTimeMsgService;
#InjectMocks
MsgReceiver msgReceiver;
#Test
public void msgReceiverTest_bookTimeServiceShouldObtainMsg() {
assertTrue(true);
}
part of my pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>RELEASE</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
How to fix the issue?
I changed the version of jupiter and junit to 5.3.2 and the problem has gone
I was able to run the Junit 5 tests after adding the platform and launcher dependency:
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-commons -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.4.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
minimal required pom.xml setup for spring-boot 2.1.3 and junit 5.4.0 testing next:
<?xml version="1.0" encoding="UTF-8"?>
<project ...>
<parent>
<groupId>org.springframework.boot</g..roupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<junit-jupiter.version>5.4.0</junit-jupiter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
use:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.4.2</version>
<scope>test</scope>
</dependency>
instead
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
you can find more details here
Combos of Maven/Gradle, Eclipse/IntelliJ using Spring Boot/or not while including Junit4/Junit5 throw different flavors of this NoSuchMethodError. This leads to many different solutions all of which only fix a portion of people's problems. This long winded answer is an attempt to explain the root cause and steps you can take to fix your problem whichever flavor you are using.
Root cause:
At compile time, one version of ReflectionUtils (I've seen other classes here too) was used, and at runtime, a different one was used. The runtime version was expecting a certain method to exist, but either it didn't exist, or it existed with a different signature (number/type of parameters)
Note, when I say compile time, this can mean (and usually does) mean the version of ReflectionUtils that was used to compile a third party jar inside your classpath.
The solution will always be to change or exclude some version of some jar. The trick is to find which jar and which version.
To solve:
First, identify what version of ReflectionUtils (or other class it is complaining about) is being used at runtime. This is fairly easy. In eclipse, do an Open Type, in IntelliJ, use a navigate/go to class. Make sure to fully qualify the path. If you have exactly 1 version on the path, this is good. If you have more than on version on the path, your first action to take is to modify your build path to exclude one of the versions.
If you cannot modify the build path, even temporarily, it is harder to identify what version of ReflectionUtils is being used at runtime. Since JUnit 4 and 5 begins and dies before you can run any of your code, you cannot insert a
CodeSource src = ReflectionUtils.class.getProtectionDomain().getCodeSource();
if (src != null) {
URL jar = src.getLocation();
System.out.println(jar.toString());
}
to get the location of the runtime jar. But you can try:
1 - create a fresh new project, a fresh pom, put 1 junit inside of it, and run it with the above code and see what comes out. This may work because whatever conflict was in the non working project has been removed. This may not work in which case move to #2. This also may give a false result because the runtime path of a fresh project may be different than that of the non working project. Try updating the pom of the fresh project to look more and more like that of the non working project. Remember, the goal is to find what version of the class is being used at runtime
2 - Use google. Yah I know you got here by using google but this time change your search to see if someone documented "This version of JUnit5 uses this version of ReflectionUtils at runtime" or "This version of IntelliJ puts this version of ReflectionUtils on the classpath" or "This version of Spring Boot is putting this version of ReflectionUtils on the classpath". Remember, the goal is to find what version of the class is being used at runtime
3 - Hijack a class (advanced). When the stacktrace comes out, you will see several classes in the stacktrace. Try to find the source code for the class on the internet and create that package and class in your project. Then insert the code above and try to run the test which was failing.
Once you have found which version of ReflectionUtils is used at runtime, you need to find out what version was used at compile time
Identify which jar ReflectionUtils lives inside.
In the stacktrace which pops out when you run your test, identify the top class and method which died trying to use ReflectionUtils. This is the code which was compiled with an incompatible version of ReflectionUtils. Find out what jar that class is inside. I'll call this jar the conflicting jar. Try and get a dependency tree for that conflicting jar. The dependency tree should indicate which version of the ReflectionUtils jar the conflicting jar was dependent on.
Once you have found which version of ReflectionUtils jar was used at compile time and runtime, you likely will see that they are different and also incompatible. At this point, you will have to
1 - change the version of the ReflectionUtils jar used at runtime to match the version of ReflectionUtils needed by the conflicting jar
2 - change the version of the conflicting jar to one which is dependent on the same version of the ReflectionUtils jar which is on the runtime path
3 - if the conflicting jar is a dependency of some parent jar in build path and not explicity included by your build path, try excluding the conflicting jar from the parent jar and add a newer version explicitly. This may solve the problem - but this is, of course, the type of behavior which created this mess in the first place.
Other actions you can take:
If you know that you are using both JUnit4 jars as well as JUnit5 in your project, try removing one or the other completely from the compile time and runtime paths. The Assert patterns of the two paradigms are different enough that you likely do not want to mix them in the same project as it could lead to confusion of which parameters are the expected results, the message, and the variable under test.
I have gone through all previous StackOverflow issues related to the same. This post will be long so please bear with me. The folders in my cucumber project are ordered as follows:
-src/main/java
-src/main/resources
-src/test/java
-|CucumberRunner (package)
-|CucumberTestRunner.java
-|CucumberTestDefinition (package)
-|CucumberStepDefinition.java
-src/test/resources
-CucumberFeaturesFolder
-|CucumberFeatureFile.feature
Here is a picture of the arrangement of the Project folders if the above order did not make sense to you. Order of project folders inside the project
My pom.xml has the following dependency added (no more dependency):
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.2</version>
<scope>test</scope>
</dependency>
My CucumberTestRunner.java file contains the following:
package CucumberRunner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(
features = "CucumberFeaturesFolder/CucumberFeatureFile.feature",
glue = {"src/test.java/CucumberTestDefinition"}
)
public class CucumberTestRunner {}
The error that I get when I try to run CucumberFeatureFile.feature is the following:
cucumber.runtime.CucumberException: Failed to instantiate class CucumberTestDefinition.CucumberStepDefinition
Now, after reading the similar posts mentioned on StackOverflow, I tried changing the version of cucumber-unit & cucumber-java from 1.2.2 to 1.2.0 which also resulted in an error but a different one:
Exception in thread “main” cucumber.runtime.CucumberException: No backends were found
Change
glue = {"src/test.java/CucumberTestDefinition"}
to
glue = {"src/test/java/CucumberTestDefinition"}
Make sure the Java version is compatible with cucumber dependencies and other dependencies added in POM.xml. Earlier I was tried with JDK 7 but after changing to JDK 8, the error/exception was no longer visible at runtime and was able to execute the test successfully.
Restart the Appium Server and run again
Also check the all saved locator. If you create any Mobile/Web element and don't assign any value you can face CucumberException.
#iOSXCUITFindBy(accessibility = "")ERROR REASON
public MobileElement submitButton;
#iOSXCUITFindBy(accessibility = "submit") TRUE
public MobileElement submitButton;
I'm trying to setup my IntelliJ workspace to do development on an eclipse project. One of the things I've run into is rather confusing:
Error:(24, 8) java: SomeClass.java:24: getHeader(java.lang.String) in org.springframework.mock.web.MockHttpServletResponse cannot implement getHeader(java.lang.String) in javax.servlet.http.HttpServletResponse; attempting to use incompatible return type
found : java.lang.Object
required: java.lang.String
The problem is the following class definition:
public class SomeClass extends MockHttpServletResponse {
The problem seems to be because MockHttpServletResponse implements Collection<String> getHeaders(String) as public List getHeaders(String name). Here, I can see that the implementing method uses a raw List where the parent asks for a generic Collection typed with String. Aside from being potentially not type-safe, why would IntelliJ mark this as a complier error instead of a warning?
I have no option of changing any of these libraries. I'm simply trying to make work in IntellJ 14 what already works without complaints in Eclipse 4.3+.
EDIT:
I have since updated to IntelliJ 15.0, and the project is using Java 1.7 now instead of 1.6. I am still running into this issue with IntelliJ, but the issue is not presenting itself at all in Eclipse. I can compile the project using existing Ant scripts via IntelliJ, but I cannot debug through the IDE.
Here is my class definition
public class ExecutableServletResponse extends MockHttpServletResponse {
...
Here is the error showing in my 'Messages' pane:
Error:(24, 8) java: getHeader(java.lang.String) in org.springframework.mock.web.MockHttpServletResponse cannot implement getHeader(java.lang.String) in javax.servlet.http.HttpServletResponse
return type java.lang.Object is not compatible with java.lang.String
The project SDK is using version 1.7 (1.7.0_79 to be exact). Language level is 7. Module SDK and Language Levels match the project.
I've tried using the eclipse compiler, but the app still doesn't fully compile, and will fail to run presumably because it fails to compile this class, and a whole part of the webapp doesn't compile as a result.
Here's a screenshot of my error, FWIW:
You are seeing the error in your class, but the real issue is that the Spring mock library is not compatible with the Servlet Specification you are using. This may happen if you upgraded to the Servlet 3.0 Spec (or added a dependency that pulled it in transitively). Check your dependencies and ensure that either:
Only Servlet 2.5 is provided, or
You are using a version of Spring that is compatible with Servlet 3.0. Also ensure that all of your Spring dependencies are using the same version.
This combination should work:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
as should this:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.0.RELEASE</version>
<scope>test</scope>
</dependency>
But this will fail:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.2.RELEASE</version>
<scope>test</scope>
</dependency>
The fact that it works in Eclipse but not in IntelliJ suggests that you have multiple dependencies that provide the same classes. There is no guarantee as to which jar the system will use to load the class. This may either be because you have both servlet-api and javaee-web-api on your classpath or because you have both spring-mock and spring-test on your classpath. After version 2.0.8, the classes in spring-mock were moved to spring-test and only version 3.1.0.RELEASE and higher of spring-test are compatible with Servlet 3.0.
Ok, so this is probably a NooB question (I'm more of a C++ guy), but I'm lost in the java woods and its frameworks forests...
I'm trying to look into eclipse RCP development. For that I'm following this well-known tutorial:
http://www.vogella.com/tutorials/EclipseRCP/article.html
At step 15 I need to add the following dependency packages to import in my bundle.
javax.annotation
javax.injection
The problem is that I cannot select these (they are not in the selection list)
I do have javax.el javax.servlet.* and javax.xml.*
Looking at
http://docs.oracle.com/javase/7/docs/api/overview-summary.html
suggests that this should be part of the standard java.
What obvious mistake am I missing?
The dependency including version:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
See: http://mvnrepository.com/artifact/javax.annotation/javax.annotation-api
Or for the newer jakarta.annotation:
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>1.3.5</version>
</dependency>
See: https://mvnrepository.com/artifact/jakarta.annotation/jakarta.annotation-api
The Java Common Annotations Module java.xml.ws.annotation was deprecated in Java version 9 and was removed in java version 11. If this leads to a problem you could try to add javax.annotation.
The Javadocs for Java 8 can be found here:
http://docs.oracle.com/javase/8/docs/api/javax/annotation/package-summary.html
Your comment indicates this is for Guava, so you want the JSR305 library, which extends the javax package.
2020 Update: Note that this library breaks the Oracle licensing agreement and Guava has since moved to checkerframework's Nullable annotation.
Not sure if this is still relevant, but for Java 8, I had to add the two following Maven dependencies in order to get javax.annotation.concurrent.ThreadSafe to work:
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.0</version>
</dependency>
Javax annotations include in this dependency. This is latest version at the moment.
For maven
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>
For gradle
compile('com.google.code.findbugs:jsr305:3.0.2')
In Java version >= 6, you should not need to add them explicitly.
They are part of the JDK. Just try to skip adding them, maybe
the list of instructions is outdated.
Before Java 6, you would have needed to add this jar, I think: jsr250-api-1.0.jar.
http://central.maven.org/maven2/javax/annotation/jsr250-api/1.0/
http://download.java.net/maven/2/javax/annotation/jsr250-api/1.0/
I have a Java Maven project that I developed a while ago and that doesn't work anymore. It uses a parent pom together with another Maven project in which I think the Jena version was changed and it also uses an external library that uses Jena. The Maven dependency is:
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>jena</artifactId>
<version>2.6.4</version>
</dependency>
When I execute my tests I get the following errors:
java.lang.NoClassDefFoundError: Could not initialize class
com.hp.hpl.jena.query.ARQ
java.lang.NoClassDefFoundError: org/apache/jena/iri/IRIFactory
at org.openjena.riot.system.PrefixMap.add(PrefixMap.java:54)
at com.hp.hpl.jena.sparql.util.MappingRegistry.addPrefixMapping(MappingRegistry.java:33)
at com.hp.hpl.jena.query.ARQ.init(ARQ.java:449) [...]
The errors are not thrown by my code directly but by the library I include. Can I prevent this by downgrading the Jena version in the parent pom or what can I do here?
P.S.: I now have a minimal code example that reproduces the error (java.lang.NoClassDefFoundError: org/apache/jena/iri/IRIFactory):
import org.junit.Test;
import com.hp.hpl.jena.query.ARQ;
public class DependencyTest
{
#Test
public void testARQ()
{
ARQ a = new ARQ();
}
}
And I guess it comes from this dependency:
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.1-incubating-SNAPSHOT</version>
</dependency>
I know there is probably a factory instead of a constructor but I guess this still shows where the problem is.
P.S.: I noticed that I had the dependencies "jena", "arq" and "jena-arq":
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>arq</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.1-incubating-SNAPSHOT</version>
</dependency>
dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>jena</artifactId>
<version>2.6.4</version>
</dependency>
So I thought maybe I have too much overlapping dependencies and commented out "jena" and "arq". But I still get the error
java.lang.NoClassDefFoundError: Could not initialize class com.hp.hpl.jena.query.ARQ
at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.<init> [...]
I also tried out forcing a non-snapshot-version 2.9.0-incubating, but then I still get the NoClassDefFoundError with and without using the "jena" and "arq"-dependencies.
P.P.S.:
I still get the same error even when I use the following dependencies:
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.9.0-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>2.7.0-incubating</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-iri</artifactId>
<version>2.7.0-incubating</version>
</dependency>
You can search for the missing class using the Maven Central search application
http://search.maven.org/#search|ga|1|fc%3A%22com.hp.hpl.jena.query.ARQ%22
It demonstrates that the dependency you appear to be missing is:
<dependency>
<groupId>com.hp.hpl.jena</groupId>
<artifactId>arq</artifactId>
<version>2.6.0</version>
</dependency>
Doesn't appear to be a version 2.6.4, but you're probabily best advised to go for a more modern version (This project was recently donated to apache)
Instructions for using Apache Jena with Maven are here:
incubator.apache.org/jena/download/maven.html
Specifying ARQ 2.9.0 as a dependency in your project pom.xml will pull in the other Jena components that you need.
I finally resolved this error by excluding the "jena"-Dependency brought in as a transitive dependency from some library.