IncompatibleClassChangeError in Scala Spring-Boot application - java

I am currently experiencing a weird exception when testing my Scala - Spring Boot application with JUnit Vintage.
java.lang.IncompatibleClassChangeError: org.myapp.SampleWebApplication and org.myapp.SampleWebApplication$delayedInit$body disagree on InnerClasses attribute
at java.lang.Class.getDeclaringClass0(Native Method)
at java.lang.Class.getDeclaringClass(Class.java:1235)
at java.lang.Class.getEnclosingClass(Class.java:1277)
at java.lang.Class.getSimpleBinaryName(Class.java:1443)
at java.lang.Class.isMemberClass(Class.java:1433)
at org.junit.internal.builders.AnnotatedBuilder.getEnclosingClassForNonStaticMemberClass(AnnotatedBuilder.java:94)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:83)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder.runnerForClass(DefensiveAllDefaultPossibilitiesBuilder.java:56)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.vintage.engine.discovery.TestClassRequestResolver.createRunnerTestDescriptor(TestClassRequestResolver.java:55)
at org.junit.vintage.engine.discovery.VintageDiscoverer.lambda$discover$0(VintageDiscoverer.java:53)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312)
at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:742)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.junit.vintage.engine.discovery.VintageDiscoverer.discover(VintageDiscoverer.java:55)
at org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:61)
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:130)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:117)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
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)
This is the entrypoint and it's the class that throws the error.
object SampleWebApplication extends App{
SpringApplication.run(classOf[Application])
}
HOWEVER! If I change the code to this (without extending app) the error stops appearing.
object SampleWebApplication {
def main(args : Array[String]) : Unit = {
SpringApplication.run(classOf[Application])
}
}
Do you have any suggestions on why I am experiencing this error? Why does removing "extends App" solve my issue?
Any help really appreciated.

I found out the issue. There is a bug in Scala 2.12.6 (fixed in 2.13.x). Because of the project I'm currently working on I can't bump the versions.
Workarounds if you cant update your Scala version:
1.One workaround is to update the test framework from JUnit Vintage -> JUnit Jupiter.
2.The second workaround (the one I did) is to change the Entrypoint of your application (see below).
object SampleWebApplication {
def main(args : Array[String]) : Unit = {
SpringApplication.run(classOf[Application])
}
}

Related

ORM in deployed WAR file is being ignored

The setup I have is that I am using Maven to build a Spring API project and generating a .WAR file. This war file is being deployed to a Tomcat server. My setup works locally in Eclipse with no issues or problems, and calls the correct queries/etc. However when I attempt to deploy it to the Tomcat server, I get the following error:
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property findDetails found for type EmployeeInfo!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:94)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:382)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:358)
at org.springframework.data.mapping.PropertyPath.lambda$from$0(PropertyPath.java:311)
at java.util.concurrent.ConcurrentMap.computeIfAbsent(ConcurrentMap.java:324)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:293)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:276)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:81)
at org.springframework.data.repository.query.parser.PartTree$OrPart.lambda$new$0(PartTree.java:250)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:251)
at org.springframework.data.repository.query.parser.PartTree$Predicate.lambda$new$0(PartTree.java:380)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:566)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:381)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:93)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:76)
... 96 more
I am not sure if we are missing something required to bridge the connection, or there is maybe a setting that is needed. Any assistance is welcome - I have included as many relevant files as I can think of, but let me know if anything else might be required and I will upload it too.
EmployeeInfoRepository
#Repository
public interface EmployeeInfoRepository extends JpaRepository<EmployeeInfo, Integer> {
#Query(nativeQuery = true)
public List<EmployeeInfo> findDetails(#Param("id") String id);
}
orm.xml (please note, it is located in src/main/resources/META-INF/)
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings version="2.0" xmlns="http://java.sun.com/xml/ns/persistence/orm" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm
http://java.sun.com/xml/ns/persistence/orm_2_0.xsd ">
<named-native-query name="EmployeeInfo.findDetails" result-class="com.place.thing.model.EmployeeInfo">
<query>SELECT ... </query>
</named-native-query>
</entity-mappings>
EmployeeInfoController
#GetMapping("/intake/{id}")
public ResponseEntity<EmployeeIntakeInfoResponse> getEmployeeInfo(#PathVariable(value = "id") String id) throws ResourceNotFoundException {
List<EmployeeInfo> result = intakeFormRepo.findDetails(id);
if (result.size() != 1) {
throw new ResourceNotFoundException("Error Message Here");
}
return ResponseEntity.ok().body(new EmployeeInfoResponse(result.get(0)));
}

Generate swagger.json

I have a java project (tomcat webapp) with some REST Api. I want to generate swagger documentation for them. I started from this tutorial (github).
I don't have maven we use an ant task. I included swagger-annotations-1.5.0.jar and all jar that cames with swagger-jaxrs JAR 1.5.0 version( I can include a complete list if it is useful)
I've annotated some method and I have a configuration class like this:
#ApplicationPath("/api")
public class Configurator extends Application {
public Configurator() {
init();
}
private void init() {
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.0");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("myserver.it:8080");
beanConfig.setBasePath("/swagger-demo/api");
beanConfig.setResourcePackage("myserver.rest");
beanConfig.setTitle("RESTEasy, Swagger and Swagger UI Example");
beanConfig.setScan(true);
}
}
when I deploy it agains tomcat 9 I can see this log in localhost.log file
java.lang.NoSuchMethodError: io.swagger.util.Json.create()Lcom/fasterxml/jackson/databind/ObjectMapper;
at io.swagger.jaxrs.json.JacksonJsonProvider.(JacksonJsonProvider.java:30)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.glassfish.hk2.utilities.reflection.ReflectionHelper.makeMe(ReflectionHelper.java:1375)
at org.jvnet.hk2.internal.ClazzCreator.createMe(ClazzCreator.java:272)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:366)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)
at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:83)
at org.jvnet.hk2.internal.SingletonContext$1.compute(SingletonContext.java:71)
at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture$1.call(Cache.java:97)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at org.glassfish.hk2.utilities.cache.Cache$OriginThreadAwareFuture.run(Cache.java:154)
at org.glassfish.hk2.utilities.cache.Cache.compute(Cache.java:199)
at org.jvnet.hk2.internal.SingletonContext.findOrCreate(SingletonContext.java:122)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2126)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:116)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:90)
at org.glassfish.jersey.inject.hk2.AbstractHk2InjectionManager.lambda$getAllServiceHolders$0(AbstractHk2InjectionManager.java:160)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.LinkedList$LLSpliterator.forEachRemaining(LinkedList.java:1235)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.glassfish.jersey.inject.hk2.AbstractHk2InjectionManager.getAllServiceHolders(AbstractHk2InjectionManager.java:164)
at org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.getAllServiceHolders(ImmediateHk2InjectionManager.java:54)
at org.glassfish.jersey.internal.inject.Providers.getServiceHolders(Providers.java:337)
at org.glassfish.jersey.internal.inject.Providers.getCustomProviders(Providers.java:175)
at org.glassfish.jersey.message.internal.MessageBodyFactory.initialize(MessageBodyFactory.java:238)
at org.glassfish.jersey.message.internal.MessageBodyFactory$MessageBodyWorkersConfigurator.postInit(MessageBodyFactory.java:136)
at org.glassfish.jersey.server.ApplicationHandler.lambda$initialize$2(ApplicationHandler.java:372)
at java.util.Arrays$ArrayList.forEach(Arrays.java:3880)
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:372)
at org.glassfish.jersey.server.ApplicationHandler.lambda$initialize$1(ApplicationHandler.java:316)
at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:256)
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:315)
at org.glassfish.jersey.server.ApplicationHandler.(ApplicationHandler.java:282)
at org.glassfish.jersey.servlet.WebComponent.(WebComponent.java:335)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:178)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:370)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1124)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:974)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4813)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5125)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3700)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:292)
at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5447)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1396)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1400)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1400)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1368)
at java.lang.Thread.run(Thread.java:748)
If I try to reach http://myserver.it:8080/swagger-demo/api/ this give me 404 error.
I read this post about this error (here) but I really can't say what's wrong. I'm strugglin.
Any hints would be appreciated
I answer to myself since I finally find a solution. Problem was inconsistency with libraries. I solved using this site Jar download, that gives me all the dependencies. Then I compare some libraries that I already jave in my project and in caso of conflict I choose the newest one.

Junit5 Intellij IDEA assertsThrows() throws noSuchMethodError

