0
Ich habe eine nullPointerException
, wenn ich versuche, eine DataSourceTransactionManager
mock, ich nur eine Methode ohne Zugriff auf die DB zu testen.Mockito NullPointerException DataSourceTransactionManager.getTransaction()
Vielleicht mache ich etwas falsch, aber ich weiß nicht was.
Testclass
@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);
...
}
}
Trace
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$1.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$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.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)
Ich benutze Java 5, Spring 2.0 und Junit 4.12 und ich kann ihre Versionen nicht aktualisieren.
Schließlich war mein OracleTransaction Object ein @Spy und wenn gesetzt Mocks to datasourse Eigenschaft. '@Spy \t privater DataSourceTransactionManager oracleTxManager; oracleTxManager.setDataSource (Mockito.mock (DataSource.class, Mockito.RETURNS_MOCKS)); ' –