【问题标题】:JUnit giving NullPointerException on accessing service class stepexecution [duplicate]JUnit 在访问服务类 stepexecution 时给出 NullPointerException [重复]
【发布时间】:2021-03-24 19:15:56
【问题描述】:

我有一个服务类要测试。

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 呢?

【问题讨论】:

  • 您的代码如图所示包含几个错误(例如,saveStepExecution 中的 ) 并在未定义此类构造函数时调用无参数构造函数 MyService())。请发帖minimal reproducible example

标签: java junit mockito spring-batch junit4


【解决方案1】:

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

@BeforeStep method is not called during unit test execution

【讨论】:

  • 在您共享的链接的帮助下使用 MetaDataInstanceFactory 解决。珍惜你的时间。谢谢
  • @usr_11 在这种情况下,请投票支持此处有帮助的答案:stackoverflow.com/a/52192057/5019386。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-29
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 2018-06-19
相关资源
最近更新 更多