Application Platform 10.3 | Application Platform API | Generating Tests with the Application Platform Integration Test Framework | Examples of Using the Application Platform Integration Test Framework
 
Examples of Using the Application Platform Integration Test Framework
This section provides examples of a non-parameterized and a parameterized test in the Application Platform integration test framework.
Example of a Non-Parameterized Test
The following example shows the GreeterImpl class published as an OSGi service that depends on the ResourceUtil class that is in turn published as another OSGi service.
In the example, the test class for the GreeterImpl class verifies that the IGreeter API implementation is correctly registered as an OSGi service and is accessible using the ServiceUtil class.
The @TestBundle annotation specifies the symbolic name of the project bundle that contains this test class.
The @RunOnServer annotation is explicitly specified. However, it is not required, as it uses the default values.
The test class inherits from the AppPlatformIntegrationTest class and it implicitly uses the IntegrationTestRunner JUnit custom runner.
@TestBundle(symbolicName="greeter-service")
@RunOnServer(host="localhost", jmxPort=8075, username="Administrator",
pwd="manage")
public class GreeterImplTest extends AppPlatformIntegrationTest {
    @Test
    public void testGreeterServiceRegistered() throws Exception {
        IGreeter greeter = ServiceUtil.getService(IGreeter.class);
        assertNotNull(greeter);
 
        String result = greeter.greetMe("test");
        assertNotNull(result);
        assertTrue(result.contains("test"));
        assertTrue(greeter instanceof GreeterImpl);
        System.out.println("Passed!");
    }
Example of a Parameterized Test
The following example shows a simple parameterized test that runs in the Application Platform integration test framework. The example consists of the following parts:
*A simple POJO class, named Hello. This class returns a greeting string for a provided input name.
*A JUnit test that tests the Hello class. The test uses the @Parameters annotation and the Application Platform parameterized integration test support.
public class Hello { 
    String name;
    Hello(String name) {
        this.name = name;
    }
    String greet() {
        return String.format("Hello, %s", name == null ? "Guest!" : name);
    }
}
 
@TestBundle(symbolicName = "HelloBundle") 
@RunOnServer(jmxPort = 8075) 
public class HelloTest extends AppPlatformIntegrationTestWithParameters { 
    @Parameters(name = "test-{index}-with-name-{0}") 
    public static Iterable<Object[]> data() {
       return Arrays.asList(new Object[][] { { "abc", "Hello, abc" }, 
       { null, "Hello, Guest!" } });
    }
 
    @Parameter(0)
    public String name;
    @Parameter(1)
    public String greeting;
 
    @Test
    public void test() {
       String result = new Hello(name).greet();
       System.out.println("Got result: " + result + " for input name: " + name);
       assertEquals(greeting, result);
    }
}