Tag Archives: Junit

[Solved] JUnit test error: Java lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

Then Google checked and found that it was the problem with the version, and then reduced the version from 4.13 2 to 4.12 still can’t

Check Google again to change to version 4.10, or import hamcrest-core-1.3 jar

The official website says:

Includes the Hamcrest classes. The simple all-in-one solution to get started quickly. Starting with version 4.11, Hamcrest is no longer included in this jar.

 

So the solution is:

1. JUnit reduced version 4.10

2. Import hamcrest-core-1.3 jar

JUnit Error: java.lang.IncompatibleClassChangeError [How to Solve]

When testing spring with JUnit, an error is reported:

 

 1 java.lang.IncompatibleClassChangeError: class org.springframework.core.LocalVariableTableParameterNameDiscoverer$ParameterNameDiscoveringVisitor has interface org.springframework.asm.ClassVisitor as super class
 2     at java.lang.ClassLoader.defineClass1(Native Method)
 3     at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
 4     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
 5     at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
 6     at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
 7     at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
 8     at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
 9     at java.security.AccessController.doPrivileged(Native Method)
10     at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
11     at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
12     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
13     at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
14     at org.springframework.core.LocalVariableTableParameterNameDiscoverer.inspectClass(LocalVariableTableParameterNameDiscoverer.java:114)
15     at org.springframework.core.LocalVariableTableParameterNameDiscoverer.getParameterNames(LocalVariableTableParameterNameDiscoverer.java:87)
16     at org.springframework.core.PrioritizedParameterNameDiscoverer.getParameterNames(PrioritizedParameterNameDiscoverer.java:65)
17     at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:182)
18     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
19     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
20     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
21     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
22     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
23     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
24     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
25     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
26     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
27     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
28     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
29     at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)
30     at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)
31     at com.it.sxd.SayHelloTest2.testSayHello(SayHelloTest2.java:11)
32     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
33     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
34     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
35     at java.lang.reflect.Method.invoke(Method.java:606)
36     at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
37     at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
38     at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
39     at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
40     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
41     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
42     at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
43     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
44     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
45     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
46     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
47     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
48     at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
49     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
50     at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
51     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
52     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
53     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
54     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

 

This error has a class called classmetadatareadingvisitor, which takes an interface called classvisitor as the parent class. But as we all know, the relationship between classes and interfaces in Java can only be implementation, not inheritance

Then this is the conflict between shelves and bags:

Just remove this rack bag

Idea write JUnit test case found:…empty test suite

Why can’t you stop buying 618?From the technical dimension to explore>>>

When learning mybatis from the video, I used JUnit to write a test case to test Dao method, and reported the following error:

After reading some posts on the Internet, some said to delete the root directory. Idea file, and some said to clear the run/debug configuration, but it was not solved. I remembered that I had reported an error before:

This means that the output path is unknown. It was set to./out at that time. Just read a post saying that class not found is not that the class cannot be found, but that the compiled. Class file cannot be found, so the output path is set to out under the project, and the problem is solved

The setting method is as follows:

1. Press Ctrl + Shift + Alt + s to find Modules > The path is as follows:

2. Select project and set the output folder according to your own needs

The problem of JUnit test report class not found in maven

Open source software supply chain lighting plan, waiting for you>>>

When you first contact maven, the path of Maven is not the same as that of normal eclipse projects. Java files are placed in Src/main/Java directory, and test files are placed in Src/test/Java directory. The normal test method is to use MVN test for unit test, but you are used to using eclipse. When you test directly in IDE, you will report exception, class not found exception

At the beginning, I thought it was the problem of the build path, because when I was watching maven, someone on the Internet said that they wanted to set the buildpath of eclipse, and then changed it. But sometimes it was good and sometimes it was bad. I felt that the problem was not here. If you think about it more carefully, since it’s class not found, go and look for the class file to see if the directory is correct. Once you look for it, you can find the reason. There is no such class at all. In other words, the java file is not compiled into a class file at all. In eclipse, it can help us compile automatically, but after we run the MVN clean command in maven, we will clean up the compiled class file. At this time, if we use Maven to test and run MVN test, Maven will help you compile again automatically, and the test can run normally. However, if it is directly in eclipse, eclipse will report an exception if it can’t find the compiled class file. The solution is to run the command MVN test to test. After the test is passed, as long as you don’t run MVN clean again, you can also pass the unit test directly in eclipse

The lessons learned this time: don’t rush to check the Internet for some problems. Think about why you report such abnormalities. Think about the deep reasons. Maybe the solution is very simple

junitjava.lang.Exception: No tests found matching

Open source software supply chain lighting plan, waiting for you>>>

JUnit error java.lang.exception: no tests found matching

Recently, I used JUnit in the process of learning spring, but I encountered a crazy error

1 java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=cdShouldNotBeNull], {ExactMatcher:fDisplayName=cdShouldNotBeNull(soundsystem.CDPlayerTest)], {LeadingIdentifierMatcher:fClassName=soundsystem.CDPlayerTest,fLeadingIdentifier=cdShouldNotBeNull]] from org.junit.internal.requests.ClassRequest@3deff3
2     at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40)
3     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:77)
4     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:68)
5     at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
6     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
7     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
8     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
9     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

The code is as follows:

 1 import static org.junit.Assert.*;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.test.context.ContextConfiguration;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9 
10 @RunWith(SpringJUnit4ClassRunner.class)
11 @ContextConfiguration(classes = CDPlayerConfig.class)
12 public class CDPlayerTest {
13 
14     @Resource
15     private CompactDisc cd;
16 
17     @Test
18     public void cdShouldNotBeNull(){
19     assertNotNull(cd);
20     }
21 
22 }

After carefully checking the code, there should be no problem. After checking on the Internet, I found that many friends have also encountered this problem. Finally, I found the solution as follows

Reference solution:

It turns out that apart from the packages that JUnit needs to refer to in the code, if the application context that spring needs to create automatically is involved, another jar package is also involved
hamcrest-all-1.3. Jar
just import this jar package

Original reference:
JUnit error java.lang.exception: no tests found matching

JUnit error: Java. Lang. exception: the test class should have only one public constructor

Error message

java.lang.Exception: Test class should have exactly one public zero-argument constructor
	at org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(BlockJUnit4ClassRunner.java:171)
	at org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:148)
	at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:127)
	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.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
	at org.junit.vintage.engine.discovery.DefensiveAllDefaultPossibilitiesBuilder$DefensiveJUnit4Builder.runnerForClass(DefensiveAllDefaultPossibilitiesBuilder.java:128)
	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.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)

Test class should have only one public zero parameter constructor