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();
        }
    
    }
  • 相关阅读:
    用fnmatch函数进行字符通配
    activity和service之间的相互通信方法
    IGMP协议简介
    Android2.2快速入门
    Android开发之旅:HelloWorld项目的目录结构
    Android的五大基本组件
    Android Service 组件
    TCP交互数据流 成块数据流
    为什么要进行IP选路?
    embOS实时操作系统 任务通讯
  • 原文地址:https://www.cnblogs.com/usual2013blog/p/4667425.html
Copyright © 2011-2022 走看看