使用 spring 测试类时模拟配置值

发布于 2024-11-28 19:41:49 字数 1183 浏览 5 评论 0原文

我正在尝试测试一个像这样的类,

@Controller
public class FailureController {

    @Value("#{configValues.defaultRedirectUrl}")
    private String defaultRedirectUrl;
public FailureController (String defaultRedirectUrl) {
this.defaultRedirectUrl = defaultRedirectUrl;
}
...

问题是如果不为测试类创建一个特殊的构造函数(如上所示),我就无法测试这个类,这最终会初始化 中的 defaultRedirectUrl失败控制器。

在测试期间如何在不创建构造函数(来自 spring 上下文)的情况下测试它。我的主要目标是在运行测试时在没有构造函数的情况下初始化 FailureController 中的值。

是否有可能以某种方式在测试期间加载 spring 上下文并初始化 FailureController 中的字段 这就是我一直在做的事情,但它不起作用,defaultRedirectUrlFailureController中仍然是null

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext-test.xml" })
public class FailureControllerTest {
    @Autowired
    FailureController failureController;

applicationContext-test.xml

...
        <bean id="failureController" class="se.synergica.watchtower.controllers.FailureController">
        </bean>

        <import resource="spring-config-test.xml" />
    </beans>

谢谢。 阿尔

I am trying to test a class like

@Controller
public class FailureController {

    @Value("#{configValues.defaultRedirectUrl}")
    private String defaultRedirectUrl;
public FailureController (String defaultRedirectUrl) {
this.defaultRedirectUrl = defaultRedirectUrl;
}
...

The problem is that I can't test this class without creating a special constructor for test class (like above), which is eventually to initializes the defaultRedirectUrl in FailureController.

How can I test it without creating a constructor (that are to be come from spring context) during the test. My main objective is to initialize the values in FailureController without a constructor when running a test.

Is it possible in some way that spring context gets loaded during the test and initializes the fields in FailureController
this is what i have been doing but its not working, defaultRedirectUrl remains null in FailureController.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/applicationContext-test.xml" })
public class FailureControllerTest {
    @Autowired
    FailureController failureController;

applicationContext-test.xml

...
        <bean id="failureController" class="se.synergica.watchtower.controllers.FailureController">
        </bean>

        <import resource="spring-config-test.xml" />
    </beans>

thank you.
al

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

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

发布评论

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

评论(2

美羊羊 2024-12-05 19:41:49

我将在您的测试类路径(标准项目中的 src/test/resources)中创建一个单独的 applicationContext.xml 文件。在那里我将设置一个带有虚拟值的 configValues 对象。
您的被测类也应该在该上下文中实例化。

然后在 junit 测试中加载这样的测试上下文:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext-test.xml")
public class MyTest {

    @Autowired
    private AClassToBeTest subject;

    //...perform test on subject
}

有关 spring 测试的更多信息: http://static.springsource.org/spring/docs/3.0.x/reference/testing.html

I would create a separate applicationContext.xml file in your test-classpath (src/test/resources in standard projects). There I would set up a configValues object with dummy values.
Your class under test should also be instanciated in that context.

Then load the test context like this in a junit test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext-test.xml")
public class MyTest {

    @Autowired
    private AClassToBeTest subject;

    //...perform test on subject
}

more on spring testing: http://static.springsource.org/spring/docs/3.0.x/reference/testing.html

马蹄踏│碎落叶 2024-12-05 19:41:49

这些配置属性通常来自 .properties 文件 - 只需在单元测试类路径中使用不同的属性文件(带有测试值)

These config properties usually come from .properties files - simply use a different properties file (with test values) in the unit test classpath

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