微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

根据常量值执行 JUnit

如何解决根据常量值执行 JUnit

我有这个带有定义常量的类

public class Constants {
    public static final boolean TEST = true;
}

我想像这样检查这个常量是否为真:

@Test
@EnabledIf("'true' == Constants.TEST")
public void theMeasurementSampleScreenFromPickConfirm() throws InterruptedException {
   // some code execution
}

但我收到错误 java.lang.NoSuchMethodError: 'java.lang.reflect.Method org.junit.platform.commons.util.ReflectionUtils.getrequiredMethod(java.lang.class,java.lang.String,java.lang.class[])'

你知道我如何正确实施这项检查吗。

解决方法

当使用 Spring 的 @EnabledIf 时,您可以使用 SpEL 表达式。见@EnabledIf With a SpEL Expression

要引用常量,请使用 T operator from SpEL

您可以使用特殊的 T 运算符来指定 java.lang.Class(类型)的实例。静态方法也使用这个操作符调用

另请注意,您的 TEST 常量是布尔值,而不是字符串。

结合以上你可以使用:

@EnabledIf("#{T(com.sandbox.Constants).TEST == true}")

甚至

@EnabledIf("#{T(com.sandbox.Constants).TEST}")
,

在比较布尔值时不要使用单引号。

解决方案 1: 在注释本身中附加值。

RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/index\.php?page=8\sHTTP
RewriteRule ^index\.php$ / [R=301,L]

解决方案 2: 使用 Spring 表达式语言,您可以通过提供类的完全限定名称来使用 Type 运算符。例如:如果 @Test @EnabledIf("#{" + Constants.TEST + "}") public void theMeasurementSampleScreenFromPickConfirm() throws InterruptedException { // some code execution } 类在 Constants 包中,则

com.example

注意:这仅适用于具有 Spring Boot Starter Test(内部默认使用 JUnit)支持的 Spring 项目,因为 SpEL 不能评估它是否是非春季项目。 JUnit 不支持在没有独立 Spring 的情况下对 SpEL 的评估。

因此,使用 @Test @EnabledIf("#{T(com.example.Constants).TEST}") public void theMeasurementSampleScreenFromPickConfirm() throws InterruptedException { // some code execution } 创建一个 Spring Boot 项目,并使用来自 spring-boot-starter-test@EnabledIf 注释,它能够评估 Spring 表达式语言。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。