Junit test of Spring mvc framework (implement your own Listener)

I Unit testing using spring JUnit 4classrunner

1,Annotate the test class

@RunWith(SpringJUnit4ClassRunner.class)

2. Load the required configuration file

@ContextConfiguration(locations={"/spring-gateway-py.xml","classpath:spring-repository-context.xml"})

3. Transaction configuration

@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)

 

Here, you need to explain that defaultRollback = true: true = the test data will not pollute the database

Of course, the database is not polluted here, but the data can not be seen in the table. However, if the primary key you set is automatically increased, it can be clearly seen that although the data is rolled back, when you try not to roll back the new data to the database, you will find that the ID is not continuous So it is not really pollution-free

4,@TestExecutionListeners

This is used to specify some actions that can be done before the test class is executed, such as dependencyinjectiontestexecutionlistener Class, you can inject dependencies in a test class, transactionaltestexecutionlistener Class is used to manage transactions; These two are Srping's own; We can also implement our own Listener class to complete our own operations. We only need to inherit the class org springframework. test. context. support. Abstracttestexecutionlistener is OK

5,@Transactional

Here @ Transactional is not required. Here is the transactionaltestexecutionlistener in @ TestExecutionListeners Class is used together to ensure the test data in the inserted database. After the test, the transaction is rolled back and the inserted data is deleted to ensure the cleanness of the database. If the specified @ Transactional is not displayed, the data inserted into the database is real.

2, Testing

1. Preparing custom annotations

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Inherited
@Documented
public @interface DBUnitTestData {
 
	public String[] dataSetLocations();
}

2. Write a Listener whose name is defined as dbunittestexecutionlistener java

import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
 
public class DBUnitTestExecutionListener implements TestExecutionListener {
 
	public void prepareTestInstance(TestContext testContext) throws Exception {
	}
 
	public void beforeTestClass(TestContext testContext) throws Exception {
		// Nothing to do
	}
 
	public void afterTestClass(TestContext testContext) throws Exception {
		// Nothing to do
	}
 
	public void beforeTestMethod(TestContext testContext) throws Exception {
		DBUnitTestData dbUnitRefresh = testContext.getTestMethod().getAnnotation(DBUnitTestData.class);
		if (dbUnitRefresh == null) {
			return;
		}
		String[] dataSetLocations = dbUnitRefresh.dataSetLocations();
		loadTestData(testContext, dataSetLocations);
	}
 
	public void afterTestMethod(TestContext testContext) throws Exception {
		// Nothing to do
	}
 
	private void loadTestData(TestContext testContext, String[] dataSetLocations) {
		if (dataSetLocations == null || dataSetLocations.length == 0) {
			return;
		}
		for (String dataSetLocation : dataSetLocations) {
			//Do what you want to do with the data set files
			System.out.println(dataSetLocation);
		}
	}
 
}

3. Add the Listener implementation class to the @ TestExecutionListeners of the test class, and add the annotation @ DBUnitTestData on the method. At this time, the test class will be as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/spring1.xml", "classpath*:/spring2.xml" })
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class,DBUnitTestExecutionListener.class })
@Transactional
public class TestClass {
	@Inject
	//This class will be injected during execution. Here, it is injected by type. If you want to inject by name, you need to add @ Named annotation, such as @ Named("class1")
	//The implementation class can be annotated with @ Named("class1") or configured in the configuration file
	Class1 class1;
	
	@Test 
	@DBUnitTestData(dataSetLocations={"classpath:/testData/testData1.xml","classpath:/testData/testData2.xml"})
	public void  t1(){}
}

4. After executing this method, you can see that the data file path of @ DBUnitTestData annotation is printed.

 

Keywords: Junit unit testing Spring MVC

Added by immot on Sat, 15 Jan 2022 00:10:10 +0200