Spring and JUNIT4 Integration test

Source: Internet
Author: User
Tags java web

One. Application scenario: Common Java Web Integration Spring test,

Project structure: Using the method of adding jars, not the way Maven joins dependencies

Note: If this is the way to introduce a jar package, for example: Spring-test4.0.5.jar and Junit4.12.jar,

  If you are using the Junit4.12.jar version, to add the Hamcrest-core1.3.jar package, the following error is reported:

Java.lang.Exception:No tests found matching [{Exactmatcher:fdisplayname=test], {exactmatcher:fdisplayname=test ( Org.devin.hellochat.test.Test)], {leadingidentifiermatcher:fclassname=org.devin.hellochat.test.test, Fleadingidentifier=test]] from [email protected] at Org.junit.internal.requests.FilterRequest.getRunner ( FILTERREQUEST.JAVA:40) at Org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest ( junit4testloader.java:77) at Org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest ( junit4testloader.java:68) at Org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests ( junit4testloader.java:43) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests ( remotetestrunner.java:444) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests ( remotetestrunner.java:675) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (Remotetestrunner.java : 382) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.maIn (remotetestrunner.java:192) 

Two. Use:

1.applicationcontext.xml spring-mvc.xml configuration file is placed under resource source file, Note: the Web. xml file needs to be configured <context-param></ Context-param>, and introduced the Applicationcontext.xml file,

Initialization of Spring-mvc.xml file in Sevlet configuration required

@RunWith (Springjunit4classrunner.class)

@ContextConfiguration ({"Classpath:applicationContext.xml", "Classpath:spring_mvc.xml"})

public class test{

@Automired

Private Itemcatservice Itemcatservice;

@Test

public void Test () {

System.out.println (Itemcatservice)

}

}

The 2.applicationcontext.xml spring-mvc.xml configuration file is placed in the Web-inf directory:

  

@RunWith (Springjunit4classrunner.class)

@ContextConfiguration ({"File:webcontent/web-inf/applicationcontext.xml", "File:webcontent/web-inf/spring_ Mvc.xml "})

public class test{

@Automired

Private Itemcatservice Itemcatservice;

@Test

public void Test () {

System.out.println (Itemcatservice)

}

}

Spring and JUNIT4 Integration test

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.