JUnit 在访问服务类 stepexecution 时给出 NullPointerException

问题描述

我有一个要测试的服务类。

public class MyService{
  private final MyConfig myconfig;
  private final WebClient webClient;
  private String result;
  private StepExecution stepExecution;

  @BeforeStep
  public void saveStepExecution(){
    this.stepExecution = stepExecution);
  }

  @AutoWired
  MyService(
    MyConfig myconfig,WebClient webClient) {
    this.myconfig = myconfig;
    this.webClient = webClient;
  }

  public void someMethod(){
    // does some stuff with stepexecution
  }
}

我的 Junit 测试用例是:

@Test
public void sometest(){
  MyService myservice = new MyService();
  myservice.someMethod();
}

但是它在 stepExecution 给出了 Null 指针,这是显而易见的。那么,我该如何通过 JUnit 测试方法的 stepexecution 呢?

解决方法

这可能不是您要找的答案。我没有足够的声誉来发表评论或改进您的问题,但是,我觉得您的问题与此类似。

@BeforeStep method is not called during unit test execution