DAO层JUNIT测试的PlatformTransactionManager配置问题

丝雨 1年前

编写DAO层JUNIT测试时,出现下面的错误:

2017-04-14 15:46:08 [main] WARN context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@6c3708b3] to process 'before' execution of test method [public void com.chengshu.service.OrgServiceTest.test()] for test instance [com.chengshu.service.OrgServiceTest@654f0d9c]
java.lang.IllegalStateException: Failed to retrieve PlatformTransactionManager for @Transactional test for test context [DefaultTestContext@9225652 testClass = OrgServiceTest, testInstance = com.chengshu.service.OrgServiceTest@654f0d9c, testMethod = test@OrgServiceTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@76908cc0 testClass = OrgServiceTest, locations = '{file:src/main/resources/conf/mybatis-config.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]].
	at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:194)
	at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:269)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)

不知道什么原因,还请大神指教.


被测试接口代码如下:

public interface OrgService {
	
	Org getOrgById( String id ) throws Exception;

	int addOrg( Org org ) throws Exception;

	...
	
}

测试代码如下:

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = {"file:src/main/resources/conf/mybatis-config.xml"})  
@Transactional  
@Rollback(true) 
public class OrgServiceTest {

	 
    private OrgService OrgService; 

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void test() {

		Org org = new Org();
		org.setId("gfdkjsghkdjshfgkjhsfd");
		org.setAddress("http://www.test.com");
		org.setBusinessOfAccess("测试客服");
		org.setContactNumber("021-888888");
		org.setOrgNameCn("测试1号");
		try {
			OrgService.addOrg(org);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		try {
			assertOrgEqualsExactly(org,OrgService.getOrgById("gfdkjsghkdjshfgkjhsfd"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void assertOrgEqualsExactly(Org expectOrg,Org realOrg){
	    ...
	}

}

mybatis 配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="${jdbc_url}"/>
                <property name="username" value="${jdbc_username}"/>
                <property name="password" value="${jdbc_password}"/>
                <property name="poolMaximumActiveConnections" value="100"/>
                <property name="poolMaximumIdleConnections" value="5"/>
                <property name="poolPingEnabled" value="true"/>
                <property name="poolPingQuery" value="select 1"/>
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
    	<mapper resource="mapping/AdminReportMapper.xml"/>
            ...
        <mapper resource="mapping/UserSessionMapper.xml"/>
    </mappers>
    
</configuration>


*
*