Spring Explanation: Unit Testing the spring framework with Spring-test

Source: Internet
Author: User
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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.