开发者

Spring Autowiring not working in classes being tested

I'm attempting to run high level test on a portion of code used in a web service I'm writing. During the process, the code is expected to make calls to a database (which I've set up using Spring and Hibernate). Autowiring is working fine in the test case class itself, but other classes that are being invoked as a result of the test being run are not being Autowired.

Unit Test

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration
public class ProviderTest
{
@Autowired
protected ApplicationContext applicationContext;
protected ProviderA provider;

@Before
public void setUp()
{
    Transaction transaction = DbLogHelper.getInstance().getLog();
    transaction.setCompanyName("Unit Test");
    transaction.setCreateTime(Calendar.getInstance());
    transaction.setPhoneNumber("Provider Test - N/A");
    transaction.setQueryId("Unit Test");
    transaction.setRequest("<NONE - UNIT TEST>");
    transaction.setResponse("<NONE - UNIT TEST>");
    LogJob job = new LogJob(transaction, DbLogHelper.getInstance.getDao());
    job.run();

    provider = new ProviderA(transaction);
}

@After
public void tearDown()
{
    Transaction transaction = DbLogHelper.getInstance().getLog();
    transaction.setResponseTime(Calendar.getInstance());
    DbLogHelper.getInstance().writeLog(transaction);
    DbLogHelper.getInstance().resetLog();
}

@Test
public void testProvider()
{
    try
    {
        List<TAddress> results = provider.searchItem("SOME_ITEM", "12345");
        Assert.assertNotNull(results);
    }
    catch (Exception e)
    {
        Assert.fail();
    }
}
}

Singleton Log Helper

public class DbLogHelper
{
private static DbLogHelper instance;
@Autowired
private LogDao dao;
private Executor executor = new ThreadPoolExecutor(3, 10, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
private ThreadLocal<Transaction> transactionRef = new ThreadLocal<Transaction>() {

    @Override
    protected Transaction initialValue()
    {
        return new Transaction();
    }

};

public static DbLogHelper getInstance()
{
    if (instance == null)
    {
        synchronized (DbLogHelper.class)
        {
            if (instance == null)
            {
                instance = new DbLogHelper();
            }
        }
    }

    return instance;
}

/**
 * Gets the Transaction object attached to this thread. Generates a new one if it does not
 * yet exist.
 * @return Transaction
 */
public Transaction getLog()
{
    return transactionRef.get();
}

/**
 * Removes the current Transaction object from the thread map. The next getLog() call will
 * generate a new one.
 */
public void resetLog()
{
    transactionRef.remove();
}

/**
 * Gets the DAO used to persist Transaction objects
 */
public LogDao getDao()
{
    return dao;
}

/**
 * Queues the passed Transaction object to be written to the database. This happens in a
 * separate thread so the service is not waiting on database persistence before responding.
 * @param transaction
 */
public void writeLog(Transaction transaction)
{
    LogJob job = new LogJob(transaction, dao);
    executor.execute(job);
}
}

Log DAO

@Repository
public class LogDao
{
private static Logger log = LoggerFactory.getLogger(LogDao.class);
private EntityManager entityManager;

@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
    log.debug("Entity Manager set on LogDao");
    this.entityManager = entityManager;
}

@Transactional
public void updateTransaction(Transaction log)
{
    log = entityManager.merge(log);
}
}

ProviderTest-context.xml

<!-- database -->
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <!-- uncomment to see sql
         <property name="driverClassName" value="com.p6spy.engine.spy.P6SpyDriver" />
    -->
    <property name="url" value="jdbc:mysql://localhost" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

<!-- LOCAL entity manager factory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <!-- <property name="persistenceUnitManager" ref="persistenceUnitManager"/> -->
    <property name="persistenceUnitName" value="domainPersistenceUnit" />
    <property name="dataSource" ref="datasource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="false" />
            <!-- <property name="generateDdl" value="true" /> -->
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
        </bean>
    </property>
</bean>

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="datasource" />
</bean>

<context:annotation-config/>
<context:component-scan base-package="com.company.application.webservice"/>

The autowired DAO in the DbLogHelper always ends up null. However, if I autowire it directly in the unit test, it gets injected just fine the entity manager ready to go. My fear, of course, is that if it's not working in the test, it will not 开发者_JAVA百科work when deployed proper. Even if I need not be worried about that, I'd still like to test it.


DBLogger is not a spring bean so spring cannot Autowire any class to it (because it doesnt know about it). I would recommend defining it as a bean in your spring context and injecting DbLogHelper into any class you want to use it.


You should either define DBLogger as a spring bean as mentioned by @John Vint, or If you can't due to some reason, you can get the object you want to autowire from spring application context (not recommended).

For details on the latter: http://www.javacodegeeks.com/2015/03/using-spring-managed-bean-in-non-managed-object.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