spring测试junit事务管理及spring面向接口注入和实现类单独注入(无实现接口),实现类实现接口而实现类单独注入否则会报错。

时间:2021-09-17 03:37:55

1.根据日志分析,spring junit默认是自动回滚,不对数据库做任何的操作。

18:16:57.648 [main] DEBUG o.s.j.d.DataSourceTransactionManager - Switching JDBC Connection [net.sf.log4jdbc.sql.jdbcapi.ConnectionSpy@481d6644] to manual commit
18:16:57.649 [main] DEBUG o.s.t.c.t.TransactionalTestExecutionListener - No method-level @Rollback override: using default rollback [true] for test context [DefaultTestContext@4d95d2a2 testClass = TransDispatchingServiceTest, testInstance = com.autoyol.service.trans.TransDispatchingServiceTest@54b48b, testMethod = testCancelCauseNoSupport@TransDispatchingServiceTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@3b088d51 testClass = TransDispatchingServiceTest, locations = '{classpath:conf/applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]].
18:16:57.649 [main] INFO o.s.t.c.t.TransactionalTestExecutionListener - Began transaction (6) for test context [DefaultTestContext@4d95d2a2 testClass = TransDispatchingServiceTest, testInstance = com.autoyol.service.trans.TransDispatchingServiceTest@54b48b, testMethod = testCancelCauseNoSupport@TransDispatchingServiceTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@3b088d51 testClass = TransDispatchingServiceTest, locations = '{classpath:conf/applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]]; transaction manager [org.springframework.jdbc.datasource.DataSourceTransactionManager@491cceb4]; rollback [true]

2.事务配置和spring注入

package com.autoyol.service.trans;

import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional; import com.autoyol.service.trans.impl.TransDispatchingServiceImpl; /**
* @author xxx
* @function
* @date 2016年4月23日
* @version
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:conf/applicationContext.xml")
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
public class TransDispatchingServiceTest {
@Autowired
private TransDispatchingServiceImpl transDispatchingServiceImpl; //无面向接口编程,单独的类。
// @Autowired
// @Qualifier("testtestddd")
// private TransDispatchingServiceImpl testtestddd; //要么单纯实现类,要么面向接口注入。 @service("testtestddd") // @Autowired
// private TransDispatchingService transDispatchingService; //面向接口编程 //
// @Autowired
// private TransDispatchingMapper transDispatchingMapper;
Long orderNo = 449230224061l;
Integer carNo = 431319914;
@Test
public void testAddCarSelectUpgrade(){
int i = transDispatchingServiceImpl.addCarSelectUpgrade(orderNo);
System.out.println("添加车辆升级条件i="+i);
} /*@Test
public void testA(){
Map<String,Object> reqMap = new HashMap<String,Object>();
reqMap.put("orderNo", orderNo);
reqMap.put("renterNo", 123);
reqMap.put("rentTime", 201611l);
reqMap.put("revertTime", 201612l);
reqMap.put("ownerNo", 456);
reqMap.put("carNo", carNo);
reqMap.put("rentAmt", 300);
reqMap.put("insurance", 300);
reqMap.put("renterSt", 21);
reqMap.put("ownereSt", 21); int i = transDispatchingMapper.addTransDispatchingInitData(reqMap);
System.out.println("i="+i);
}*/ @Test
public void testAddTransDispatchingInitData(){
int i = transDispatchingServiceImpl.addTransDispatchingInitData(orderNo, 123, 201611l, 201612l, 456, carNo, 300, 300, 21, 21);
System.out.println("添加调度初始化数据i="+i);
}
@Test
public void testUpdateTransDispatchingStatus(){
int i = transDispatchingServiceImpl.updateTransDispatchingStatus(orderNo, carNo, 2, null, 1);
System.out.println("修改调度状态i="+i);
}
@Test
public void testQueryTransDispatchingStatus(){
System.err.println("transDispatchingServiceImpl="+transDispatchingServiceImpl);
// System.err.println("transDispatchingService="+transDispatchingService);
Map map = transDispatchingServiceImpl.queryTransDispatchingStatus(orderNo, carNo);
System.err.println("查询当前的调度状态:map="+map);
}
@Test
public void testRefuseCauseNoSupport(){
int i = transDispatchingServiceImpl.refuseCauseNoSupport(orderNo, carNo);
System.out.println("调度原因i="+i);
}
@Test
public void testRefuseCauseLessTime(){
int i = transDispatchingServiceImpl.refuseCauseLessTime(orderNo, carNo);
System.out.println("调度原因i="+i);
}
@Test
public void testCancelCauseNoSupport(){
int i = transDispatchingServiceImpl.cancelCauseNoSupport(orderNo, carNo);
System.out.println("调度原因i="+i);
}
@Test
public void testCancelCauseLessTime(){
int i = transDispatchingServiceImpl.cancelCauseLessTime(orderNo, carNo);
System.out.println("调度原因i="+i);
}
@Test
public void testManualCauseNoSupport(){
int i = transDispatchingServiceImpl.manualCauseNoSupport(orderNo, carNo);
System.out.println("调度原因i="+i);
}
@Test
public void testAutoCauseLessTime(){
int i = transDispatchingServiceImpl.autoCauseLessTime(orderNo, carNo);
System.out.println("调度原因i="+i);
}
@Test
public void testUpdateTransDispatchingSucc(){
int i = transDispatchingServiceImpl.updateTransDispatchingSucc(orderNo, carNo, 77, 88, 99, 100);
System.out.println("调度成功i="+i);
}
}