Spring Boot – How to set or override properties defined in application.yml via a custom extension class that implements BeforeAllCallback?

huangapple go评论93阅读模式
英文:

Spring Boot - How to set or override properties defined in application.yml via a custom extension class that implements BeforeAllCallback?

问题

我有一个扩展类,实现了 BeforeAllCallback 接口,用于设置随机端口,如下所示:

import org.junit.jupiter.api.extension.ExtensionContext
import java.net.ServerSocket

class CustomExtension : org.junit.jupiter.api.extension.BeforeAllCallback {
    var port: Int? = null

    override fun beforeAll(context: ExtensionContext?) {
        port = getRandomPort()
    }
    
    private fun getRandomPort(): Int {
        ServerSocket(0).use { 
            serverSocket -> return serverSocket.localPort 
        }
    }
}

另外,在 src/test/resources/application.yml 文件中定义了一个属性,如下所示:

app:
  random-port: 8765

这个属性被以下配置类所使用:

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConstructorBinding

@ConstructorBinding
@ConfigurationProperties(prefix = "app")
data class PortProperty(
    val randomPort: Int
)

问题

我如何在从 CustomExtension 类运行我的测试时覆盖这个硬编码的 app.random-port 值?我的想法是让我的 PortProperty 获取正确的值,而不是硬编码的值。

我的测试框架是 JUnit5

import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
@ExtendWith(CustomExtension::class)
class RandomPortTest {
}

编辑

实际用例是在测试类中使用 AWS DynamoDBLocal 库启动本地 DynamoDB。

英文:

I have an extension class that implements BeforeAllCallback to set a random port like this.

import org.junit.jupiter.api.extension.ExtensionContext
import java.net.ServerSocket

class CustomExtension : org.junit.jupiter.api.extension.BeforeAllCallback {
    var port: Int? = null

    override fun beforeAll(context: ExtensionContext?) {
        port = getRandomPort()
    }
    
    private fun getRandomPort(): Int {
        ServerSocket(0).use { 
            serverSocket -> return serverSocket.localPort 
        }
    }

}

Also, I have a property defined in src/test/resources/application.yml like this.

app:
  random-port: 8765

This property is used by this configuration class.

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConstructorBinding

@ConstructorBinding
@ConfigurationProperties(prefix = "app")
data class PortProperty(
    val randomPort: Int
)

Question

How can I override that hard corded app.random-port when running my tests from the CustomExtension class? The idea is so that my PortProperty gets the right value and not the hard coded one.

My test framework is JUnit5.

import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.boot.test.context.SpringBootTest

@SpringBootTest
@ExtendWith(CustomExtension::class)
class RandomPortTest {
}

Edit

The use case is actually to start a local dynamo db using the AWS DynamoDBLocal library for the test classes.

答案1

得分: 1

你可以在 beforeAll 方法中设置 app.random-port 系统属性。由于系统属性的优先级高于属性文件,应用程序的 application.yml 文件中的端口值将被覆盖:

override fun beforeAll(context: ExtensionContext?) {
    port = getRandomPort()
    System.setProperty("app.random-port", port?.toString())
}
英文:

You can set the app.random-port system property in the beforeAll method. As system properties have higher priority than properties files, the port value from the application.yml will get overridden:

override fun beforeAll(context: ExtensionContext?) {
    port = getRandomPort()
    System.setProperty("app.random-port", port?.toString())
}

huangapple
  • 本文由 发表于 2020年10月15日 06:07:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64362165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定