如何在使用@RunWith和@ContextConfiguration注释的jUnit测试中访问Spring上下文?

发布于 2024-08-24 17:29:16 字数 358 浏览 5 评论 0原文

我有以下测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/services-test-config.xml"})
public class MySericeTest {

  @Autowired
  MyService service;
...

}

是否可以通过其中一种方法以编程方式访问 services-test-config.xml ?喜欢:

ApplicationContext ctx = somehowGetContext();

I have following test class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/services-test-config.xml"})
public class MySericeTest {

  @Autowired
  MyService service;
...

}

Is it possible to access services-test-config.xml programmatically in one of such methods? Like:

ApplicationContext ctx = somehowGetContext();

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

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

发布评论

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

评论(5

岁吢 2024-08-31 17:29:16

这也很好用:

@Autowired
ApplicationContext context;

This works fine too:

@Autowired
ApplicationContext context;
趴在窗边数星星i 2024-08-31 17:29:16

由于测试也将像 Spring bean 一样实例化,因此您只需要实现 ApplicationContextAware 接口:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/services-test-config.xml"})
public class MySericeTest implements ApplicationContextAware
{

  @Autowired
  MyService service;
...
    @Override
    public void setApplicationContext(ApplicationContext context)
            throws BeansException
    {
        // Do something with the context here
    }
}

对于非 xml 需求,您也可以这样做:

@RunWith(SpringJUnit4ClassRunner.class)
/* must provide some "root" for the app-context, use unit-test file name to the context is empty */
@ContextConfiguration(classes = MyUnitTestClass.class)
public class MyUnitTestClass implements ApplicationContextAware {

Since the tests will be instantiated like a Spring bean too, you just need to implement the ApplicationContextAware interface:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/services-test-config.xml"})
public class MySericeTest implements ApplicationContextAware
{

  @Autowired
  MyService service;
...
    @Override
    public void setApplicationContext(ApplicationContext context)
            throws BeansException
    {
        // Do something with the context here
    }
}

For non xml needs, you can also do this:

@RunWith(SpringJUnit4ClassRunner.class)
/* must provide some "root" for the app-context, use unit-test file name to the context is empty */
@ContextConfiguration(classes = MyUnitTestClass.class)
public class MyUnitTestClass implements ApplicationContextAware {
萌能量女王 2024-08-31 17:29:16

如果您的测试类扩展了 Spring JUnit 类
(例如,AbstractTransactionalJUnit4SpringContextTests 或任何其他扩展 AbstractSpringContextTests 的类),您可以通过调用 getContext() 方法来访问应用上下文。
查看 org.springframework 包的 javadocs。测试。

If your test class extends the Spring JUnit classes
(e.g., AbstractTransactionalJUnit4SpringContextTests or any other class that extends AbstractSpringContextTests), you can access the app context by calling the getContext() method.
Check out the javadocs for the package org.springframework.test.

时间你老了 2024-08-31 17:29:16

可以使用 SpringClassRule 注入 ApplicationContext 类的实例
和 SpringMethodRule 规则。如果您想使用它可能会非常方便
另一个非 Spring 跑步者。这是一个例子:

@ContextConfiguration(classes = BeanConfiguration.class)
public static class SpringRuleUsage {

    @ClassRule
    public static final SpringClassRule springClassRule = new SpringClassRule();

    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

    @Autowired
    private ApplicationContext context;

    @Test
    public void shouldInjectContext() {
    }
}

It's possible to inject instance of ApplicationContext class by using SpringClassRule
and SpringMethodRule rules. It might be very handy if you would like to use
another non-Spring runners. Here's an example:

@ContextConfiguration(classes = BeanConfiguration.class)
public static class SpringRuleUsage {

    @ClassRule
    public static final SpringClassRule springClassRule = new SpringClassRule();

    @Rule
    public final SpringMethodRule springMethodRule = new SpringMethodRule();

    @Autowired
    private ApplicationContext context;

    @Test
    public void shouldInjectContext() {
    }
}
(り薆情海 2024-08-31 17:29:16

创建了一篇与此问题相关的帖子,这确实有助于获得信心

尝试此链接了解更多详细信息

@Test
void TestBeanLaunch()
{
    context.run(it -> {
        /*
         * I can use assertThat to assert on the context
         * and check if the @Bean configured is present
         * (and unique)
         */
        assertThat(it).hasSingleBean(PracticeApplication.class);
        assertThat(it).hasSingleBean(PracticeApplication.TestBean.class);
        assertThat(it.getBean("BeanTest")).isInstanceOf(PracticeApplication.TestBean.class);
    });
} 

Created one post related to this issue, this really helpful to gain confidence

Try this link for more details

@Test
void TestBeanLaunch()
{
    context.run(it -> {
        /*
         * I can use assertThat to assert on the context
         * and check if the @Bean configured is present
         * (and unique)
         */
        assertThat(it).hasSingleBean(PracticeApplication.class);
        assertThat(it).hasSingleBean(PracticeApplication.TestBean.class);
        assertThat(it.getBean("BeanTest")).isInstanceOf(PracticeApplication.TestBean.class);
    });
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文