Java 如何在我的 spring 应用程序中测试 afterPropertiesSet 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18771360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:54:33  来源:igfitidea点击:

How to test afterPropertiesSet method in my spring application?

javaspringjunit

提问by AKIWEB

I am working on writing some junit test for my spring application. Below is my application which implements InitializingBean interface,

我正在为我的 spring 应用程序编写一些 junit 测试。下面是我实现 InitializingBean 接口的应用程序,

public class InitializeFramework implements InitializingBean {


    @Override
    public void afterPropertiesSet() throws Exception {

        try {

        } catch (Exception e) {

        }
    }
}

Now I want to call afterPropertiesSetmethod from my junit test but somehow, I am not able to understand what is the right way to do this? I thought, I can use reflection to call this method but I don't think, it's a right way to do that?

现在我想afterPropertiesSet从我的 junit 测试中调用方法,但不知何故,我无法理解这样做的正确方法是什么?我想,我可以使用反射来调用这个方法,但我不认为,这是一个正确的方法吗?

Can anyone provide me a simple example for this on how to write a simple junit test that will test afterPropertiesSetmethod in InitializeFrameworkclass?

任何人都可以为我提供一个简单的例子,说明如何编写一个简单的 junit 测试来测试类中的afterPropertiesSet方法InitializeFramework

采纳答案by Sotirios Delimanolis

InitializingBean#afterProperties()without any ApplicationContextis just another method to implement and call manually.

InitializingBean#afterProperties()没有任何ApplicationContext只是另一种手动实现和调用的方法。

@Test
public void afterPropertiesSet() {
    InitializeFramework framework = new InitializeFramework();
    framework.afterPropertiesSet();
    // the internals depend on the implementation
}

Spring's BeanFactoryimplementations will detect instances in the context that are of type InitializingBeanand, after all the properties of the object have been set, call the afterPropertiesSet()method.

Spring 的BeanFactory实现将检测上下文中类型的实例,InitializingBean并在设置对象的所有属性后调用该afterPropertiesSet()方法。

You can test that too by having your InitializeFrameworkbean be constructed by an ApplicationContextimplementation.

您也可以通过让您的InitializeFrameworkbean 由ApplicationContext实现构造来测试它。

Say you had

说你有

@Configuration
public class MyConfiguration {
    @Bean
    public InitializeFramework initializeFramework() {
        return new InitializeFramework();
    }
}

And somewhere in a test (not really junit worthy though, more of an integration test)

在测试中的某个地方(虽然不是真正的 junit,但更多的是集成测试)

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class);

When the context loads you will notice that the afterPropertiesSet()method of the InitializeFrameworkbean is called.

当上下文加载时,您会注意到调用afterPropertiesSet()InitializeFrameworkbean的方法。