Spring 4.0 junit Simple Dao,service test

Source: Internet
Author: User
Tags assert

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

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.