1. We know that JUnit tests are available in both J2SE and Android, and using JUnit can help facilitate the testing of code. In my previous blog, I also wrote some examples of junit tests in J2SE, today for a little discussion of junit in spring.The jar package required for this sp
The JUnit Unit test framework is common but can disrupt database locality and a large number of hard-coded occurrences (Applicationcontext.getbean (""));Today, Buddy, let's introduce some annotations in spring test, a spring-brought jar package, to implement unit testing + transaction rollback + annotation Dependency InjectionThere are not many tutorials on the w
Suppose the spring configuration file is Applicationcontext.xml
the spring configuration file is below the Classpath
In spring's Java application, our spring configuration files are placed below the classpath (or, after compilation, into the classes directory).
Here's my project, because it's managed with MAVEN, so the configuration files are all placed under the
Use Maven to create a WebApp project online a lot of information, not in detail here. After the completion of the creation, it is said to switch to the Dynamic Web project to change to 3.0 version, but I do not locally switch, the online method seems useless, and temporarily useless to this piece. After the SSM framework is set up, let's elaborate on this piece. Today's talk is about the pit for the JUnit unit test that was encountered when building t
Org.springframework.test.context.ActiveProfiles;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Import org.springframework.test.context.transaction.TransactionConfiguration;
Import org.springframework.transaction.annotation.Transactional; Used to illustrate the runner of this test class, where Springjunit4classrunner @RunWith (springjunit4classrunner.class)//Speci
);Sysemployeedao.saveemployee (employee);Find employees by codeList Emps = Sysemployeedao.findbynamedparam (New string[]{"Employeecode"},New String[]{empcode});Assert.asserttrue ("Saveemployee Method logic error, employee save failed!",!emps.isempty ());}}Note Three annotations on a class:Specifies the run of the test case here is the specified JUNIT4@RunWith (Springjunit4classrunner.class)Specify spring's profile path relative to Classpath@ContextConfiguration ({"/meta-inf/
In the spring MVC + mybatis project, We sometimes need to inject the DAO operations database into the test code, and make the table to be added and removed, to achieve the following:This is the general MAVEN project project structureThe test code is generally written under the Src/test/java Package.This is a common test class that queries the data of a table by Mybatis.1 public classSpringmybatistest {2 3 @Resource4 Private StaticTestdao testdao;
Consolidate JUnit for testing:
JUnit has natural support for spring, and just add two annotation to start the spring context, the sample code is as follows:
@ContextConfiguration ("/meta-inf/spring/integration/inbound-gateway-config.xml")
@RunWith ( Springjunit4classrunner.c
I. Join a Dependency package
Spring Test (e.g. Spring-test-2.5.4.jar)
JUnit 4
Spring Other related packages
II. New JUnit Test caseIii. reading a configuration file@RunWith (Springjunit4classrunner.class) @ContextConfiguration (locations = {"Classpath:
It's been a long time since JUnit was tested, and today, using JUnit tests under the spring framework.I looked up some information on the Internet to find that my project is missing some jar package, now put this jar package information up, so as not to forget laterJUnit test, using in spring
Unit testing Benefits Needless to say, JUNIT4 and Spring-test provide a more convenient way.
Import related jars, note version
JUnit-related annotations: @Test test methods The primary annotation @BeforeClass the first run, a global execution @Before run the @After test method before the test method runs, perform @AfterClass last run, and perform a global @Ignore ignore Perform
To create a test class and
out), you can use the Abstracttransactionaljunit4springtests class. The use of transaction management is the same as the normal use of spring transaction management. It should be noted that if you want to use declarative transaction management, that is, using the Abstracttransactionaljunitspringcontexttests class, Please include the TransactionManager bean in the Applicationcontext.xml file:
class= "Org.springframework.jdbc.datasource.DataSourceTrans
Precautions for maven + spring + junit testing: mavenjunit
There are a lot of online documents for creating a webapp project using maven, which is not detailed here. After the creation, I mentioned that I had to switch to version 3.0 when switching to a dynamic web project, but I couldn't switch it locally. The method on the Internet seems useless, and I haven't used it for the moment. After the SSM framew
) public class Testcardao {@Autowiredprivate Cardao dao; @Testpublic void Test () {Car c = new Ca R (); C.setname ("0--0-----00--");d Ao.save (c);}}As you can see, it is convenient to use annotations to configure JUnit. Run the test class using JUnit, starting with: java.lang.noclassdeffounderror:lJavax.persistence.ForeignKey () errorafter the investigation, found JavaEE5 Javaee.jar did not foreignkey this
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 obtain
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.