RSS

Using JUnit in XPages Projects

21 Aug

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:

3 responses to “Using JUnit in XPages Projects

  1. Stephan Koops

    August 27, 2014 at 10:29 am

    Hi,

    what came first in my mind is, that in this case the test code must be in the nsf database than the productive logic. As far as I know it is not possible to call Java code from outside the nsf (except starting an agent, but that’s not the solution, because for tests you have do a lot of methods calls with parameters).
    This has some disadvantages:
    * It could happen that you accidentially use the test code or test JARs in the productive code.
    * You have to deliver the test code to the customer, which is bad style.
    * Potentially the customer could use the logic that is public to everyone (the test code), what is not intended to be.
    For this kind of usage there should be at least an easy way (that means one click / one command) to compile the nsf without the test logic (source code and JARs).

    Is there an easy way to remove the test code and the test JARs when creating the ntf to deliver to customer from the development nsf? E.g. remove a special package? Is it possible to have multiple JAR folders and source folders in an nsf file? Than a way could be to remove the test logic folders (JARs and source). I don’t know it, because I came from the Java side to Notes and XPages, and so I’m primary developing the Java backend

    We have the following project structure for our test driven development:
    * OurProject.nsf
    * OurProject.ondisk [synchronized with OurProject.nsf by the Designer]
    * OurProject.tests [dependency to the on disk project]

    The test project runs as classic Eclipse (Designer) JUnit test against the ondisk project. (Run again the nsf project is not possible, because no class files are on the file system in the nsf project, so they could not be found by the JVM). You have to use the folder Code/Java in the on disk project as source folder (while sync with nsf this get sometimes lost and you have to re-add it). This allows to run every test class or test method as you want from the Package Explorer. (This Eclipse view is well known for Java developers. To open it: Menu Window -> Show Eclipse views -> Package explorer or Window -> Show Eclipse views -> other -> Java -> Package explorer).
    So we have a clear separation of the productive logic and the test logic and no test JAR nor code is in the nsf. I’ve created a user library in Designer / Eclipse that contains the JARs for tests only (e.g. junit, hamcrest and mockito), which could be added to all test projects.
    The disadvantage is, that you have to develop test driven in the on disk project and than not to forget to synchronise with nsf before do the real tests in browser. But we have no mix between test and productiv code.

    BTW: If you made accidentially changes in the same file in both projects, and the designer can’t synchronise this files. But than you could mark both projects in the Package explorer (see above), right click -> Compare with -> each other. Than you have the classic compare editor of Eclipse, which allows to copy one change in a source file from right to left and another from left to right.

    Maybe the discussion results in a good combination.

    best regards
    Stephan Koops

    BTW: the shown junit.xsp is not a valid xsp file.

     
    • guedebyte

      September 2, 2014 at 7:57 am

      Having the test code in the application is definitely not the final solution. But think about this:
      - The current approach is easy to adapt by each developer
      - If you make a production version of your template, you only have to delete the test package, the junit.xsp page and the dependency to the org.openntf.junit.xsp library
      - And what if the maven build plugin for the headless designer is abel to to this build part for you?

      Btw: If fixed the wrong junit.xsp Code. I’ve to train my wordpress blog not to kill my XML code

       

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 675 other followers

%d bloggers like this: