相关疑难解决方法(0)

Spring不能在使用JUnit的单元测试中自动装配

我用JUnit测试以下DAO:

@Repository
public class MyDao {

    @Autowired
    private SessionFactory sessionFactory;

    // Other stuff here

}
Run Code Online (Sandbox Code Playgroud)

如您所见,sessionFactory使用Spring自动装配.当我运行测试时,sessionFactory保持为null并且我得到一个空指针异常.

这是Spring中的sessionFactory配置:

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

怎么了?如何为单元测试启用自动装配?

更新:我不知道它是否是运行JUnit测试的唯一方法,但请注意我在Eclipse中运行它,右键单击测试文件并选择"run as" - >"JUnit test"

java junit spring

41
推荐指数
5
解决办法
9万
查看次数

spring junit为测试加载应用程序上下文

我的WEB-INF目录下有一些XML文件:

  • lyricsBaseApp-servlet.xml中
  • hibernate.xml
  • dataSource.xml
  • beans.xml中

servlet xml导入其他xml文件:

<import resource="dataSource.xml"/>
<import resource="hibernate.xml"/>
<import resource="beans.xml"/>
Run Code Online (Sandbox Code Playgroud)

我希望我的junit4 JukeboxTest类包含整个弹簧配置.使用默认文件名我创建了一个JukeboxTest-content.xml文件.最后,我不知道该放什么......

我试过了:

<import resource="/WEB-INF/dataSource.xml"/>
<import resource="/WEB-INF/hibernate.xml"/>
<import resource="/WEB-INF/beans.xml"/>
Run Code Online (Sandbox Code Playgroud)

要么

<import resource="classpath:./WEB-INF/dataSource.xml"/>
<import resource="classpath:./WEB-INF/hibernate.xml"/>
<import resource="classpath:./WEB-INF/beans.xml"/>
Run Code Online (Sandbox Code Playgroud)

和其他一些想法,但都失败了.有人能指出我如何访问这些文件以及spring解释这些文件路径的方式是什么?

junit spring applicationcontext

27
推荐指数
2
解决办法
8万
查看次数

运行集成测试时无法加载 ApplicationContext

我正在尝试运行 mvnintegration-test阶段,但在执行集成测试时出现Failed to load ApplicationContext错误(单元测试正确执行)。我正在SpringJUnit4ClassRunner使用类来运行我的测试。

这是完整的堆栈跟踪:

2017-02-09 03:22:15.705 [main] ERROR o.s.t.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@5c072e3f] to prepare test instance [com.dentilax.app.accountservice.AccountServiceIT@768b970c]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    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) …
Run Code Online (Sandbox Code Playgroud)

junit spring maven applicationcontext

5
推荐指数
1
解决办法
4万
查看次数

标签 统计

junit ×3

spring ×3

applicationcontext ×2

java ×1

maven ×1