An open API service indexing awesome lists of open source software.

https://github.com/mikybars/junit5-randomizer-extension

Populate test objects with random content via annotations
https://github.com/mikybars/junit5-randomizer-extension

easy-random junit5 object-mother

Last synced: over 1 year ago
JSON representation

Populate test objects with random content via annotations

Awesome Lists containing this project

README

          

# junit5-randomizer-extension

> The randomizer extension for JUnit5 is just a convenient wrapper around [easy-random](https://github.com/j-easy/easy-random), a sort of dumb [ObjectMother](https://martinfowler.com/bliki/ObjectMother.html) for the JVM.

## Basic usage

### Single entities

```java
@ExtendWith(RandomizerExtension.class)
class CreateOrderUseCaseImplTest {

@Random
private MyObject randomField;

//...

@Test
void shouldNotCreateOrder_WhenOrderTypeIsMissing(@Random Order newOrder) {
// given
newOrder.setType(null);

// when
createOrderUseCase.execute(newOrder);

// then
verify(orderRepository, never()).save(any());
}
}
```

### Collections

```java
@Test
void givenItemListTooLong_thenReturnBadRequest(
@Random(type=ItemDto.class, size=30) List manyItems) {
//...
}
```

## Advanced usage

### Custom mappings

```java
@ExtendWith(RandomizerExtension.class)
class EntityApiControllerUpdateItemsTest implements CustomRandomFieldProvider {

@Override
public void registerCustomRandomizers(CustomRandomizerRegistry registry) {
registry.registerRandomizer(
StringField.named("state"),
StringRandomizerFactory.ofEnum(SubmissionState.class));
}
}
```