[Z] use the runwith annotation to change the default execution class of JUnit and implement its own listener

Source: Internet
Author: User

Use the runwith annotation to change the default execution class of JUnit and implement its own listener unit test at ordinary times. If the runwith annotation is not used, JUnit will use the default execution class suite for execution, as shown below:

[Java]View plaincopy
  1. Public class testclass {
  2. @ Test public void T1 (){}
  3. }

JUnit allows you to specify other unit test execution classes. You only need to inherit org. JUnit. Runners. blockjunit4classrunner from the test execution class. Spring execution class springjunit4classrunner inherits this class. We usually use spring. To make it easier to reference the configuration file, we use the execution class implemented by spring for unit testing. The unit test execution class will look like this:

[Java]View plaincopy
  1. @ Runwith (springjunit4classrunner. Class)
  2. @ Contextconfiguration (locations = {"classpath *:/spring1.xml", "classpath *:/spring2.xml "})
  3. @ Testexecutionlisteners ({dependencyinjectiontestexecutionlistener. Class, transactionaltestexecutionlistener. Class })
  4. @ Transactional
  5. Public class testclass {
  6. @ Inject
  7. // This class will be injected during execution. Here it is injection by type. To inject by name, add the @ named annotation, for example, @ named ("class1 ")
  8. // The implementation class can be annotated with @ named ("class1") or configured in the configuration file
  9. Class1 class1;
  10. @ Test
  11. Public void T1 (){}
  12. }

The annotations used are described as follows:

@ Runwith: This is the specified unit test execution class. springjunit4classrunner. Class is specified here;

@ Contextconfiguration: Specifies the path of the spring configuration file. You can specify multiple files at the same time;

@ Testexecutionlisteners: specifies some actions that can be performed before the test class is executed, such as dependencyinjectiontestexecutionlistener. class to inject the dependencies in a test class, transactionaltestexecutionlistener. class is used to manage transactions. Both of these are built-in with srping. We can also implement our own listener class to complete our own operations. We only need to continue with the class Org. springframework. test. context. support. abstracttestexecutionlistener. For more information, see dependencyinjectiontestexecutionlistener. class implementation, I will post the instance later. The listener is executed before the implementation class is executed and before the test method of the implementation class is executed, which is related to the implementation of listener.

@ Transactional: @ transactional is not required here. It is consistent with transactionaltestexecutionlistener in @ testexecutionlisteners. class is used together to ensure that the test data in the inserted database is rolled back after the test, and the inserted data is deleted to ensure the database is clean. If the specified @ transactional is not displayed, the data inserted into the database is actually inserted.

Our unit tests usually involve database operations, so we need to read data from the database and perform logical processing. In order to ensure the database is clean, to ensure the accuracy and correctness of the test data, we 'd better Insert the test data we prepared before the test, and then delete the data after the test is complete, in this case, we can add a listener to prepare the test data we need and combine it with the transaction management above, so that we won't actually commit it to the database.

The following example is a simple listener implementation, but the function is to print the configuration file path in the annotation we configured on method, because there are a lot of code to integrate dbunit and insert the database, I will not post it here. 1. First, we need to prepare an annotation to identify the data file whose parameters are used for testing: dbunittestdata. Java

[Java]View plaincopy
  1. @ Retention (retentionpolicy. runtime)
  2. @ Target (elementtype. Method)
  3. @ Inherited
  4. @ Brief ented
  5. Public @ interface dbunittestdata {
  6. Public String [] datasetlocations ();
  7. }

2. Compile listener. The Listener name is defined as dbunittestexecutionlistener. Class: dbunittestexecutionlistener. java.

[Java]View plaincopy
  1. Import org. springframework. Test. Context. testcontext;
  2. Import org. springframework. Test. Context. testexecutionlistener;
  3. Public class dbunittestexecutionlistener implements testexecutionlistener {
  4. Public void preparetestinstance (testcontext) throws exception {
  5. }
  6. Public void beforetestclass (testcontext) throws exception {
  7. // Nothing to do
  8. }
  9. Public void aftertestclass (testcontext) throws exception {
  10. // Nothing to do
  11. }
  12. Public void beforetestmethod (testcontext) throws exception {
  13. Dbunittestdata dbunitrefresh = testcontext. gettestmethod (). getannotation (dbunittestdata. Class );
  14. If (dbunitrefresh = NULL ){
  15. Return;
  16. }
  17. String [] datasetlocations = dbunitrefresh. datasetlocations ();
  18. Loadtestdata (testcontext, datasetlocations );
  19. }
  20. Public void aftertestmethod (testcontext) throws exception {
  21. // Nothing to do
  22. }
  23. Private void loadtestdata (testcontext, string [] datasetlocations ){
  24. If (datasetlocations = NULL | datasetlocations. Length = 0 ){
  25. Return;
  26. }
  27. For (string datasetlocation: datasetlocations ){
  28. // Do what you want to do with the data set files
  29. System. Out. println (datasetlocation );
  30. }
  31. }
  32. }

3. Add the listener implementation class to @ testexecutionlisteners of the test class and add the annotation @ dbunittestdata to the method. In this case, the test class will be like this:

[Java]View plaincopy
  1. @ Runwith (springjunit4classrunner. Class)
  2. @ Contextconfiguration (locations = {"classpath *:/spring1.xml", "classpath *:/spring2.xml "})
  3. @ Testexecutionlisteners ({dependencyinjectiontestexecutionlistener. Class, transactionaltestexecutionlistener. Class, dbunittestexecutionlistener. Class })
  4. @ Transactional
  5. Public class testclass {
  6. @ Inject
  7. // This class will be injected during execution. Here it is injection by type. To inject by name, add the @ named annotation, for example, @ named ("class1 ")
  8. // The implementation class can be annotated with @ named ("class1") or configured in the configuration file
  9. Class1 class1;
  10. @ Test
  11. @ Dbunittestdata (datasetlocations = {"classpath:/testdata/testdata1.xml", "classpath:/testdata/testdata2.xml "})
  12. Public void T1 (){}
  13. }

4. Run this method to view the data file path of the @ dbunittestdata annotation.

[Z] use the runwith annotation to change the default execution class of JUnit and implement its own listener

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.