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
- Host: GitHub
- URL: https://github.com/mikybars/junit5-randomizer-extension
- Owner: mikybars
- Created: 2021-10-26T20:22:24.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-10-26T21:19:38.000Z (over 4 years ago)
- Last Synced: 2025-04-02T04:58:06.332Z (over 1 year ago)
- Topics: easy-random, junit5, object-mother
- Language: Java
- Homepage: https://mperezi.github.io/junit5-randomizer-extension/allclasses-index.html
- Size: 133 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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));
}
}
```