Jebus_Chris
01/14/2023, 4:19 AMAdam S
01/14/2023, 9:17 AMJebus_Chris
01/14/2023, 4:30 PMinternal class CSVDataProviderTest {
private val csvDataProvider = CSVDataProvider(TestConfig.config.csv.path)
@Test
fun getAllData() {
val data = csvDataProvider.getData("ABC")
assertEquals(10, data.size)
}
Config
data class Database(val host: String, val port: Int, val user: String, val pass: String)
data class CSV(val path: String)
data class Config(val env: String, val database: Database, val csv: CSV)
class TestConfig {
companion object {
val config = ConfigLoaderBuilder.default()
.addResourceSource("/test-config.yml")
.build()
.loadConfigOrThrow<Config>()
}
} // Uses hoplite, just parses a file into some data classes
Jebus_Chris
01/14/2023, 4:34 PMAdam S
01/14/2023, 4:38 PMCSVDataProvider
you could add a property with a default value in the constructor?
class CSVDataProvider(
val config: Config = loadConfig() // load using Hoplite
)
then in tests you can manually create a Config
instance, and pass it into the constructor
@Test
fun blah() {
val testConfig = Config(...)
val csvDataProvider = CsvDataProvider(config = testConfig)
}
Adam S
01/14/2023, 4:41 PMMY_PROJECT_CONFIG_FILE_LOCATION=/Users/me/my-project/config.yml
then tell Hoplite to try and load that config, if the property is defined. In tests you could override the environment variable, and set it to a new location. This would be a little more fragile - it might be possible for two tests to accidentally overwrite the environment variableJebus_Chris
01/14/2023, 4:52 PMJebus_Chris
01/14/2023, 4:52 PM