Issue
I'm running unit tests with Spring boot, but I get a weird no runnable
error. The tests pass by the way, but after all test have ended successfully, I get this weird error out of nowhere:
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:137)
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
How do I fix this? Why is Spring boot looking for a runnable in my tests?
Here is an example of my code
package ca.bell.uc.hello.world
import org.junit.Assert
import org.junit.jupiter.api.Test
import org.junit.runner.RunWith
import org.springframework.test.context.junit4.SpringRunner
@RunWith(SpringRunner::class)
internal class example {
@Test
fun f() {
Assert.assertTrue(true)
}
}
And here is a screenshot of the error:
Thanks
P.S. This is Kotlin
Solution
You are trying to use JUnit 5 @Test
annotation because of import:
import org.junit.jupiter.api.Test
In the console log we can see that JUnit 4 is used.
If you want to work with JUnit 4 you should use the import:
import org.junit.Test
Answered By - mslowiak
Answer Checked By - Robin (JavaFixing Admin)