UP | HOME
../../

Unit Testing

1 Unit Testing

  1. OOP and programming language (PL) neutral term "unit" cf. class, method
  2. Unit test: test one invocation of one method of one isolated class with one thread
  3. Tools for unit testing: JUnit and TestNG (both for any JVM based PL)

1.1 Unit Testing

  1. "The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. Each unit is tested separately before integrating them into modules to test the interfaces between modules. Unit testing has proven its value in that a large percentage of defects are identified during its use." [from msdn.microsoft.com]

1.2 Unit Testing

  1. "The most common approach to unit testing requires drivers and stubs to be written. The driver simulates a calling unit and the stub simulates a called unit. The investment of developer time in this activity sometimes results in demoting unit testing to a lower level of priority and that is almost always a mistake. Even though the drivers and stubs cost time and money, unit testing provides some undeniable advantages. It allows for automation of the testing process, reduces difficulties of discovering errors contained in more complex pieces of the application, and test coverage is often enhanced because attention is given to each unit." [from msdn.microsoft.com]

1.3 Unit Testing

  1. We do not expect the methods being unit tested to:
    1. Access the network
    2. Hit a database
    3. Alter the file system
    4. Spin up a thread
    5. etc.
  2. A method that failed a unit test is almost always easy to rectify in minutes.

2 Unit Test Generation for OOP code

  1. Write a test class TC for a class C to be tested. Unit test methods reside in TC source code files. A test method TC.tm() invokes a method m of C. It knows what the expected result is. It asserts that the actual result equals expected. TC.tm() creates one or more objects of C.
  2. To invoke a method of a lower-level class, we must create argument objects to pass.
  3. Much of the implied source code above is mechanically generatable by parsing the source code of C.
  4. All modern OOP IDE have this feature set. They generated the "boiler plate" code and we fill in the specifics of the test.
  5. To help with the above, the IDE suggests "annotations" (e.g., @test) and other naming conventions (e.g., class MyClassTest for class MyClass)

3 Java Reflection API

  1. Reflection API provides info re the class C
  2. At run time, by examining the C.class file (even without C.java).
  3. Inspect and use classes, methods, attributes, …
  4. http://docs.oracle.com/javase/tutorial/reflect/index.html
  5. Example
    Method med = foo.getClass().getMethod("doSomething", null);
    med.invoke(foo, null);
    

4 JUnit and TestNG

  1. JUnit.org and TestNG.org are unit testing frameworks for Java with plugins for IDEAJ, and Eclipse.
  2. Both use reflection.
  3. TestNG vs Junit Feature comparison. 2015.

4.1 Buzzwords

  1. POJO "Plain Old Java/ JavaScript Object"
  2. CI server Continuous Integration server
  3. Selenium a tool to emulate user interaction with a web page.
  4. WebDriver An official W3C Specification. A method of interacting with a web browser.

5 References

  1. Yoonsik Cheon and Gary T. Leavens, "The JML and JUnit Way of Unit Testing and its Implementation", February 2004 PDF Recommended Reading.
  2. http://www.cse.lehigh.edu/~glennb/oose/ppt/Junit.ppt Slides. Recommended Reading.
  3. http://www.sfs.uni-tuebingen.de/~vhenrich/ws12-13/java/slides/JUnit.pdf 2012 Slides. Recommended Reading.
  4. Beyond JUnit: TestNG the next gen PPT 50+ slides, Cédric Beust, Designer of TestNG, 2007 ./testNG-gtac-2007.ppt Required Reading.
  5. http://www.tutorialspoint.com/testng/ Recommended Reading.
  6. http://tech.lds.org/wiki/images/5/5e/Unit_Testing_with_TestNG.pdf lots of effective photos. Recommended Reading.
  7. Several tutorials on YouTube.com! Recommend back one that you liked.

Copyright © 2016 pmateti@wright.edu www.wright.edu/~pmateti 2016-03-23