This page introduces common frameworks, which enable us to efficiently write unit test in StreamPipes:

JUnit: Framework to write unit tests in Java.

import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;

public class ClassNameTest {
    
    @BeforeClass
    public static void beforeClass() {
        // before each
    }

    @Before
    public void before() {
        // before each
    }

    @After
    public void after() {
        // after each
    }

    @AfterClass
    public static void afterClass() {
        // after each
    }

    @Test
    public void test() {
        // set up
        String expected = "test";

        // perform test
        String result = callMethodUnderTest();

        assertEquals(expected, result);
    }


Mockito: Create mocks for classes and services which are used in the tested method.

public class ClassName {

    private AddService service;

    public ClassName(AddService service) {
        this.service = service;
    }

    public Integer calculate(Integer i, Integer j) {
        return service.add(i, j) * 5;
    }

}


import org.mockito.Mockito.when;

public class ClassNameTest {

    @Test
    public void test() {
        // set up
        AddService service = mock(AddService.class);
        when(service.add(any(Integer.class), any(Integer.class))).thenReturn(6);
        
        ClassName testClass = new ClassName(service);

        // perform test
        Integer result = testClass.calculate(2, 3);

        assertEquals(30, result);
    }


Powermock: Extension of Mockito to mock static methods


Mock static method:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ WorkerRestClient.class })
public class ExampleTest {
    @Before
    public  void before() {
        PowerMockito.mockStatic(WorkerRestClient.class);
    }

    @Test
    public void test() {
        // void method
        doNothing().when(WorkerRestClient.class, "stopAdapter", anyString(), any(), anyString());

       // return String
       when(WorkerRestClient.startAdapter(anyString())).thenReturn("success");

    }
}


Mock Enum:

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import static org.powermock.api.mockito.PowerMockito.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ TestEnum.class })
public class Test1 {

    @Test
    public void  enumTest() throws Exception {
        TestEnum testEnum = mock(TestEnum.INSTANCE.getClass());
        when(testEnum.a()).thenReturn("lalala");
        Whitebox.setInternalState(TestEnum.class, "INSTANCE", testEnum);

        assertEquals("lalala", TestEnum.INSTANCE.a());
    }
}

public enum TestEnum {

    INSTANCE;

    public String a() {
        return "";
    }
}


Mock creating Object:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ProcessorManager.class, PmmlProcessorController.class})
public class ProcessorManagerTest {
    @Test
    public void createProcessor() throws Exception {    
        ...          
        whenNew(PmmlProcessorController.class).withAnyArguments()
                .thenReturn(mock(PmmlProcessorController.class));
        ...
        processorManager.createProcessor(null, "");

    }
}

public class ProcessorManager {

    public boolean createProcessor(PMMLConfigDescription configDescription, String username) throws PMMLConfigException {
        ...
         PmmlProcessorController controller = new PmmlProcessorController(configDescription);

        ...
    }
}


Rest-assured: Framework to test REST APIs

public class ClassNameTest {

    private Server server;

    @Before
    public void before() {
        ResourceConfig config = new ResourceConfig().register(new ResourceToTest());

        URI baseUri = UriBuilder
                .fromUri("URL_OF_RESOURCE")
                .build();

        server = JettyHttpContainerFactory.createServer(baseUri, config);
    }

    @After
    public void after() {
        server.stop();
    }

    @Test
    public void test() {
        // POST request to URL "baseUri + /v1/testapi"
        // response must be json that contains key success with value true
        given().contentType("application/json").body("data body").when()
                .post("/v1/testapi").then().assertThat()
                .body("success", equalTo(true));
    }


Wire-Mock

public class ClassName {
    public Integer calculate() {
        // makes a rest call to "ENDPOINT/"
        ...
    }
}

public class ClassNameTest {

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(PORT);

    @Test
    public void test() {
        // this
        stubFor(get(urlEqualTo("/"))
            .willReturn(aResponse()
            .withStatus(200)
            .withBody(expectedBody)));

        ClassName className = new ClassName();

        Integer result = className.calculate();

        assertEquals(30, result);
    }

    @Test
    public void test() {
        // POST request to URL "baseUri + /v1/testapi"
        // response must be json that contains key success with value true
        given().contentType("application/json").body("data body").when()
                .post("/v1/testapi").then().assertThat()
                .body("success", equalTo(true));
    }




  • No labels