I'm trying to run test in Junit5 which should catch an exception from method. It catches NoSuchMethodError instead of excepted exception.
Method:
public void thisMethodShouldThrowException() throws IllegalArgumentException {
throw new IllegalArgumentException();
}
Test method:
#Test
void thisMethodShouldThrowException() throws IllegalArgumentException {
DBProperties dbProperties = DBProperties.getInstance();
Assertions.assertThrows(IllegalArgumentException.class,
()->dbProperties.thisMethodShouldThrowException());
}
build.gradle:
dependencies {
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.3.0-alpha4'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.11'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.0.0'
testCompile group: 'org.junit.platform', name: 'junit-platform-launcher', version: '1.0.0'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.0.0'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: '5.0.0'
testCompile("org.junit.vintage:junit-vintage-engine:4.12.0")
}
Stack trace:
org.opentest4j.AssertionFailedError: Unexpected exception type thrown ==>
Expected :<java.lang.IllegalArgumentException>
Actual :<java.lang.NoSuchMethodError>
<Click to see difference>
at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:59)
at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:38)
at org.junit.jupiter.api.Assertions.assertThrows(Assertions.java:1108)
at pl.javamill.bnbahistory.Controller.DBPropertiesTest.thisMethodShouldThrowException(DBPropertiesTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:389)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:167)
at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:163)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:110)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:83)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$null$2(HierarchicalTestExecutor.java:92)
at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$3(HierarchicalTestExecutor.java:92)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:77)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:51)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:170)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:154)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
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)
Caused by: java.lang.NoSuchMethodError: pl.javamill.bnbahistory.Controller.DBProperties.thisMethodShouldThrowException()V
at pl.javamill.bnbahistory.Controller.DBPropertiesTest.lambda$thisMethodShouldThrowException$1(DBPropertiesTest.java:24)
at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:50)
... 54 more
Intellij Idea version is 2018.2
I tried lastest version of Junit5 also.
I have no idea what I'm doing wrong. Can you help me?
I encountered the same error using junit-jupiter-api 5.3.2. A quick research showed the following:
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.function.ThrowingSupplier;
#Test
public void testVoidMethod() {
// works
Executable supplier = () -> crash();
assertThrows(NullPointerException.class, supplier);
}
#Test
public void testMethodWithReturnTypeCalledByExecutable() {
// works
Executable supplier = () -> wrong();
assertThrows(NullPointerException.class, supplier);
}
#Test
public void testMethodWithReturnType() {
// fails
assertThrows(NullPointerException.class, () -> wrong());
}
#Test
public void testMethodWithReturnTypeCalledByThrowingSupplier() {
// fails
ThrowingSupplier<?> supplier = () -> wrong();
assertThrows(NullPointerException.class, supplier);
}
public void crash() {
throw new NullPointerException();
}
public Object wrong() {
throw new NullPointerException();
}
Using assertThrows with a ThrowingSupplier raises a NoSuchMethodError.
I update to junit-jupiter-api 5.4.0. The API changed. There is no method assertThrows with a ThrowingSupplier as parameter anymore. Only the ones taking an Executable remained. But there are some new methods taking a ThrowingSupplier named assertDoesNotThrow.
Finally I updated to junit-jupiter-api 5.5.2.
The 5.4 Release Notes mention a bugfix of a similar case. However this is not an explanation for the NoSuchMethodError.
UPDATE:
The NoSuchMethodError is thrown if I run the JUnit tests via Eclipse's JUnit plugin. It uses org.junit.jupiter.api_5.1.0.v20180327-1502.jar whereas in the pom.xml JUnit 5.3.2 was specified. Running the tests with maven at JUnit 5.3.2 succeeds.
This was my case using Eclipse. I don't have IntelliJ IDEA.
I was looking up this same problem with something I was coding, and I fixed my issue by putting the test method into curly braces like so:
Assertions.assertThrows(IllegalArgumentException.class,
() -> {dbProperties.thisMethodShouldThrowException();} );

IntelliJ + JUnit 5 (Jupiter)

