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