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();
    }

}

 

posted @ 2015-07-22 14:45  唾手可得的树  阅读(1443)  评论(0编辑  收藏  举报