zoukankan      html  css  js  c++  java
  • spring验证事务的代码,用到了mockito

    package *.withdraw;
    
    import javax.annotation.Resource;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.InjectMocks;
    import org.mockito.Mockito;
    import org.mockito.MockitoAnnotations;
    import org.mockito.Spy;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.test.AbstractTransactionalSpringContextTests;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.TestExecutionListeners;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
    import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
    import org.springframework.transaction.PlatformTransactionManager;
    
    import com.github.springtestdbunit.DbUnitTestExecutionListener;
    import com.github.springtestdbunit.annotation.DatabaseOperation;
    import com.github.springtestdbunit.annotation.DatabaseSetup;
    import *.SasInfoDao;
    import *.SasInfo;
    import *.SettleBillManagerService;
    import *.WithdrawServiceImpl;
    
    
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "/applicationContext-test.xml" })
    @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class,
            TransactionalTestExecutionListener.class })
    @SuppressWarnings("deprecation")
    public class TransactionTest extends AbstractTransactionalSpringContextTests {
    
        private static Logger LOGGER = LoggerFactory.getLogger(TransactionTest.class);
    
        @Resource
        private PlatformTransactionManager transactionManager;
    
        @Spy
        @Resource
        private SasInfoDao sasInfoDao;
    
        @Resource
        private SettleBillManagerService settleBillManagerService;
    
        @InjectMocks
        private WithdrawServiceImpl withdrawServiceImpl;
    
        @Before
        public void init() {
            MockitoAnnotations.initMocks(this);
        }
    
        @Test
        @DatabaseSetup(value = { "/testData/sampleData.xml" }, type = DatabaseOperation.CLEAN_INSERT)
        public void testSettleBillIssuing() {
            this.setTransactionManager(transactionManager);
            this.setDefaultRollback(false);
            this.startNewTransaction();
            Mockito.doThrow(new RuntimeException("fu")).when(sasInfoDao).update(Mockito.any(SasInfo.class));
            settleBillManagerService.setSasInfoDao(sasInfoDao);
            withdrawServiceImpl.setSettleBillManagerService(settleBillManagerService);
            withdrawServiceImpl.operationSettleBillData(55L);
            SasInfo sasInfo = settleBillManagerService.findSettleBillById(54L);
            LOGGER.debug(sasInfo.toString());
            this.endTransaction();
        }
    
    }
  • 相关阅读:
    machine learning(11) -- classification: advanced optimization 去求cost function最小值的方法
    machine learning(10) -- classification:logistic regression cost function 和 使用 gradient descent to minimize cost function
    machine learning(9) -- classification:Decision boundary
    machine learning(8) -- classification
    day 23 对象的名称空间 类,对象属性和方法 封装 接口提供
    day 22 面向对象 类与对象 名称空间操作
    day 21 内存管理,正则
    day 14 三元运算符,列表字典推导式,递归,匿名函数,内置函数(排序,映射,过滤,合并)
    day 17 项目开发常用模块
    前端之jQuery
  • 原文地址:https://www.cnblogs.com/usual2013blog/p/4667425.html
Copyright © 2011-2022 走看看