spring-boot - 如何为 lambda 表达式编写 junits

标签 spring-boot junit5

我有一段代码需要编写 junits。我不明白如何写语句“return(a,b) ->”。请帮我解决这个问题,因为这个 return(a,b)-> 中的语句在我运行它的 Junit 时没有执行

public GlobalFilter xyz() {
  String xyz = ""
  return (a, b) -> { 
    List<String> abc = a.getRequest().getHeaders().get(xyz);
    // Checking for empty value. If empty then returning 401 status code
    if (abc.equals("")) {
      // setting and returning a status code 401
    }
  }
}

最佳答案

在您的测试中,调用xyz 方法,并获取您的GlobalFilter 实例(lambda 表达式)。 然后在那个实例上你可以在这个实例上运行你的测试,就像任何其他单元测试一样。例如:

GlobalFilter testedFilter = testedClass.xyz();
.. init mocks 
Mono<Void> mono = testedFilter.filter(mockExchange, mockChain);
.. verification and assertions

关于spring-boot - 如何为 lambda 表达式编写 junits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56074264/

相关文章:

java - bean 类的无效属性 'fields[0]'

eclipse - JUnit5 测试在 maven 上工作正常,但在通过 Eclipse 运行时不行, "No tests found with test runner ' JUnit 5'."

java - 'clientRegistrationRepository' 中的 Bean 方法 'OAuth2ClientRegistrationRepositoryConfiguration' 未加载

java - 如何在Spring Boot中使用MySql数据库查询?

java - org.mockito.exceptions.base.MockitoException : Please ensure that the type 'UserRestService' has a no-arg constructor

java - 可选类型的单元测试用例

java - 使用Cucumber JUnit5引擎选择单个测试失败

android - 如何更改 TestEngine ID 以运行 JUnit 5?

java - (Spring boot + Java) 大于 1 GB 文件的下载 API

java - 如何在 Spring Boot 单元测试中使用依赖注入(inject)?