Spring jUnit 测试 - 无法自动装配或找不到 appContext.xml

发布于 2024-12-12 10:59:36 字数 3108 浏览 0 评论 0原文

我正在对基于 spring 的应用程序 atm 进行单元测试。 首先的问题是,如果我没有在服务器上启动应用程序一次,单元测试都会失败。 如果我首先在服务器上启动应用程序(然后停止它),我的单元测试就会正常工作。

如果不启动服务器,我会收到以下错误:

... java.io.FileNotFoundException: class path resource [META-INF/spring/applicationContext-test.xml] cannot be opened because it does not exist

我的单元测试定义如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/applicationContext-test.xml" })
@TransactionConfiguration
@Transactional
public class InventoryControllerTest extends AbstractTransactionalJUnit4SpringContextTests {

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    private AnnotationMethodHandlerAdapter handlerAdapter;

    @Before
    public void setUp() throws Exception {
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        handlerAdapter = applicationContext
            .getBean(AnnotationMethodHandlerAdapter.class);
}
    //... tests
}

所以就像我说的,如果我之前启动过应用程序一次,一切都会正常。

所以我将配置位置更改为 位置 = {“classpath/META-INF/spring/applicationContext-test.xml”}) 但不费吹灰之力,就会出现与上面提到的相同的异常。

进一步前进的唯一方法是这个位置: 位置= {“类路径*:applicationContext-test.xml”}) 然后我得到这个异常: 找不到依赖项的 [javax.sql.DataSource] 类型的匹配 bean:预计至少有 1 个有资格作为此依赖项的自动装配候选者的 bean。依赖注释: {}

但这很令人困惑,因为我的测试上下文文件中肯定有一个数据源:

<bean class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" id="dataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:testdb;sql.syntax_ora=true" />
    <property name="username" value="some" />
    <property name="password" value="some" />
</bean>

EIDT 2

认识到问题是 RunWith(...) 并同时扩展 spring 类并从中删除所有通配符位置路径。我得到这个例外:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found
... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found
... 40 more
Caused by: java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found)
... 47 more

我真的很感激任何帮助!

提前致谢

I am unittesting a spring based application atm.
First the problem is, that if I haven't started the app once on a server the unittests all fail.
If I do start the app on the server first (and stop it), my unit tests are working.

Without starting the server I get the following error:

... java.io.FileNotFoundException: class path resource [META-INF/spring/applicationContext-test.xml] cannot be opened because it does not exist

My Unit test is defined as following:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/META-INF/spring/applicationContext-test.xml" })
@TransactionConfiguration
@Transactional
public class InventoryControllerTest extends AbstractTransactionalJUnit4SpringContextTests {

    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    private AnnotationMethodHandlerAdapter handlerAdapter;

    @Before
    public void setUp() throws Exception {
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        handlerAdapter = applicationContext
            .getBean(AnnotationMethodHandlerAdapter.class);
}
    //... tests
}

So like I said, if I've started the app once before, everything works fine.

So I changed the configuration location to
locations = { "classpath/META-INF/spring/applicationContext-test.xml" })
But without effort, same exception as named above.

The only way to get further is this location:
locations = { "classpath*:applicationContext-test.xml" })
Then I get this exception:
No matching bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

But that's confusing, because I definetly have a datasource in my test context file:

<bean class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" id="dataSource">
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
    <property name="url" value="jdbc:hsqldb:mem:testdb;sql.syntax_ora=true" />
    <property name="username" value="some" />
    <property name="password" value="some" />
</bean>

EIDT 2

After recognizing, that the problem is RunWith(...) and extending the spring class at the same time and removing all wildcards from the location path. I get this exception:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Cannot resolve reference to bean 'entityManagerFactory' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found
... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [META-INF/spring/applicationContext-test.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found
... 40 more
Caused by: java.lang.IllegalArgumentException: No persistence unit with name 'persistenceUnitTest' found)
... 47 more

I would really appreciate any help!

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒中人 2024-12-19 10:59:36

classpath和Path之间必须有:,并且路径不能以/开头。所以正确的语法是:

@ContextConfiguration(locations = { "classpath:META-INF/spring/applicationContext-test.xml" })

或者更短的形式

@ContextConfiguration("classpath:META-INF/spring/applicationContext-test.xml")

您自己发现的另一个问题是您应该使用 @ContextConfiguration 或 AbstractTransactionalJUnit4SpringContextTests。 Java 文档的编码注释

这是来自 AbstractTransactonalJUnit4SpringContextTests >
注意:该类只是为了方便扩展。如果你不这样做
希望您的测试类与 Spring 特定的类层次结构相关联,
您可以使用以下方法配置自己的自定义测试类
{@link SpringJUnit4ClassRunner},{@link ContextConfiguration
@ContextConfiguration},{@link TestExecutionListeners
@TestExecutionListeners}、{@link 事务性@Transactional}、


启动问题:
Eclipse 不会将资源从 src\test\resources 复制到目标目录。因此,您需要一种工具或其他东西来为您做到这一点。您找到了一种方法:启动应用程序。第二个是从 eclipse 运行 maven test

There must be a : between classpath and Path, as well as the path must not start with /. So the correct syntax would be:

@ContextConfiguration(locations = { "classpath:META-INF/spring/applicationContext-test.xml" })

or a bit shorter form

@ContextConfiguration("classpath:META-INF/spring/applicationContext-test.xml")

An other problem, found by your self is that you should use @ContextConfiguration OR AbstractTransactionalJUnit4SpringContextTests. Here is the accoding note from Java Doc of AbstractTransactonalJUnit4SpringContextTests

>
Note: this class serves only as a convenience for extension. If you do not
wish for your test classes to be tied to a Spring-specific class hierarchy,
you may configure your own custom test classes by using
{@link SpringJUnit4ClassRunner}, {@link ContextConfiguration
@ContextConfiguration}, {@link TestExecutionListeners
@TestExecutionListeners}, {@link Transactional @Transactional},
etc.


The starting problem:
Eclipse does not copy the resources from src\test\resources to the target directory. So you need one tool or something that do this for you. You have found one way: starting the application. A second one would be running maven test from eclipse.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文