public class MessageGenerator
extends org.junit.rules.TestWatcher
class MyTest {
{@literal @}Rule
public MessageGenerator gen = new MessageGenerator()
.dumpOnFailure()
.addFactory(f -> f.getName().endsWith("uuid") ? () -> UUID.randomUUID().toString() : null);
{@literal @}Test
public testSomething() {
gen.addFactory(f -> f.equals(MyMessage._Field.NAME) ? () -> "name" : null);
MyMessage msg = gen.generate(MyMessage.kDescriptor);
sut.doSomething(msg);
assertThat(sut.state() is(SystemToTest.CORRECT));
}
}
| Modifier and Type | Class and Description |
|---|---|
static interface |
MessageGenerator.ValueSupplierFactory
Factory for value suppliers.
|
| Constructor and Description |
|---|
MessageGenerator()
Make a simple default message generator.
|
| Modifier and Type | Method and Description |
|---|---|
MessageGenerator |
addFactories(Collection<MessageGenerator.ValueSupplierFactory> factories)
Add a collection of value supplier factories to the generator.
|
MessageGenerator |
addFactories(MessageGenerator.ValueSupplierFactory... factories)
Add value supplier factories to the generator.
|
MessageGenerator |
addFactory(MessageGenerator.ValueSupplierFactory factory)
Add a value supplier factory to the generator.
|
void |
dumpGeneratedMessages()
Dump all generated messages.
|
MessageGenerator |
dumpOnFailure()
Dump all generated messages on failure for this test only.
|
protected void |
failed(Throwable e,
org.junit.runner.Description description) |
protected void |
finished(org.junit.runner.Description description) |
<M extends PMessage<M,F>,F extends PField> |
generate(PMessageDescriptor<M,F> descriptor)
Generate a message with random content.
|
List<PMessage> |
getGenerated()
Get all generated messages.
|
Supplier<Object> |
getValueSupplier(PDescriptor descriptor)
Get the default value supplier for the given descriptor.
|
MessageGenerator |
setFairy(org.jfairy.Fairy fairy)
Set the feiry data generator being used.
|
MessageGenerator |
setFillRate(double fillRate)
Set the field fill rate in the range <0.0 ..
|
MessageGenerator |
setMaxCollectionItems(int max)
Set the max collection items for default generated collections.
|
MessageGenerator |
setMessageReader(MessageReader reader)
Set the message reader for the generator.
|
MessageGenerator |
setMessageWriter(MessageWriter writer)
Set the message writer in case of failure.
|
MessageGenerator |
setOutputSerializer(Serializer defaultSerializer)
Set default serializer to standard output.
|
MessageGenerator |
setRandom(Random random)
Set the random generator being used.
|
MessageGenerator |
setResourceReader(String resource)
Read messages from the given resource (pretty formatted).
|
MessageGenerator |
setResourceReader(String resource,
Serializer serializer)
Read messages from the given resource.
|
protected void |
starting(org.junit.runner.Description description) |
public MessageGenerator()
public <M extends PMessage<M,F>,F extends PField> M generate(PMessageDescriptor<M,F> descriptor)
M - The message type.F - The field type.descriptor - Message descriptor to generate message from.public List<PMessage> getGenerated()
public void dumpGeneratedMessages()
throws IOException
IOException - If writing the messages failed.public MessageGenerator setRandom(Random random)
random - The random generator.public MessageGenerator setFairy(org.jfairy.Fairy fairy)
fairy - The fairy data generator.public MessageGenerator setFillRate(double fillRate)
fillRate - The new full rate.public MessageGenerator addFactory(MessageGenerator.ValueSupplierFactory factory)
factory - The factory.public MessageGenerator addFactories(MessageGenerator.ValueSupplierFactory... factories)
factories - The factories.public MessageGenerator addFactories(Collection<MessageGenerator.ValueSupplierFactory> factories)
factories - The factory.public MessageGenerator setMessageWriter(MessageWriter writer)
writer - The message writer.public MessageGenerator setMessageReader(MessageReader reader)
reader - The message reader. All messages will be read from thispublic MessageGenerator setResourceReader(String resource)
resource - The resource path.public MessageGenerator setResourceReader(String resource, Serializer serializer)
resource - The resource path.serializer - Serializer to use for reading resource.public MessageGenerator setOutputSerializer(Serializer defaultSerializer)
defaultSerializer - The new default serializer.public MessageGenerator setMaxCollectionItems(int max)
max - The max number of items.public MessageGenerator dumpOnFailure()
public Supplier<Object> getValueSupplier(PDescriptor descriptor)
descriptor - The descriptor to make a supplier for.protected void starting(org.junit.runner.Description description)
starting in class org.junit.rules.TestWatcherprotected void failed(Throwable e, org.junit.runner.Description description)
failed in class org.junit.rules.TestWatcherprotected void finished(org.junit.runner.Description description)
finished in class org.junit.rules.TestWatcherCopyright © 2015–2017 morimekta.net. All rights reserved.