Spring JUnit Testing: Auto-Threading and Transactional Issues

We have an existing Java EE application that uses Spring and moves from the xml configuration to auto-university. We just converted most of our EJBs to Spring beans, but currently still use MDB and EJB timers.

  • WAS 7.0
  • Java 6
  • Spring 3.0.5
  • JUnit 4.8.1

I am also in the process of writing integration tests in JUnit. My integration tests use most context configuration files, as when working in WAS, but not in terms of JNDI or JTA transaction manager. For them, I have equivalents that install ActiveMQ queues, Hibernate transaction manager, etc.

My test is as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/junit-container-context.xml",
    "/spring-contexts/service-context.xml",
    "/spring-contexts/integration-context.xml",
    "/available-tests-context.xml" })
public class TestCase1Runner {

    @Autowired
    TestCase1 test;

    @Autowired
    private ApplicationContext applicationContext;

    @Before
    public void setupErrorHandling() {
        // Some setup
    }

    @Test
    @Transactional
    public void run() throws Exception {
        test.executeTest();
    }
}

My tests have problems for at least several reasons:

  • Autowiring beans
  • beans

1, , . beans, . . , . xml , - , . ( , :):

<context:component-scan
    base-package="com.mycompany.package1,com.mycompany.package2" />

<context:annotation-config />

, , .

. , beans, , Spring, , .

2 1. beans . JUnit :

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" />
    </tx:attributes>
</tx:advice>

<aop:config>
    <aop:pointcut id="servicePointcut"
        expression="execution(public * com.mycompany.package1..*.*(..))" />

    <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" />
</aop:config>

, , beans . beans com.mycompany.package1.

, , WAS. ContextSingletonBeanFactoryLocator. JUnit, , , .

.

+3
1

, :

AbstractTransactionalJUnit4SpringContextTests

:

@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)

defaultRollback , .

0

Source: https://habr.com/ru/post/1785933/


All Articles