oghenez / mycila

Automatically exported from code.google.com/p/mycila
0 stars 0 forks source link

NullPointerException when using Spring to manage Integration Test transactions #16

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I was attempting to utilize Spring's TransactionTestExecutionListener to
manage transaction and do auto-rollbacks on dao integration tests.  These
tests are Mycila annotated.  When the Spring following Spring annotations
were added to test class, a NullPointer Exception is thrown. 

@TestExecutionListeners ( { TransactionalTestExecutionListener.class }) 
@Transactional

Here is the stack trace:
java.lang.NullPointerException
        at foo.DataDaoTest.testSaveData(DataDaoTest.java:75)
        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:597)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall
(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run
(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively
(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate
(InvokeMethod.java:20)
        at com.mycila.testing.junit.MycilaJunitRunner$3.evaluate
(MycilaJunitRunner.java:85)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild
(BlockJUnit4ClassRunner.java:76)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild
(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at com.mycila.testing.junit.MycilaJunitRunner$2.evaluate
(MycilaJunitRunner.java:66)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
(JUnit4TestReference.java:45)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run
(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:460)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:673)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:386)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
(RemoteTestRunner.java:196) 

Here is an example of the Mycila test class annotated:

@SpringContext (locations = { "classpath:/applicationContext.xml",
    "classpath:/applicationContext-resources.xml",
    "classpath:/applicationContext-dao.xml",
    "classpath:/applicationContext-test.xml",
    "classpath:/applicationContext-service.xml", })
@TestExecutionListeners
( { TransactionalTestExecutionListener.class })
@Transactional
@RunWith (MycilaJunitRunner.class)
public class DataDaoTest
{
    @Autowired
    private ApplicationContext context;

    @Autowired
    private TestContext testContext;

    @Autowired
    private DataDao dao;

    @Test
    public void testSaveData ()
    {
        Data data = new Data();
        dao.saveData(new Long(1413646L), data);
    }

}

In the Google Group conversation thread regarding this issue
(http://groups.google.com/group/mycila/t/a24fc78c0edcd407) I was asked the
rerun the test with Breakpoints on.  Here is what happens:

- TestContextManager.constructor called
- TestContextManager.prepareTestInstance called
- DataDaoTest.testSaveData called
- NullPointerException thrown from the saveData line.

Original issue reported on code.google.com by jmcg...@gmail.com on 3 Dec 2009 at 7:10

GoogleCodeExporter commented 8 years ago
Hi,

Did you checked what is null at line 75 from your DAO ? To be sure it's 
relative to
the transactional listener...

java.lang.NullPointerException
        at foo.DataDaoTest.testSaveData(DataDaoTest.java:75)

As i can see for now, beforeTest and afterTest methods of TestContextManager 
are not
called. This won't be hard to fix it: I'll try to fix it quickly.

Original comment by mathieu....@gmail.com on 3 Dec 2009 at 7:45

GoogleCodeExporter commented 8 years ago

Original comment by mathieu....@gmail.com on 3 Dec 2009 at 7:46

GoogleCodeExporter commented 8 years ago

Original comment by mathieu....@gmail.com on 3 Dec 2009 at 7:46

GoogleCodeExporter commented 8 years ago
Issue fixed in svn, will deploy release 2.3 this evening

Original comment by mathieu....@gmail.com on 3 Dec 2009 at 9:42

GoogleCodeExporter commented 8 years ago
This version is available in Maven 2 central repository and contains:

* A fix for Spring Plugin: Spring Test Listeners where not invoked correctly 
for each
test method

Here is an example of test using @Transactional:

@RunWith(MycilaJunitRunner.class)
@SpringContext(locations = "/ctx-tx.xml")
@TransactionConfiguration(transactionManager = "myTransactionManager")
public class SpringTxTest {

    @Autowired
    Dao dao;

    @Test
    @Transactional
    public void test() {
        dao.save();
    }
}

* The name attribute of @Bean annotation is now optional. If not provided, the 
name
of the field or method is used.

@Bean
String abean = "helloa";

* Spring Java config is now supported in @SpringContext annotation through the
classes attribute:

@RunWith(MycilaJunitRunner.class)
@SpringContext(locations = "/ctx-autowired2.xml", classes = MyJavaConfig.class)
@TransactionConfiguration(transactionManager = "myTransactionManager")
public class SpringJavaConfigTest {

    @Autowired
    Dao dao;

    @Autowired
    Autowired2Bean autowired2Bean;

    @Test
    @Transactional
    public void test() {
        assertNotNull(dao);
        assertNotNull(autowired2Bean);
        assertNotNull(autowired2Bean.dao);
        dao.save();
    }
}

Original comment by mathieu....@gmail.com on 4 Dec 2009 at 3:19