Spring 具有@PostConstruct anotation的@Component类中的测试方法

Spring 具有@PostConstruct anotation的@Component类中的测试方法,spring,testing,annotations,Spring,Testing,Annotations,我正在测试spring应用程序中控制器中的服务方法。在我看来,我的问题是一个基本的服务方法调用是在类中完成的,该类是anotate@Component,方法调用在@PostConstruct anotate方法中 @Component public final class Helper @PostConstruct public void initialize() { stuff = service.getNessesaryStuff(); } 这些东西的内容来自一个数据库,在每次测试中

我正在测试spring应用程序中控制器中的服务方法。在我看来,我的问题是一个基本的服务方法调用是在类中完成的,该类是anotate@Component,方法调用在@PostConstruct anotate方法中

@Component
public final class Helper

@PostConstruct
public void initialize() {
  stuff = service.getNessesaryStuff();
}
这些东西的内容来自一个数据库,在每次测试中都会有所不同。这就是为什么数据库在每次测试之前都会填充所需的数据。如果没有这些数据,其他服务方法将无法工作。我已经检查了正确的数据是否完全按照预期的方式进入数据库,但它不会立即产生影响。在得到正确的数据并通过测试之前,我需要运行相同的测试几次

有办法吗


在运行应用程序时,助手类会自己创建。在运行测试时,我需要创建一个bean来避免NullPointerException

问题是什么还不清楚。您提到了@PreConstruct,但代码片段显示了@PostConstruct。也许您需要从实际测试代码中发布更多内容,并更好地解释您需要什么以及什么不起作用。PostConstruct是正确的。我的想法乱七八糟。你到底想要什么还是不清楚如何访问帮助程序?