Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

skbkontur/KFixture

Repository files navigation

Build Status

What is about?

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.

Use in your project

maven
<dependency>
    <groupId>ru.kontur.kinfra.kfixture</groupId>
    <artifactId>kfixture</artifactId>
    <version>0.6.0</version>
</dependency>
gradle
implementation "ru.kontur.kinfra.kfixture:kfixture:0.5.0"

Fixture generates random data

data class DataGenerate(
    val param: String
)

fun `should just generate random data`(@Fixture fixture: DataGenerate) {}

JavaxFixture generates valid random data

data class JavaxData(
    @field:Pattern("\\d{2}")
    val param: String
)

fun `should just generate valid data`(@JavaxFixture fixture: JavaxData) {}

If kfixture can't generate your class, you can use ParamConstructor

class DurationConstructor: ParamConstructor<Duration> {
    override fun call(context: FixtureContext): Duration {
        return Duration.ofSeconds(10)
    }
}

For custom annotation you have to provide your generation router

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)
    }
}

Create business fixtures just use Customized and Cusomizer

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) {}

About

Generate test data

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages