简体   繁体   中英

Upgraded to Spring 3 and junit 11 - Context becoming null after running a test. No autowired bean available for the second test

I have a weird problem. After upgrading to Spring 3 and junit 11, the tests are not working as expected.

When I run the tests individually from intellij everything works fine as expected. However on running through maven, the tests fail on Autowiring. Also this is not consistent. The first tests in the test class run gets the autowired class and runs, but the next test fails with null pointer exception on the autowired bean

Here is the test Class:

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations = {"classpath:dao-export-test.xml", "classpath:jndidatasource-config.xml"})
 @TransactionConfiguration(transactionManager = "transactionManager.console", defaultRollback = true)
 @TestExecutionListeners({
    DependencyInjectionTestExecutionListener.class,
    DirtiesContextTestExecutionListener.class,
    TransactionalTestExecutionListener.class
 })
@Transactional
public class ActivityLogDaoTest  {
private final Logger logger = Logger.getLogger(getClass());
ActivityLog activityLog;

@Autowired
private ActivityLogDao activityLogDao;
private static final String CUSTOMER_KEY = "king-kong-systems";

@Before
public void setUp() {
    activityLog = new ActivityLog();
    activityLog.setReferenceId(2000L);
    activityLog.setAmount(BigDecimal.TEN);
    activityLog.setCustomerKey(CUSTOMER_KEY);
    activityLog.setSource(ActivityLogSource.BALANCE_LINE_ITEM);
    activityLog.setType(ActivityType.UNKNOWN);
    activityLog.setDescription("Initial purchase for server blades at rackspace");
}


@Test
public void testAddNewLogRecord() {
    logger.info("Adding new record to activity log");
    try {
        activityLogDao.createLogEntry(activityLog);
        Assert.assertTrue(true);
    } catch (Exception e) {
        Assert.assertTrue(false);
    }
}

@Test
public void testGetLogsForMerchant() {
    logger.info("Get new record for merchant");
    //add two records
}

The xml configurations are:

 <context:spring-configured/>    
 <context:annotation-config/>

<bean id="activityLogDao" class="com.core.dao.hibernate.ActivityLogDaoImpl" parent="baseDao.console">
    <property name="databaseToUse" value="SNAPSHOT"/>
</bean>

Debugging is getting more diffcult as the test passes through intellj, passed when I just have one test in the test class; but the moment I have another test it fails on autowiring. Could it be something related to @Transactional?

On debugging further, I am seeing that the context is getting lost after running a test and during setup on the next test, context comes in as null. I am not sure of why this is happening.

Please help!

The issue seems to be happening on running maven tests in parallel. This was not happening earlier for in spring 2.5.6. However I am not facing the issue after removing the parallel method in maven-test-surefire plugin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM