Using JUnit for spring testing

Source: Internet
Author: User

These days in the SPRINGMVC project, now summarize some of the problems encountered during the test.
Prior to the project, is in the newer MyEclipse (2013) above the MAVEN development, the Pom.xml file is copied directly, do the test time is relatively smooth. However this time, is in MyEclipse 8.5 above development, uses is JavaEE5, in the test time, has encountered many problems, sometimes quickly gets killed!
In general, we give the test Class A single package, and also give spring a test configuration file (copy the production configuration file, delete unused components, such as Shiro,ehcache, etc. temporarily unavailable). The component version I used:
 
   
  
  1. Spring 4.0.6
  2. Hibernate 4
  3. junit 4.8.2
Note that the spring-test in the form of annotations, Need to introduce Spring-test-4.0.6.release.jar then, do not write the test class, here with a car entity DAO example first is Car.java, for simplicity, car has only the Name property.
Test class:
  
 
  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = { "classpath*:test/root-context.xml" })
  3. @Transactional(readOnly=false)
  4. public class TestCarDao {
  5. @Autowired
  6. private CarDao dao;
  7. @Test
  8. public void test(){
  9. Car c = new Car();
  10. c.setName("0--0-----00--");
  11. dao.save(c);
  12. }
  13. }
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 annotation.

At this point, you can configure some JUnit's runtime environment to remove the javaEE5 reference, as shown in:

Run again, and if you don't see the servlet.**** class error, you can add your own Servlet.api.jar to the project class path. I'm using: Servlet-api-2.5.jar.
Test is now running normally.
Spring test is the default transaction rollback, that is, the database operation in test does not affect the actual data (except for the table structure of the update operation), if you need to modify the data, you can cancel the spring rollback, add the following annotations on the test method:
 
   
  
  1. @Rollback(false)




From for notes (Wiz)

Using JUnit for spring testing

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.