Library give a parameter resolver, which can be used to generate valid data for your class to pass validation of your api.
- Generate data for all javax validation annotations.
- Also generate data ignore javax annotations
- Provide an interface to define custom rules for validation.
- Valid param go through chain of validation.
- Suite of test utils for generation parameters and so on.
<dependency>
<groupId>ru.kontur.kinfra.kfixture</groupId>
<artifactId>kfixture</artifactId>
<version>0.6.0</version>
</dependency>
implementation "ru.kontur.kinfra.kfixture:kfixture:0.5.0"
data class DataGenerate(
val param: String
)
fun `should just generate random data`(@Fixture fixture: DataGenerate) {}
data class JavaxData(
@field:Pattern("\\d{2}")
val param: String
)
fun `should just generate valid data`(@JavaxFixture fixture: JavaxData) {}
class DurationConstructor: ParamConstructor<Duration> {
override fun call(context: FixtureContext): Duration {
return Duration.ofSeconds(10)
}
}
class EmailRouter<T> : ValidRouter<T, Email> where T : Any {
private val emailGenerator = EmailGenerator()
override fun process(param: T, annotation: Email, clazz: KClass<*>, type: KType): Any? {
return when (param) {
is String -> emailGenerator.process(param, annotation, clazz, type)
else -> throw AnnotationCantBeAppliedException(annotation, clazz)
}
}
}
If you need just construct object in validation and do not process annotation generation, you can use ValidationConstructor
class DurationConstructor: ValidationConstructor<Duration> {
override fun call(context: FixtureContext): Duration {
return Duration.ofSeconds(10)
}
}
data class TestDto(
val param: String,
val param1: String
)
class TestCustomizer: Customizer<TestDto>{
override fun customize(value: TestDto, context: FixtureContext): TestDto {
return value.copy(param="AAAA")
}
}
@Fixture
@Customized(sequence=[TestCustomizer::class])
annotation class BusinessFixture
@Test
fun `should generate fixture`(@BusinessFixture fixture: TestDto) {}