Mockito NullPointerException DataSourceTransactionManager.getTransaction()

Mockito NullPointerException DataSourceTransactionManager.getTransaction()

当我尝试模拟一个 DataSourceTransactionManager 时,我有一个 nullPointerException,我只测试了一个没有访问数据库的方法。

也许我做错了什么,但我不知道是什么。

测试类

@RunWith(MockitoJUnitRunner.class)
public class VdpDirectshipDAOImplUT {

    Logger logger = LoggerFactory.getLogger(getClass());

    @InjectMocks
    private VdpDirectshipDAOImpl dao;

    @Mock
    private DataSourceTransactionManager oracleTxManager;

    @Mock
    TransactionStatus transactionStatus;

    @Mock
    TransactionDefinition transactionDefinition;

    @Test
    public void testSubmitDSOrder() {
        // given
        int result = -1;
        VdpDirectShipSession vdpDirectShipSession = new VdpDirectShipSession();
        String vnetUserId = "10624802";

        // when
        try {
            result = dao.submitDSOrder(vdpDirectShipSession, vnetUserId);
            logger.info("result: {}", result);

        } catch (DirectShipDataAccessException e) {
            fail(e.getMessage());
        }

        // then
        assertTrue("SKU invalid correction", result > 0);
    }

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        oracleTxManager = Mockito.mock(DataSourceTransactionManager.class);
        Mockito.when(oracleTxManager.getTransaction(transactionDefinition)).thenReturn(Mockito.mock(TransactionStatus.class));
    }

}

DAO

public class VdpDirectshipDAOImpl extends SimpleJdbcDaoSupport implements
        VdpDirectshipDAO {

    /**
     * transactionManager for Oracle Database
     */
    private DataSourceTransactionManager oracleTxManager;


    ....

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class )
    public int submitDSOrder(final VdpDirectShipSession vdpDirectshipService,
                final String vnetUserId) throws DirectShipDataAccessException {

        ...
            final TransactionDefinition def = new DefaultTransactionDefinition();
            final TransactionStatus status = oracleTxManager.getTransaction(def);

        ...
    }
}

跟踪

java.lang.NullPointerException
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:317)
    at com.autozone.vendornet.vdpdirectship.dao.VdpDirectshipDAOImplUT.setup(VdpDirectshipDAOImplUT.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:592)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我正在使用 Java 5、Spring 2.0 和 junit 4.12,但我无法更新它们的版本。

我的正确实施是下一个。

@Spy 
private DataSourceTransactionManager oracleTxManager; 

...

oracleTxManager.setDataSource(Mockito.mock(DataSource.class, Mockito.RETURNS_MOCKS));