jUnit 4.12 Annotation example for NetRexx 3.06

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

jUnit 4.12 Annotation example for NetRexx 3.06

rvjansen
To show what we can do now that there is annotation support, the following jUnit 4.12 example is presented. This is the ‘getting started’ example from the jUnit website at https://github.com/junit-team/junit4/wiki/Getting-started , adapted for NetRexx 3.06.

Make sure org.junit-4.12.jar and hamcrest-core-1.3.jar are on the CLASSPATH

First, we have to make a class-under-test, we call it Calculator, in a file called Calculator.nrx:

class Calculator

  method evaluate(expression=String) returns int
    sum = int 0
    loop summand over expression.split("\\+")
      sum = sum + Integer.valueOf(summand).intValue()
    end
    return sum


As you compare it with the original, there are only slight differences, as the for: has been replaced with a (much more readable imho) loop over. I left the String.split() as is.

Then we make a class that tests this class, we call it CalculatorTest, in a file called CalculatorTest.nrx:

import org.junit.Test

class CalculatorTest

  method CalculatorTest()

    @Test
  method evaluatesExpression()
    calc = Calculator()
    sum = calc.evaluate("1+2+3")
    org.junit.Assert.assertEquals(6, sum)


We compile both classes, and run them with:

╭─rvjansen@fifi  ~/test  ‹master*› 
╰─$ java org.junit.runner.JUnitCore CalculatorTest         
JUnit version 4.12
.
Time: 0.094

OK (1 test)

With this, we see that jUnit versions from this century suddenly can be used. Note that I inserted a class constructor, as ‘program-level’ and ‘class-level’ annotations currently do not work yet.
When compared to the Java example, also the static import statement does not work. (There are no current plans to implement this. It works fine without it).

As an exercise, you can make the example fail as shown on the aforementioned web page, by making the ariithmetic fail.

You can also try a more adventurous approach by commenting out the @Test annotation, and notice that the program still compiles, as it is valid NetRexx. But when running the example, it turns out that the annotation performed the function the jUnit designers intended for it, and now running the test fails:

╭─rvjansen@fifi  ~/test  ‹master*› 
╰─$ java org.junit.runner.JUnitCore CalculatorTest
JUnit version 4.12
.E
Time: 0.004
There was 1 failure:
1) initializationError(CalculatorTest)
java.lang.Exception: No runnable methods
at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)


This is working as designed, and also happens when we remove the @Test annotation from the Java version of CalculatorTest. So leaving out an essential annotation works the same in Java as it does in NetRexx; neither of them yields a compile time error due to this omission. In NetRexx 3.05 and earlier, of course, the program fails to compile with the annotation added.

Stand by for the next example of the new annotations feature!

best regards,

René.




_______________________________________________
Ibm-netrexx mailing list
[hidden email]
Online Archive : http://ibm-netrexx.215625.n3.nabble.com/

Loading...