1
votes

When I try to invoke JUnit test cases from my Spring Boot Application, I am getting following error:

I am using this annotation in my Test class.

@RunWith(SpringRunner.class)

enter image description here

After I add JUnit library from project dependencies, it gives me following error:

enter image description here

java.lang.TypeNotPresentException: Type org.springframework.test.context.junit4.SpringRunner not present
    at sun.reflect.annotation.TypeNotPresentExceptionProxy.generateException(TypeNotPresentExceptionProxy.java:46)
    at sun.reflect.annotation.AnnotationInvocationHandler.invoke(AnnotationInvocationHandler.java:84)
    at com.sun.proxy.$Proxy2.value(Unknown Source)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:87)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:73)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:46)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:522)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.ClassNotFoundException: org.springframework.test.context.junit4.SpringRunner
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
    at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
    at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
    at sun.reflect.annotation.AnnotationParser.parseSig(AnnotationParser.java:439)
    at sun.reflect.annotation.AnnotationParser.parseClassValue(AnnotationParser.java:420)
    at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:349)
    at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:286)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
    at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
    at java.lang.Class.createAnnotationData(Class.java:3521)
    at java.lang.Class.annotationData(Class.java:3510)
    at java.lang.Class.getAnnotation(Class.java:3415)
    at org.junit.internal.builders.IgnoredBuilder.runnerForClass(IgnoredBuilder.java:10)
    ... 11 more

Here is my gradle dependencies: enter image description here

Also, I have the affected class in the class path: enter image description here

My spring boot version is 2.0.4. Editor is SpringToolSuite. Could someone please help me resolve this?

Thanks

4

4 Answers

1
votes

You don't need to pull in the JUnit 4 dependency via Eclipse's "JUnit library".

Instead, you should use the version of JUnit 4 that the spring-boot-starter-test dependency pulls in transitively (and automatically). That should pull in JUnit 4.12 for you, which is required by all modern versions of Spring Boot Test.

If that doesn't help you sort out the problem, please post your entire Gradle build script (or as much as you can share).

0
votes

I think you miss the dependency of JUnit because it's like spring is trying to find something in the org/junit/ but you didn't have that.

I never use gradle but for maven it should be like below.

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
</dependency>

Hope this helps.

0
votes

I tried running Junit by creating another sample project with Gradle and it just works fine and got surprised to see that below are the two dependencies creating issues mentioned in my post. It just works fine if I remove those dependencies. Not sure what's happening here:

compile('org.springframework.boot:spring-boot-starter-data-jpa') compile('com.fasterxml.jackson.core:2.9.6')

0
votes

@trjade, here is the screenshot of my dependencies:

enter image description here