gpt4 book ai didi

java - Spring 3.2 : @WebAppConfiguration breaks existing @Transactional tests

转载 作者:太空宇宙 更新时间:2023-11-04 07:42:12 24 4
gpt4 key购买 nike

问候。
我有一个 Roo 生成的网络应用程序。我想测试我的 Controller 。所以我使用 spring-test-3.2。
使用STS 3.2。
用于测试的嵌入式 Derby。
当 Controller 测试夹在两个集成测试之间时,测试套件会失败 - 破坏其后的集成测试。如果我@忽略 Controller 测试,套件运行正常。这个集成测试既有 Roo 生成的方法,也有我自己的测试方法。
我四处搜寻了很多 - 但无法解释这种行为。我怀疑事务没有正确回滚,但这似乎一切都很好(日志很好)。 我只有一种空 Controller 测试方法。我认为出于某种原因我的 @ContextConfiguration 是问题......

之前的集成测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/META-INF/spring /applicationContext*.xml")
@Transactional
public class CompanyServiceImplTest {

Controller 测试:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(value = {
"file:src/main/resources/META-INF/sprin/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/webmvc-config.xml" })
@Transactional

集成测试(这会破坏)

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/META-INF/spring/applicationContext*.xml")
@Transactional
@RooIntegrationTest(entity = Status.class)
public class StatusIntegrationTest {

如果有任何指点,我们将不胜感激......

--- 加油

最佳答案

这可能是由 https://jira.springsource.org/browse/SPR-6121 引起的

对我有用的解决方法是通过将@WebAppConfiguration添加到所有单元测试中,并确保它们都具有相同的@ContextConfiguration,使所有单元测试使用相同的应用程序上下文。

您看起来正在使用 Spring Roo。为了避免编辑所有现有的单元测试,您可以为测试创建 webmvc-config.xml 的副本,重命名/移动它以匹配 applicationContext*.xml 模式。

可以使用方面来向现有测试添加 @WebAppConfiguration 注释:

package com.foo.bar;

import org.springframework.test.context.web.WebAppConfiguration;

privileged aspect WebAppConfigurationAspect {
declare @type: com.foo.bar..*Test: @WebAppConfiguration;
}

关于java - Spring 3.2 : @WebAppConfiguration breaks existing @Transactional tests,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15879732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com