junit4.9 Test Cases
Test base class
[JavaScript]View PlainCopy
- Import Org.junit.runner.RunWith;
- Import org.springframework.test.context.ContextConfiguration;
- Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- @RunWith (Springjunit4classrunner. Class) //Use JUNIT4 for testing
- @ContextConfiguration
- ({"/*.xml"}) //load config file
- ------------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
- @Transactional This is very important, if you do not add this annotation configuration, transaction control will be completely invalidated!
- //
- 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!
- @TransactionConfiguration (TransactionManager = "TransactionManager", Defaultrollback = True)
- //------------
- Public class Basejunit4test {
[JavaScript]View PlainCopy
- <span style="White-space:pre" > </span> ...
- }
About the transaction configuration file point here
Sub-class
[Java]View PlainCopy
- Public class Userservicetest extends Basejunit4test {
- @Resource //automatic injection, by default by name
- private Iuserservice Userserviceimpl;
- @Test
- public void Hasmatchuser () throws Exception {
- Boolean B1 = Userserviceimpl.hasmatchuser ("admin", "123456");
- Boolean b2 = Userserviceimpl.hasmatchuser ("admin", "111111");
- Assert.asserttrue (B1);
- Assert.asserttrue (!B2);
- }
- @Test
- public void Getuserbyname () throws Exception {
- User user1 = userserviceimpl.getuserbyname ("admin");
- User user2 = Userserviceimpl.getuserbyname ("admin2");
- Assert.assertnotnull (user1);
- Assert.assertnull (User2);
- }
- @Test
- <strong>@Transactional //indicate that this method requires the use of transactions
- <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>
- public void Loginsuccess () throws Exception {
- User user1 = userserviceimpl.getuserbyname ("admin");
- Userserviceimpl.loginsuccess (user1);
- }
- }
junit4.9 test Case Spring test Case Assert Annotation