RSS

Tag Archives: JUnit

Using JUnit in XPages Projects

Yes! I’ve read some books during my holidays. Clean Code was one of them and also Test Driven Development. Both of them where very interesting and has closed some open questions. Back from holidays, I’ve started to research how I can implement JUnit testing to XPages Applications. The following StackOverflow question brings it to the reality: There is no easy way!

But wait, Jakob Majkilde had a good idea! Why not integrate the testing direct to the application and using the XPages rendering engine to present the result. This would not be the first time, that testing was done direct in the project. Inspired by this idea, I began to research, how eclipse presents the results of JUnit tests and also how the maven-surfire-plugin generates its results. I learned a lot about JAXB and the junit-4.xsd to generate the xml. Some short test, figured out that I’ve to make another plugin. But this time somehow different. I decide to work aligned to the TDD principles. It was a very nice experience. And also a very fast approach (I was really surprised!).

And here it is the org.openntf.junit.xsp plugin. Install it like you do it with the ExtLib or the OpenNTF Essentials (maybe it will become a part of it). And yes, server and DDE. Now you can activate the library in the xsp.properties.

junit-xsppropsActivating the junit library gives you the capability to write tests and use the TestSuite control. But lets start with a simple test class. This class makes not a lot of sense, but shows a bit of the capabilities of JUnit.

package org.openntf.junit.example;

import static org.junit.Assert.*;

import org.junit.Test;

public class TestMock2 {

    @Test
    public void checkTrue() {
        assertTrue(true);
    }

    @Test
    public void checkFalse() {
        System.err.println("MOCK2SYSERR");
        System.out.println("MOCK2SYSOUT");
        assertFalse(true);
    }

    @Test
    public void checkEquals() throws InterruptedException {
        Thread.sleep(201);
        assertEquals(10, 10);
    }
}

The class contains 3 test. They are marked with the @Test annotation as a test. The most methods ends with some kind of a assertXXXXX. With the assertXXXX functionality is the result checked. Two of this tests will be successful, one will fail. To test the capacity of my JUnit runner implementation, I’ve made in checkEquals a Thread.sleep to see if my time measuring works. checkFalse() contains System.out and System.err to check, if I can capture the Output during the test. Fortunatly its possible 🙂

Next step -> new XPage called junit.xsp

I’ve then made a new XPages called junit.xsp (but call it the way you love). Include the TestSuite Control on this XPages and define the classes to test. The following code shows the result:junitxsp

When you load the page, you will get the following result rendered for the junit:testsuite control:

junit-resultAnd yes you are right! The red bar is not the thing you wanna see! Now you can go and fix the problems. After that a refresh on this page and you see, that all is green 🙂

Btw. the definition

downloadFile="TEST-Failures.xml"

gives you the capability to download the report with <pagename.xsp>/<downloadFile> e.g. junit.xsp/TEST-Failures.xml

Have fun!

Christian

 
3 Comments

Posted by on August 21, 2014 in OpenNTF

 

Tags: