【问题标题】:How retrieve the Spring ApplicationContext through TestRule如何通过 TestRule 检索 Spring ApplicationContext
【发布时间】:2018-03-09 21:34:22
【问题描述】:

我想知道是否可以通过TestRule 检索ApplicationContext

这里TestRule 用于避免测试类之间的层次结构,其目的是重用基础设施配置在这种情况下所需的结构是@ 987654324@ 已经由 Spring 创建。

为了简化共享代码,假设我们需要检索Environment 对象。

我尝试了这两种方法:

一个

@WebAppConfiguration
@ContextConfiguration(classes={RootApplicationContext.class, ServletApplicationContext.class})
public class ManolitoRule implements TestRule {

    @Autowired
    private Environment environment;

    private static final Logger logger = LoggerFactory.getLogger(ManolitoRule.class.getSimpleName());

    @Override
    public Statement apply(Statement base, Description description) {

        if(environment == null) {
            logger.error("NULL");
        }
        else {
            logger.info("Profiles: {}", Arrays.toString(environment.getActiveProfiles()));
        }

        return new Statement() {

                //The @Before configuration should go here now
                ...


            }


        };


    }

}

public class ManolitoTest {

    private static final Logger logger = LoggerFactory.getLogger(ManolitoTest.class.getSimpleName());

    @Rule
    public ManolitoRule manolitoRule = new ManolitoRule();

    @Test
    public void manolitoTest() {

        ...

    }

}

environmentnull

两个

public class ManolitoRule implements TestRule {

    private final Environment environment;

    public ManolitoRule(Environment environment) {
        this.environment = environment;
    }

    private static final Logger logger = LoggerFactory.getLogger(ManolitoRule.class.getSimpleName());

    @Override
    public Statement apply(Statement base, Description description) {

        if(environment == null) {
            logger.error("NULL");
        }
        else {
            logger.info("Profiles: {}", Arrays.toString(environment.getActiveProfiles()));
        }

        return new Statement() {

            //The @Before configuration should go here now    
            ....


        };


    }

}

还有

@WebAppConfiguration
@ContextConfiguration(classes={RootApplicationContext.class, ServletApplicationContext.class})
@TestExecutionListeners(listeners={LoggingTestExecutionListener.class}, mergeMode=MergeMode.MERGE_WITH_DEFAULTS)
public class ManolitoTest {

    private static final Logger logger = LoggerFactory.getLogger(ManolitoTest.class.getSimpleName());

    @Autowired
    private Environment environment;

    @Rule
    public ManolitoRule manolitoRule = new ManolitoRule(environment);

    @Test
    public void manolitoTest() {

        ...

    }

}

environment 又是null

是否存在完成此配置的正确配置?

我的印象是JUnitSpring Framework 之间的生命周期是造成这种情况的原因。

注意:如果使用抽象类,一切正常。但考虑避免层次结构和 @Rules 的场景,例如ManolitoRuleSpringClassRuleSpringMethodRule 一起工作

【问题讨论】:

    标签: spring junit spring-test


    【解决方案1】:

    方法 Two 接近可行的解决方案,只需要进行以下更改。

    1. @RunWith(SpringRunner.class)注释测试类。
    2. ManolitoRule 的一个实例配置为测试的ApplicationContext 中的一个bean。
    3. 使用@Autowired 注释ManolitoRule 构造函数。
    4. @Rule 注释测试类中的manolitoRule 字段 @Autowired

    注意:此技术与 Spring 规则(SpringClassRuleSpringMethodRule)一起工作。此技术只能与SpringRunner 结合使用。

    【讨论】:

    • 如果您可以分享一些 2,3,4 的 sn-p 代码,对于更好地理解将是有价值的。
    • 对于第 2 步,由您决定如何定义 bean,但该过程与任何其他 bean 没有什么不同。通过@Bean 类中的@Bean 方法对其进行配置;通过组件扫描将其拾取;或在@ContextConfiguration(classes=...) 中声明类引用。
    • 您可以将此技术与 Spring 规则一起使用!您只能将此技术与SpringRunner 一起使用。我在答案中添加了“注释”。
    • 是的,使用 JUnit Jupiter(即 JUnit 5)应该更容易。我也会在链接的 SO 问题上提到这一点。
    【解决方案2】:

    你试过了吗

    @Autowired 
    private Environment env;
    

    编辑: 也试试添加这个

    @RunWith(SpringJUnit4ClassRunner.class)
    

    【讨论】:

    • 即使添加了@RunWith(SpringRunner.class),对于这两种方法,它仍然会返回null
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-19
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    相关资源
    最近更新 更多