-
-
Save anderssv/df73a54055cd1e07b811bf4ce87c6db9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DatabaseTestExtension : ParameterResolver { | |
private val STORE_NAME = "main-database" | |
override fun supportsParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext?): Boolean { | |
return parameterContext.parameter.type == Database::class.java | |
} | |
override fun resolveParameter(parameterContext: ParameterContext, extensionContext: ExtensionContext): Any { | |
// We do the store thing here to avoid loading and migrating the DB for each test/class | |
// Will however load per thread, so are not guaranteed to be done only once | |
val store = extensionContext.root.getStore(Namespace.create(DatabaseTestExtension::class.java.simpleName)) | |
val db: Database = (store.get(STORE_NAME) as Database?) ?: Database(Config.load()).also { | |
// New object so do initialization and store | |
it.initializeAndMigrate() | |
store.put(STORE_NAME, it) | |
} | |
return db | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment