Class GeneratorWatcher<Context extends GeneratorContext<Context>>

  • All Implemented Interfaces:
    org.junit.rules.TestRule
    Direct Known Subclasses:
    SimpleGeneratorWatcher

    public class GeneratorWatcher<Context extends GeneratorContext<Context>>
    extends org.junit.rules.TestWatcher
    Providence 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 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 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 PMessage<M>> M generate​(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.
      • context

        public Context context()
        Returns:
        The watchers message generator options.
      • dumpGeneratedMessages

        public void dumpGeneratedMessages()
                                   throws java.io.IOException
        Dump all generated messages.
        Throws:
        java.io.IOException - If writing the messages failed.
      • dumpGeneratedMessages

        public void dumpGeneratedMessages​(@Nonnull
                                          PMessageDescriptor descriptor)
                                   throws java.io.IOException
        Dump 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​(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​(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:
        starting in class org.junit.rules.TestWatcher
      • failed

        protected void failed​(java.lang.Throwable e,
                              org.junit.runner.Description description)
        Overrides:
        failed in class org.junit.rules.TestWatcher
      • finished

        protected void finished​(org.junit.runner.Description description)
        Overrides:
        finished in class org.junit.rules.TestWatcher