小编vab*_*048的帖子

在测试中的 Spring Environment 中使用 @EnabledIf 和 spring.profiles.active 属性

根据文档(https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/junit/jupiter/EnabledIf.html#expression--),您可以使用@EnabledIf测试类或测试方法上的注释,如下所示:

@EnabledIf("${smoke.tests.enabled}")

或者

@EnabledIf("#{systemProperties['os.name'].toLowerCase().contains('mac')}")

其中字符串是 Spring Environment 中可用属性的占位符。

假设我有以下application.yml文件:

smoke:
  tests:
    enabled: true
spring:
  profiles:
    active: test
Run Code Online (Sandbox Code Playgroud)

以及以下测试类:

@EnabledIf(value = "#{${spring.profiles.active} == 'test'}", loadContext = true)
@SpringBootTest
public class SomeClassForTests {

    @Autowired SomeType autowiredType;

    @Test
    public void someTest() {
        // test logic...
    }
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,我收到以下错误:

Test ignored.

org.junit.jupiter.engine.execution.ConditionEvaluationException: Failed to evaluate condition [org.springframework.test.context.junit.jupiter.EnabledIfCondition]: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'test' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe …
Run Code Online (Sandbox Code Playgroud)

java spring spring-test junit5

9
推荐指数
1
解决办法
3375
查看次数

为什么 Java 记录的规范构造函数不能具有比记录级别更严格的访问权限?

我有一种情况,我希望特定类型的记录实例只能使用同一包内单独类中的工厂方法创建。这样做的原因是因为在创建记录之前我需要执行大量的验证。

记录旨在成为其验证字段的哑数据载体,但验证不能在记录的构造函数中进行,因为我们需要访问一些精心设计的验证器对象才能实际执行验证。

由于将验证器对象传递给记录构造函数意味着它们将构成记录状态的一部分,这意味着我们不能使用记录构造函数来执行记录的验证。

因此,我将验证提取到它自己的工厂中并编写了如下代码(工厂类和同一包中的记录):

package some.package;

// imports.....

@Component
class SomeRecordFactory {
    private final SomeValidator someValidator;
    private final SomeOtherValidator someOtherValidator;
    // Rest of the fields
    // ....

    // constructor  
    // ....


    public SomeRecord create(...) {
         someValidator.validate(....);
         someOtherValidator.validate(....);
         // .... other validation

         return new SomeRecord(...);
    }
}
Run Code Online (Sandbox Code Playgroud)
package some.package;

public record SomeRecord(...) {
    /* package-private */ SomeRecord {
    }
}
Run Code Online (Sandbox Code Playgroud)

无论出于何种原因,上述内容不适用于 IntelliJ 抱怨:

Compact constructor access level cannot be more restrictive than the record access level (public)
Run Code Online (Sandbox Code Playgroud)

我可以通过使用普通类(允许使用单个包私有构造函数)来避免这个问题,但希望更准确地将数据建模为记录。

为什么记录存在此限制?未来是否有取消此限制的计划?

java java-record java-16

7
推荐指数
3
解决办法
523
查看次数

是否有与 Kotlin 的 require 函数等效的内置 Java?

Kotlin 有一个require函数,可以像这样使用(从参考文档中复制):

fun getIndices(count: Int): List<Int> {
    require(count >= 0) { "Count must be non-negative, was $count" }
    // ...
    return List(count) { it + 1 }
}

// getIndices(-1) // will fail with IllegalArgumentException

println(getIndices(3)) // [1, 2, 3]
Run Code Online (Sandbox Code Playgroud)

如果值为 false,该函数本质上会抛出 IllegalArgumentException。显然,这可以很容易地在 Java 中实现 - 但我想知道 JDK 或 apache 库(或任何其他无处不在的库)中是否已经有一些东西提供了这样的功能?

java kotlin

0
推荐指数
1
解决办法
271
查看次数

标签 统计

java ×3

java-16 ×1

java-record ×1

junit5 ×1

kotlin ×1

spring ×1

spring-test ×1