Class GeneratorWatcher<Context extends net.morimekta.providence.testing.generator.GeneratorContext<Context>>
- java.lang.Object
-
- org.junit.rules.TestWatcher
-
- net.morimekta.providence.testing.junit4.GeneratorWatcher<Context>
-
- All Implemented Interfaces:
org.junit.rules.TestRule
- Direct Known Subclasses:
SimpleGeneratorWatcher
public class GeneratorWatcher<Context extends net.morimekta.providence.testing.generator.GeneratorContext<Context>> extends org.junit.rules.TestWatcherProvidence message serializer that can be used as a junit rule.class MyTest { {@literal @}Rule public SimpleGeneratorWatcher gen = GeneratorWatcher .create() .dumpOnFailure() .withGenerator(MyMessage.kDescriptor, gen -> { gen.setAlwaysPresent(MyMessage._Fields.UUID, MyMessage._Fields.NAME); gen.setValueGenerator(MyMessage._Fields.UUID, () -> UUID.randomUUID().toString()); }); {@literal @}Test public testSomething() { MyMessage msg = gen.context().nextMessage(MyMessage.kDescriptor); sut.doSomething(msg); assertThat(sut.state(), is(SystemToTest.CORRECT)); } {@literal @}Test public testSomethingElse() { gen.generatorFor(MyMessage.kDescriptor) .setValueGenerator(MyMessage._Field.NAME, () -> "Mi Nome") .setAlwaysPresent(MyMessage._Field.AGE) .setValueGenerator(MyMessage._Field.AGE, () -> 35); MyMessage msg = gen.context().nextMessage(MyMessage.kDescriptor); sut.doSomething(msg); assertThat(sut.state(), is(SystemToTest.CORRECT)); } }
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedGeneratorWatcher(Context globalContext)Make a simple default message generator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description GeneratorWatcher<Context>apply(java.util.function.Consumer<Context> consumer)Contextcontext()static SimpleGeneratorWatchercreate()Create a default message generator watcher.static <Context extends net.morimekta.providence.testing.generator.GeneratorContext<Context>>
GeneratorWatcher<Context>create(Context base)Create a message generator watcher with the given base context.voiddumpGeneratedMessages()Dump all generated messages.voiddumpGeneratedMessages(net.morimekta.providence.descriptor.PMessageDescriptor descriptor)Dump all generated messages.GeneratorWatcher<Context>dumpOnFailure()Dump all generated messages on failure for this test only.GeneratorWatcher<Context>dumpOnFailure(net.morimekta.providence.descriptor.PMessageDescriptor descriptor)Dump all generated messages on failure for this test only.protected voidfailed(java.lang.Throwable e, org.junit.runner.Description description)protected voidfinished(org.junit.runner.Description description)<M extends net.morimekta.providence.PMessage<M>>
Mgenerate(net.morimekta.providence.descriptor.PMessageDescriptor<M> descriptor)Generate a message with random content using the default generator for the message type.GeneratorWatcher<Context>setOutputSerializer(net.morimekta.providence.serializer.Serializer defaultSerializer)Set default serializer to standard output.protected voidstarting(org.junit.runner.Description description)
-
-
-
Constructor Detail
-
GeneratorWatcher
protected GeneratorWatcher(Context globalContext)
Make a simple default message generator.- Parameters:
globalContext- The global / default generator context.
-
-
Method Detail
-
create
public static SimpleGeneratorWatcher create()
Create a default message generator watcher.- Returns:
- The watcher instance.
-
create
public static <Context extends net.morimekta.providence.testing.generator.GeneratorContext<Context>> GeneratorWatcher<Context> create(Context base)
Create a message generator watcher with the given base context.- Type Parameters:
Context- The context type.- Parameters:
base- The base generator to use when generating messages.- Returns:
- The watcher instance.
-
generate
public <M extends net.morimekta.providence.PMessage<M>> M generate(net.morimekta.providence.descriptor.PMessageDescriptor<M> descriptor)
Generate a message with random content using the default generator for the message type.- Type Parameters:
M- The message type.- Parameters:
descriptor- Message descriptor to generate message from.- Returns:
- The generated message.
-
apply
public GeneratorWatcher<Context> apply(java.util.function.Consumer<Context> consumer)
-
context
public Context context()
- Returns:
- The watchers message generator options.
-
dumpGeneratedMessages
public void dumpGeneratedMessages() throws java.io.IOExceptionDump all generated messages.- Throws:
java.io.IOException- If writing the messages failed.
-
dumpGeneratedMessages
public void dumpGeneratedMessages(@Nonnull net.morimekta.providence.descriptor.PMessageDescriptor descriptor) throws java.io.IOExceptionDump all generated messages.- Parameters:
descriptor- Message type to dump messages for.- Throws:
java.io.IOException- If writing the messages failed.
-
setOutputSerializer
public GeneratorWatcher<Context> setOutputSerializer(net.morimekta.providence.serializer.Serializer defaultSerializer)
Set default serializer to standard output. If test case not started and a writer is already set, this method fails. Not that this will remove any previously set message writer.- Parameters:
defaultSerializer- The new default serializer.- Returns:
- The message generator.
-
dumpOnFailure
public GeneratorWatcher<Context> dumpOnFailure()
Dump all generated messages on failure for this test only.- Returns:
- The message generator.
-
dumpOnFailure
public GeneratorWatcher<Context> dumpOnFailure(net.morimekta.providence.descriptor.PMessageDescriptor descriptor)
Dump all generated messages on failure for this test only.- Parameters:
descriptor- Message type to dump messages for.- Returns:
- The message generator.
-
starting
protected void starting(org.junit.runner.Description description)
- Overrides:
startingin classorg.junit.rules.TestWatcher
-
failed
protected void failed(java.lang.Throwable e, org.junit.runner.Description description)- Overrides:
failedin classorg.junit.rules.TestWatcher
-
finished
protected void finished(org.junit.runner.Description description)
- Overrides:
finishedin classorg.junit.rules.TestWatcher
-
-