1.abstracttransactionaljunit4springcontexttests and Abstractjunit4springcontexttests. We are going to inherit one of the two types in the test case class.
Abstracttransactionaljunit4springcontexttests provides automatic database rollback, which means the database is the same before and after the test
Abstractjunit4springcontexttests does not provide automatic database rollback, and testing destroys the database.
View source discovery, Abstracttransactionaljunit4springcontexttests is inherited from the abstractjunit4springcontexttests, at the same time the class above two more annotation: @TestExecutionListeners (Transactionaltestexecutionlistener.class) and @transactional. So there are two ways to implement database rollback, as follows:
Public class Basictest extends Abstracttransactionaljunit4springcontexttests {}
And
@TestExecutionListeners (Transactionaltestexecutionlistener. Class) @Transactionalpublicclass Basictest extends abstractjunit4springcontexttests {}
Well, it's better to go straight to the code if you say so much useless.
To avoid having to configure the spring environment for each test case, first create a parent class: Basictest, the content is simple, really simple:
@ContextConfiguration (locations= {"classpath:spring/applicationcontext.xml"," classpath:spring/spring-servlet.xml"})publicclass {}
@ContextConfiguration: As the name implies is to configure the address of the context configuration file, I here is the Web program, so also to Spring-servlet.xml.
There is already a @runwith on the Abstractjunit4springcontexttests class, so there is no need for it. As long as you configure @contextconfiguration.
Then the test case inherits Basictest as follows: It is needless to say how to write a specific test method.
Public classPeopleservicetest extends basictest{@Resource (name="Peopleservice") PrivatePeopleservice PS; @Test Public voidQueryall () {List<People> List=ps.queryall (NULL); System. out. println (list); Assert.assertequals ("list does not have 10",Ten, List.size ()); } @Test Public voidUpdate () {people P=NewPeople ().Set("ID", in).Set("name","Tom").Set("Address","").Set(" Age",Ten); Assert.assertequals ("Not modified",1, This. Ps.update (p)); } @Test Public voidInsert () {people P=NewPeople ().Set("name","Mike").Set("Address","America").Set(" Age",Ten); Assert.assertequals ("not inserted",1, This. Ps.add (p)); }}
Spring 4.0 junit Simple Dao,service test