I am facing one problem while running the Junit scripts. I am getting the below error message.
I have three java classes under which i have commented all the #Test annotations from Class A and Class B but have four #Test annotations in Class C..but still it is showing the below error message.
Can anyone help me how to fix this issue?
Error: java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:169)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:104)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:355)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.Parameterized$TestClassRunnerForParameters.<init>(Parameterized.java:171)
at org.junit.runners.Parameterized.createRunnersForParameters(Parameterized.java:319)
at org.junit.runners.Parameterized.<init>(Parameterized.java:282)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
Can anyone please help here?
Ensure that all of your relevant test case objects are public and non-abstract, and that your test methods are annotated with #Test but are also public void and non-static.
See more at the JUnit Getting Started Guide.
Make sure you have this import:
import org.junit.Test;
instead of:
import org.junit.jupiter.api.Test;
As well as the following annotations on test class if your project is SpringBoot 2:
#SpringBootTest
#RunWith(SpringRunner.class)
I faced the same issue "Error: java.lang.Exception: No runnable methods" in my simple Spring-boot project. I resolved the issue by doing the following steps.
Make sure that the the test class is in src/test/java folder. In my project settings the Test class was in src/test folder.
Make sure that src/test/java is in Java Build Path. Go to Project->properties->Java Build Path ->source and add src/test/java folder.
Test class should be in same package as the source Java class with main method.
Make sure you import the right package :
import org.junit.runners.Parameterized;
and not the testng package
Related
I am Using Junit5 with Quarkus F/w.
when I try to Mock Service class by Using #InjectMock(io.quarkus.test.junit.mockito.InjectMock) it gives an error
Like org.junit.jupiter.api.extension.TestInstantiationException: Failed to create test instance
and another side when I am using #InjectMocks which belongs to (org.mockito.InjectMocks) package
Junit Running fine but, the Service class Did not Mock it is hit a real DAO class and inserted data
inside database
total conclusion in #InjectMocks is not working.
Please give me any suggestion but this mocking
Thank you.
I have a problem using Junit tests with Java and Eclipse. All of my tests run just fine when I invoke them standalone. By this I mean that HDLmTreeTest, HDLmTreeTest1, and HDLmTreeTest2 all run fine when I run them by themselves. However, I have a Java source module with all of the test files in it. See below.
package com.headlamp;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;
#RunWith(JUnitPlatform.class)
#SelectClasses({HDLmConfigTest.class, HDLmDefinesTest.class,
HDLmErrorTest.class, HDLmStringTest.class, HDLmTreeTest.class,
HDLmTreeTest1.class, HDLmTreeTest2.class,
HDLmModTest.class, HDLmBuildJSTest.class, HDLmFindTest.class,
HDLmAssertTest.class, HDLmBuildLinesTest.class, HDLmUtilityTest.class,
HDLmSavedChangeTest.class, HDLmCurlApacheTest.class, HDLmMainTest.class,
HDLmJettyTest.class, HDLmCurlJettyTest.class, HDLmEditorServletTest.class,
HDLmApacheTest.class, HDLmProxyTest.class, HDLmSessionTest.class,
HDLmLogMsgTest.class, HDLmMatchTest.class, HDLmImageInformationTest.class,
HDLmClusteringTest.class, HDLmJsonTest.class})
class HDLmAllTests { }
When I run this file, all of my tests get invoked except for HDLmTreeTest1.class and HDLmTreeTest2.class. I should say that HDLmTreeTest1.class and HDLmTreeTest2.class were just recently created and added to the SelectClasses list. For some reason, they are not invoked, but all of the other classes are invoked. What am I doing wrong?
I figured this out (with lots of online help). The problem was the name(s) of my test classes. Test classes (the names of test classes) must end with 'Test' or 'Tests'. They can not end with 'Test1' or 'Test2'. That was my error. I changed the class names and the problem went away. See https://howtodoinjava.com/junit5/junit5-test-suites-examples/ for some details.
I am writing JUnit Test case using powermock i.e. #RunWith(PowerMockRunner.class) and using #PrepareTest annotations and added all the Power mock dependencies but still why I am getting java.lang.NoClassDefFoundError: org.powermock/tests/utils/RunnerTestSuitChunker.
Any Suggestion will be appreciated.
I bump version of junit to 4.11 and get:
[WARNING] [deprecation] Assert in junit.framework has been deprecated
[WARNING] [deprecation] Assert in junit.framework has been deprecated
....
How and to what migrate?
As it seems the Assert class has been moved from junit.framework to org.junit.Assert in JUnit 4.0 - you can use that instead, it's not deprecated.
Change your import statement from
import junit.framework.Assert;
to
import org.junit.Assert;
and this will rectify your JUnit deprecation warnings.
Both are depricated:
junit.framework.Assert.assertThat
org.junit.Assert.assertThat
According to docs, use Instead:
org.hamcrest.MatcherAssert.assertThat
After facing this problem I have tried lots of ways to solve this but failed again and again.
The good thing is: I have download junit-4.12.jar file from here and added the jar file in the project section under the libs folder. If previously any kind of Junit dependancy exist in the project then remove that from the build.gradle and build + clean your project.
It is worked for me. Hope it will work for you.
Note: Take a look in the image that I attached in below.
Thank you
We had a large number of tests with many assertions.
Adding something like
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
to the import statements also helped to limit the changes in test code.
You can refer to jUnit4 Assert class methods from JUnit4
I was trying to mock final class(AnyFinalClass.java) in junit using JDave in eclipse.
public void setUp() throws Exception {
Mockery mockery = new Mockery() {{
setImposteriser(ClassImposteriser.INSTANCE);
}};
AnyFinalClass any = mockery.mock(AnyFinalClass.class);
}
I am trying to use jdave-unfinalizer-1.1.jar as javaagent but didnt had any success. I tried multiple things but getting following exception
java.lang.IllegalArgumentException: Cannot subclass final class class AnyFinalClass
at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:446)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
Can someone who has already tried jdave unfinalizer give me exact step how to make it work on eclipse.
I set following in eclipse.ini file but got the problem
-Xbootclasspath/a:lC:\WS\JunitTesting\jars\asm-3.0.jar
-javaagent:C:\WS\JunitTesting\jars\jdave-unfinalizer-1.1.jar
While running executing the junit, I gave vm argument as
javaagent:C:\WS\JunitTesting\jars\jdave-unfinalizer-1.1.jar
I am not sure what will be the code. jdave is not having the code and its site is pointing to some other site which is not working. Please correct my code or provide your same working code.
Any help is highly appreciated.
from Enhancer.java line 446:
if (TypeUtils.isFinal(sc.getModifiers()))
throw new IllegalArgumentException("Cannot subclass final class " + sc);
I have not worked with JDave but with another mocking frameworks and the only one that allows to mock a final class was powermock
Look also here
In order to get unfinalizer running you have to put -javaagent:path_to_unfinalizer/jdave-unfinalizer-1.1.jar in the VM arguments of the run configuration of the test.
I also had to include several dependencies of jdave-unfinalizer in the classpath of the project from which the tests ar being launched. These are, taken from the maven definitions of jdave:
jdave-core 1.1
cglib-nodep 2.1_3
objenesis 1.0
asm 3.0
asm-commons 3.0
asm-tree 3.0