If you use JUnit's liberary, you can complete the test of the program.
In spring development, Spring provides the relevant test modules, which makes it easy to load the configuration files and test them.
How to use:
The first step:
Introduction of the test package, found in the download of the Spring package Lib:
Add jar to project and build path
Step Two: Write the test class
@RunWith (Value=springjunit4classrunner.class)
@ContextConfiguration (locations = {"Classpath: Applicationcontext.xml "}) public
class testspring{
@Autowired
private iuserservice iuserservice;
@Test public
void Test () {
System.out.println ("test");
Iuserservice.save ();
}
}
Description
To test:
Execute the Test () method:
Successful execution
A problem
If the junit used is a liberary provided by the IDE, there may be a problem with the JUnit version being too low, with the following error:
Java.lang.ExceptionInInitializerError at Sun.reflect.NativeConstructorAccessorImpl.newInstance0 (Native Method) at Sun.reflect.NativeConstructorAccessorImpl.newInstance (Unknown Source) at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance (Unknown Source) at Java.lang.reflect.Constructor.newInstance (Unknown Source) at Org.junit.internal.builders.AnnotatedBuilder.buildRunner (annotatedbuilder.java:31) at Org.junit.internal.builders.AnnotatedBuilder.runnerForClass (annotatedbuilder.java:24) at Org.junit.runners.model.RunnerBuilder.safeRunnerForClass (runnerbuilder.java:57) at Org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass (Alldefaultpossibilitiesbuilder.java :) at Org.junit.runners.model.RunnerBuilder.safeRunnerForClass (runnerbuilder.java:57) at Org.junit.internal.requests.ClassRequest.getRunner (classrequest.java:24) at Org.junit.internal.requests.FilterRequest.getRunner (filterrequest.java:33) at Org.eclipse.jdt.internal.junit4.runner.JUnit4TestReFerence.<init> (junit4testreference.java:33) at Org.eclipse.jdt.internal.junit4.runner.junit4testmethodreference.<init> (JUnit4TestMethodReference.java : @ org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest (junit4testloader.java:54) at Org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests (junit4testloader.java:38) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:452) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (remotetestrunner.java:683) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (remotetestrunner.java:390) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (remotetestrunner.java:197) caused by: Java.lang.IllegalStateException:Failed to find class [Org.junit.runners.model.MultipleFailureException]:
Springjunit4classrunner requires JUnit 4.9 or higher. At Org.springframework.test.context.junit4.springjunit4classrunner.<clinit> (SpringJUnit4ClaSSRUNNER.JAVA:102)
Then remove the junit from the project and then download the Junit4.9 or version of the jar to the project Lib to resolve
junit4.9 Download Address: Download Junit4.9