Polygene™
Introduction
Tutorials
Javadoc
Samples
Core
Libraries
Extensions
Tools
Glossary 

Core Test Support

code

docs

tests

Polygene™ comes with classes to help with testing. For general development, only a couple of classes are of interest as the others are mostly for EntityStore and Index/Query SPI implementations. There is also some mocking support, to allow some of Polygene’s unique aspects to be mocked, but since Polygene™ is so flexible at a fine-granular level, we have found that mocking is seldom, if ever, needed.

Table 16. Artifact

Group IDArtifact IDVersion

org.apache.polygene.core

org.apache.polygene.core.testsupport

3.0.0


Your First Testcase

In most cases, you will probably use the AbstractPolygeneTest class to simplify starting a Polygene™ test instance.

public class HelloTest extends AbstractPolygeneTest
{
  [...snip...]

}

This will do all the initialization of a Polygene™ runtime instance and create a single layer with a single module in it. What goes into that module is declared in the assembly() method;

@Override
public void assemble( ModuleAssembly module )
{
    module.values( Hello.class );
}

In this case we declare that we have a ValueComposite of type org.apache.polygene.tutorials.hello.Hello which looks like

/**
 * This Composite interface declares a simple "Hello World" interface with a single say() method.
 * What is being said is defined in the HelloWorldState interface, which is a private mixin.
 */
@Mixins( Hello.HelloWorldMixin.class )
public interface Hello
{
    String say();

    /**
     * This is the implementation of the say() method.
     */
    class HelloWorldMixin
        implements Hello
    {
        // @This reference the composite itself,
        // and since HelloWorldState is not part of the public interface,
        // it is a private mixin.
        @This
        private State state;

        @Override
        public String say()
        {
            return state.phrase().get() + " " + state.name().get();
        }
    }

    /**
     * This interface contains only the state of the HelloWorld object.
     */
    interface State
    {
        @NotEmpty
        Property<String> phrase();

        @NotEmpty
        Property<String> name();
    }
}

The say() method will get the phrase and name from its internal state (the State interface is not magical, it could be named anything).

And then we create the actual test;

@Test
public void givenHelloValueInitializedToHelloWorldWhenCallingSayExpectHelloWorld()
{
    ValueBuilder<Hello> builder = valueBuilderFactory.newValueBuilder( Hello.class );
    builder.prototypeFor( Hello.State.class ).phrase().set( "Hello" );
    builder.prototypeFor( Hello.State.class ).name().set( "World" );
    Hello underTest = builder.newInstance();
    String result = underTest.say();
    assertThat( result, equalTo( "Hello World" ) );
}

By using the prototypeFor() we can access the hidden, internal and very private state of the ValueComposite. Once the value is created we can reach this directly.