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:
Spring 4.0.6
Hibernate 4
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:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:test/root-context.xml" })
@Transactional(readOnly=false)
public class TestCarDao {
@Autowired
private CarDao dao;
@Test
public void test(){
Car c = new Car();
c.setName("0--0-----00--");
dao.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 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:
@Rollback(false)
From for notes (Wiz)
Using JUnit for spring testing