junit4.9 test Case Spring test Case Assert Annotation

Source: Internet
Author: User

junit4.9 Test Cases

Test base class

[JavaScript]View PlainCopy
  1. Import Org.junit.runner.RunWith;
  2. Import org.springframework.test.context.ContextConfiguration;
  3. Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  4. @RunWith (Springjunit4classrunner. Class) //Use JUNIT4 for testing
  5. @ContextConfiguration
  6. ({"/*.xml"}) //load config file
  7. ------------If you add the following code, all test classes that inherit the class will follow that configuration, or you can control the transaction on the method of the test class, as in the next instance
  8. @Transactional This is very important, if you do not add this annotation configuration, transaction control will be completely invalidated!
  9. //
  10. The transaction here is associated to the transaction controller in the configuration file (TransactionManager = "TransactionManager"), and//specifies an automatic rollback (Defaultrollback = true). The data in this operation will not pollute the database!
  11. @TransactionConfiguration (TransactionManager = "TransactionManager", Defaultrollback = True)
  12. //------------
  13. Public class Basejunit4test {
[JavaScript]View PlainCopy
    1. <span style="White-space:pre" > </span> ...
    2. }

About the transaction configuration file point here


Sub-class

[Java]View PlainCopy
    1. Public class Userservicetest extends Basejunit4test {
    2. @Resource //automatic injection, by default by name
    3. private Iuserservice Userserviceimpl;
    4. @Test
    5. public void Hasmatchuser () throws Exception {
    6. Boolean B1 = Userserviceimpl.hasmatchuser ("admin", "123456");
    7. Boolean b2 = Userserviceimpl.hasmatchuser ("admin", "111111");
    8. Assert.asserttrue (B1);
    9. Assert.asserttrue (!B2);
    10. }
    11. @Test
    12. public void Getuserbyname () throws Exception {
    13. User user1 = userserviceimpl.getuserbyname ("admin");
    14. User user2 = Userserviceimpl.getuserbyname ("admin2");
    15. Assert.assertnotnull (user1);
    16. Assert.assertnull (User2);
    17. }
    18. @Test
    19. <strong>@Transactional //indicate that this method requires the use of transactions
    20. <span style="White-space:pre" > </span>@Rollback (true) //indicates that the transaction does not roll back after this method is used and true is a rollback </ strong>
    21. public void Loginsuccess () throws Exception {
    22. User user1 = userserviceimpl.getuserbyname ("admin");
    23. Userserviceimpl.loginsuccess (user1);
    24. }
    25. }

junit4.9 test Case Spring test Case Assert Annotation

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.