Maven managed Spring Web Project Integration JUnit Unit test

Source: Internet
Author: User

JUnit is a good set of unit testing frameworks, and Maven is a great Java Project Building and management tool that makes it easy to test your project automatically.

In general, simple Java applications don't say much, and some frameworks provide extensions to junit that make testing easier, such as spring's official spring-test, which provides support for obtaining applicationcontext.

The first thing to do is to change the actual execution class of JUnit and replace the default execution class suite with the Springjunit4classrunner provided by spring, which is to precede the test class with an annotation:

[Java] view plain copy print? @RunWith (Springjunit4classrunner.class)

Then we need to tell the location of this test class spring configuration file:

[Java] view plain copy print? @ContextConfiguration (locations={"Classpath:applicationContext.xml", "Classpath:applicationcontext-security.xml "," File:src/main/webapp/web-inf/servlet.xml "})

I have shown here the two ways of configuring file paths. The first two are spring common configuration files, placed in the Classpath root directory, and "file" begins with the path is a fully qualified path, the default is relative to the actual project path, for example, the author uses Eclipse for development, This path is written in relation to the root directory of the folder where the project file resides. This applies to some Web-related configuration files that are not placed directly under Classpath, for example, in this example, a file that is placed in a common Web-inf directory.

Based on the above description, I wrote a spring test base class:

[Java]  View plain copy print? package com.test.basic;       import org.apache.commons.logging.log;    import org.apache.commons.logging.logfactory;   import org.junit.before;   import org.junit.runner.runwith;   import  org.springframework.test.context.contextconfiguration;   import  org.springframework.test.context.junit4.springjunit4classrunner;       @RunWith ( Springjunit4classrunner.class)    @ContextConfiguration (locations={/* "file:src/main/webapp/web-inf/ Wxiot-servlet.xml ",*/ " Classpath:applicationContext.xml ",             "Classpath:applicationcontext-security.xml"})        public  classtestbase {       protected Log logger =  Logfactory.getlog (testbase.class);              @Before   //Some common "initialization" codes        public void  before () {       }  }  

With this base class, we can write the test class of each module, as long as we define the test class and inherit Testbase, and add @test annotations in front of the specific method, I give a simple example of a test class:

[Java] view plain copy print?   Import Org.junit.Assert;   Import Org.junit.Test;       Import org.springframework.beans.factory.annotation.Autowired;   Import Com.reports.util.ActiveMQSender;       Import Com.test.basic.TestBase;             Public Classactivemqsendertest extends Testbase {@Autowired activemqsendersender; @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.