Thursday, December 23, 2010

A Sample Web Application - 2 (Junit & Emma)

This is in continuation with my previous post


Now i have added following
  • Junit
Junit is a unit testing framework for Java. To use Junit with maven all i had to include it as a dependency in pom.xml

Then i wrote a test class (MatchingUsernamePasswordValidatorTest) for testing the isValidLogin(IUserContext) method of class (MatchingUsernamePasswordValidator)

As you can notice the test classes name just appends Test to the class name being tested.

This test class contains 2 tests. One for the happy scenario in which user name and password matches and another for negative scenario in which username and password don't match.

Now run
$mvn test

At the end of the output you can expect to see something like this

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running mayank.anup.ecom.login.MatchingUsernamePasswordValidatorTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.125 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
  • Emma
Emma is a code coverage tool. I have been recently introduced to it. With this you can know that how much of your code was executed during the time of your test execution. Again you have to include it in your pom.xml as a dependency.

So your pom.xml dependency list may have something like this














Now you can run emma reporting using following

$mvn emma:emma
You
can expect to see an output like following

[INFO] [emma:instrument {execu
tion: default-instrument}]
[INFO] Instrumenting classes with EMMA
processing instrumentation path ...
instrumentation path processed in 266 ms
[3 class(es) instrumented, 7 resource(s) copied]
metadata merged into [E:\myWorkspace\amj-ecom\target\coverage.em] {in 31 ms}
[INFO] [resources:testResources {e
xecutio
n: default-testResources}]
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platfo
rm dependent!
[INFO] skip non existing resourceDirectory E:\myWorkspace\amj-ecom\src\test\resources
[INFO] [compiler:testCompile {exe
cution: default-testCompile}]
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dep
endent!
[INFO] Compiling 1 source file to E:\myWorkspace\amj-ecom\target\test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: E:\myWorkspace\amj-ecom\target\surefire-reports

-------------------------------------------------------
T E S T S
---------------------------------------
----------------
Running mayank.anup.ecom.login.MatchingUsernamePasswordValidatorTest
EMMA: collecting runtime coverage data ...
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.172 sec

Results :

Tests run: 2, Failures: 0, Errors:
0, Skipped: 0

EMMA: runtime coverage dat
a merged into [E:\myWorkspace\amj-ecom\coverage.ec] {in 31 ms}
[WARNING] While downloading hibernate:hibernate:3.0.5
This artifact has been relocat
ed to or
g.hibernate:hibernate:3.0.5.


[INFO] [emma:emma {execution: default-cli}]
processing input files ...
2 file(s) read and merged in 47 ms
writing [xml] report to [E:\myWorkspace\amj-ecom\target\site\emma\coverage.xml] ...
writing [html] report to [E:\myWorkspace\amj-ecom\target\site\emma\index.html] ...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 24 seconds

The key thing to note is that first emma does some byte code level instrumentation with your generated main classes, then it
runs the Junit Test and finally prints the result output in html format.


No comments: