This page describes how to implement a service to be used by test cases with the AludraTest framework.
This documentation is intended for developers wanting to extend AludraTest, not for clients using AludraTest.
A service provides AludraTest test cases with features to access systems under test. Service invocations can be logged automatically using the Log4testing component.
There exists a separation of concern between a core service management class (implementing the interface AludraService) and three operational interfaces (implementors of the Action interfaces Interaction, Verification and Condition). An optional interface for error checking (ErrorChecker) may be implemented to provide information about errors.
The following steps need to be performed in order to implement a new AludraTest service called HelloService:
perform()
, verify()
and check()
to return the specific action interface type:TODO Remove ErrorChecker from image
When your service operations detect an error, throw one of the predefined AludraTest exceptions:
public void assertHello() { String msg = componentUnderTest.retrieveMessage(); if (!msg.equals("Hello")) { throw new FunctionalFailure("Retrieved message other than 'Hello'"); }
public void isHello() { try { String msg = componentUnderTest.retrieveMessage(); return msg.equals("Hello"); } catch (SomeSocketOrIOException e) { throw new AutomationException("Could not check message because of communication error", e); }
private String retrieveMessage() { try { complexExternalSystem.setTimeout(getMyConfiguredTimeout()); String message = complexExternalSystem.readNextMessage(); } catch (SomeTimeoutException e) { throw new PerformanceFailure("Timeout when waiting for message"); } }
initService()
method of the service (when extending AbstractAludraService), e.g.:public void initService() { try { complexExternalSystem = MyLibrary.connectToHelloSystem(getConfiguredAddress()); } catch (SomeConnectionException e) { throw new AccessFailure("Cannot connect to the Hello System at " + getConfiguredAddress(), e); } }
NullPointerException
) automatically in a TechnicalException. This marks an "completely unexpected exception", which could indicate an AludraTest error (e.g. in your service implementation itself).The different types of exceptions imply different outcomes of the test case, and are used by the Log4Testing component to render the output, e.g. the color coding used for single test steps.
A service is instantiated and initialized by the AludraTest framework in three steps:
After a test is finished, the AludraTest framework closes all services by calling each one's close() method.
Complex services often require the possibility to configure the service, in many cases it is even required to use different configurations for different tests. AludraTest has a complex Configuration Component which can be used by services just by extending AbstractConfigurableAludraService instead of AbstractAludraService. Please refer to Service Configuration on information how you can configure your service, and refer to the Javadoc of the interface Configurable to learn how your service instance will receive the configuration.
In order to access the service in a test run, one has to assign the service implementation to the service interface in the configuration file aludraservice.properties
, e.g.
com.my.HelloService=com.my.HelloServiceImpl
Any AludraTestCase class has one method available with which it can obtain an instance of the service: getService()
. This method expects a ComponentId parameter which identifies the type of service you want to obtain. ComponentId
has no constructor, but two static factory methods:
The method you use influences the configuration which is used for the service, if any. For a non-configurable service, you can always use the first method. Also use the first method to identify the "global" variant of a configurable service. The service object you retrieve will use the "global" configuration for the service type.
The second method adds an instance identifier to the service identifier. This allows configurators to provide configuration especially for a single use case of the service type.
Please refer to Service Configuration to learn more about the different configuration scopes and the effect of different instance identifiers.
Example for retrieving a HelloService:
@Test public void myTest() { HelloService myServiceObj = getService(ComponentId.create(HelloService.class, 'MyTest')); // some complex operations on myServiceObj // no close required - is called automatically by AludraTest framework }