My build.gradle has:
testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.0'
Using the standard example from http://junit.org/junit5/docs/current/user-guide/
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class FirstJUnit5Tests {
#Test
void myFirstTest() {
assertEquals(2, 1 + 1);
}
}
When I try to run as a JUnit test in IntelliJ 2017.2.3, I get:
objc[32347]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java (0x10f5be4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10f6864e0). One of the two will be used. Which one is undefined.
Sep 11, 2017 12:39:04 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ClassUtils.isKotlinClass(Ljava/lang/Class;)Z
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$getTestInstanceLifecycle$14(ClassTestDescriptor.java:299)
at java.util.Optional.orElseGet(Optional.java:267)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.getTestInstanceLifecycle(ClassTestDescriptor.java:299)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.<init>(ClassTestDescriptor.java:87)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.<init>(ClassTestDescriptor.java:77)
at org.junit.jupiter.engine.discovery.TestContainerResolver.resolveClass(TestContainerResolver.java:100)
at org.junit.jupiter.engine.discovery.TestContainerResolver.resolveElement(TestContainerResolver.java:45)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.tryToResolveWithResolver(JavaElementsResolver.java:169)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.lambda$resolve$8(JavaElementsResolver.java:156)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolve(JavaElementsResolver.java:159)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.lambda$resolveForAllParents$4(JavaElementsResolver.java:130)
at java.util.Collections$SingletonSet.forEach(Collections.java:4767)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolveForAllParents(JavaElementsResolver.java:130)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolveContainerWithParents(JavaElementsResolver.java:80)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolveClass(JavaElementsResolver.java:55)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.lambda$resolveSelectors$2(DiscoverySelectorResolver.java:59)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:58)
at org.junit.jupiter.engine.JupiterTestEngine.resolveDiscoveryRequest(JupiterTestEngine.java:68)
at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:61)
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:130)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:117)
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:82)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:48)
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)
Sep 11, 2017 12:39:04 PM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ClassUtils.isKotlinClass(Ljava/lang/Class;)Z
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$getTestInstanceLifecycle$14(ClassTestDescriptor.java:299)
at java.util.Optional.orElseGet(Optional.java:267)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.getTestInstanceLifecycle(ClassTestDescriptor.java:299)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.<init>(ClassTestDescriptor.java:87)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.<init>(ClassTestDescriptor.java:77)
at org.junit.jupiter.engine.discovery.TestContainerResolver.resolveClass(TestContainerResolver.java:100)
at org.junit.jupiter.engine.discovery.TestContainerResolver.resolveElement(TestContainerResolver.java:45)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.tryToResolveWithResolver(JavaElementsResolver.java:169)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.lambda$resolve$8(JavaElementsResolver.java:156)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolve(JavaElementsResolver.java:159)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.lambda$resolveForAllParents$4(JavaElementsResolver.java:130)
at java.util.Collections$SingletonSet.forEach(Collections.java:4767)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolveForAllParents(JavaElementsResolver.java:130)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolveContainerWithParents(JavaElementsResolver.java:80)
at org.junit.jupiter.engine.discovery.JavaElementsResolver.resolveClass(JavaElementsResolver.java:55)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.lambda$resolveSelectors$2(DiscoverySelectorResolver.java:59)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:58)
at org.junit.jupiter.engine.JupiterTestEngine.resolveDiscoveryRequest(JupiterTestEngine.java:68)
at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:61)
at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:130)
at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:117)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:90)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:62)
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)
Process finished with exit code 0
Empty test suite.
I'm not using Kotlin at all, so I find those warnings + stack traces odd. Secondly, it isn't running the test. Any ideas?
IDEA version 2017.2.3 and older shipped with an outdated version of JUnit 5.
Use this to solve that issue until 5.0 GA is bundled in IDEA
// Only needed to run tests in an IntelliJ IDEA that bundles an older version
testRuntime("org.junit.platform:junit-platform-launcher:1.0.0")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0")
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0")
See http://junit.org/junit5/docs/current/user-guide/#running-tests-ide-intellij-idea for details.

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/Assume$AssumptionViolatedException

I've set up a Spring project, and I got an exception when running the following test.
package com.company.app;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:spring/spring-config.xml"})
public class CheckTest {
#Test
public void test_foo() {
assertEquals(0, 1);
}
}
The exception is as follows.
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/Assume$AssumptionViolatedException
at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:281)
at org.springframework.test.context.junit4.SpringMethodRoadie$2.run(SpringMethodRoadie.java:207)
at org.springframework.test.context.junit4.SpringMethodRoadie.runBeforesThenTestThenAfters(SpringMethodRoadie.java:254)
at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:234)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:204)
at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:146)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:151)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:59)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:52)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:50)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: org.junit.Assume$AssumptionViolatedException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 22 more
I found that AssumptionViolatedException was located in org.junit.AssumptionViolatedException instead of org.junit.Assume, I think maybe this is reason why the exception was thrown.
But if I remove the following annotation lines, it would work just fine
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = {"classpath:spring/spring-config.xml"})
The output is:
java.lang.AssertionError:
Expected :0
Actual :1
Most answers I got from the Internet told me that I should use junit and spring-test with a higher version. But I think I'm using the highest versions for both of them.
junit: 4.12
spring-test: 4.3.2.RELEASE
Is there any other things I should do to make it work? Or is it just a bug?
You are actually not using spring-test 4.3.2.
Rather, you have a very old version of spring-test on your classpath.
SpringMethodRoadie was introduced in Spring 2.5 for JUnit 4.4 support, and if I recall correctly, SpringMethodRoadie was removed in Spring 3.0 with the upgrade to JUnit 4.5.
The solution is to configure your build or IDE to use the correct version of spring-test.
Regards,
Sam (author of the Spring TestContext Framework)

Categories