Unit Testing in Java

62 %
38 %
Information about Unit Testing in Java

Published on March 11, 2014

Author: AhmedMGomaa



Presentation for Using Unit Testing to test your code with closer scope over JUnit Testing for Java

{ Introduction to unit testing What is unit testing, and how we doing it. Ahmed Gomaa Jul 16, 2013 (v 1.2)

 What is unit testing  Unit Testing  Test Suit  Why unit testing  Frameworks & JUnit  Code Coverage  How we doing it  Installation of JUnit  Naming  Creating test unit  Creating test suit  Running the test  Test Automation Content

What is unit testing

 A unit test is a piece of code written by a developer that executes a specific functionality in the code which is tested. The percentage of code which is tested by unit tests is typically called test coverage.  Unit tests target small units of code, e.g. a method or a class, (local tests) whereas component and integration tests targeting to test the behavior of a component or the integration between a set of components or a complete application consisting of several components.  Unit tests ensure that code works as intended. They are also very helpful to ensure that the code still works as intended in case you need to modify code for fixing a bug or extending functionality. Having a high test coverage of your code allows you to continue developing features without having to perform lots of manual tests. Unit Test

 The test suit is a group of test cases combined tests a certain functionality or module.  The relation between test cases and test suit is many to many, as one test case can be part of multiple test suits. Test Suite

 Faster Development  Higher Quality  More flexibility  Easer Development (specially for newcomers)  Test Driven Development Why unit testing!

 Unit testing have a lot of frameworks that help simplify the process of unit testing and help in testing automation.  JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.  JUnit is open source project can easily be used and automated, and it is also has plugins for most of IDEs (like: eclipse and net beans).  JUnit is the most used unit testing framework.  Since JUnit 4, it is using the annotations to define the unit tests and test suits. Frameworks & JUnit

 Code Coverage represents the amount of the code covered by unit testing.  JaCoCo:  JaCoCo is an open source toolkit for measuring and reporting Java code coverage.  JaCoCo is a java tool (command line) used to check the code coverage.  EclEmma:  EclEmma is a free Java code coverage plugin for Eclipse.  EclEmma was originaly based on EMMA code coverage tool, since v2.0 is based in JaCoCo. Code Coverage

How we do it

 The only thing you need to do is to add 2 JARs:  junit.jar  hamcrest-core.jar  > download: team/junit/wiki/Download-and-Install Installation of JUnit

 Unit testes will be created inside the same project with the same packages and classes naming, except: 1. It will be under a root package called “unitTest”. 2. Test unit will be suffixed with “TestUnit”. 3. Test suit will be suffixed with “TestSuit”.  Example:  Business Class (class will be tested): net.tedata.webservices.getCustomerInfo.GetCustomerI nfo  Unit Test Class: ustomerInfoTestUnit Naming

 Unit test class is not required to inherit or extend any other class or interface.  Only the test methods need to be annotated with “@Test” annotation.  JUnit assumes that all test methods can be executed in an arbitrary order. Therefore tests should not depend on other tests.  Adding test methods (fail, or asserts). Creating test unit

 Example Method: Creating test unit @Test public void testMultiply() { // MyClass is tested MyClass tester = new MyClass(); // Check if multiply(10,5) returns 50 assertEquals("10 x 5 must be 50", 50, tester.multiply(10, 5)); }

 List of JUnit annotations: 1. @Test: The annotation @Test identifies that a method is a test method. 2. @Test(expected = Exception.class): Fails, if the method does not throw the named exception. 3. @Test(timeout=100): Fails, if the method does not throw the named exception. 4. @Ignore: Ignores the test method. This is useful when the underlying code has been changed and the test case has not yet been adapted. Or if the execution time of this test is too long to be included. 5. @Before, @After, @BeforeClass, @AfterClass: Before and after will run before every test method run, and class ones will run once before all the test cases run and this method should be static. Creating test unit

 Assert Statements (methods) list: 1. fail(String): Let the method fail. Might be used to check that a certain part of the code is not reached. Or to have a failing test before the test code is implemented. 2. assertTrue([message], boolean condition): Checks that the boolean condition is true. 3. assertsEquals([String message], expected, actual): Tests that two values are the same. Note: for arrays the reference is checked not the content of the arrays. 4. assertsEquals([String message], expected, actual, tolerance): Test that float or double values match. The tolerance is the number of decimals which must be the same. 5. assertNull([message], object): Checks that the object is null. 6. assertNotNull([message], object): Checks that the object is not null. 7. assertSame([String], expected, actual): Checks that both variables refer to the same object. 8. assertNotSame([String], expected, actual): Checks that both variables refer to different objects. Creating test unit

 The test suit has 2 annotations: 1. @RunWith(Suite.class): Fixed annotation 2. @SuiteClasses({ MyClassTestCases.class }): contains the list of the test cases to run.  Example: Creating Test Suit @RunWith(Suite.class) @SuiteClasses({ MyClassTest.class, MySecondClassTest.class }) public class AllTests { }

 From Eclipse (using plugin):  Right click on the test case or the suit.  Then “Run As”  Then “JUnit Test”  Outside Eclipse:  Example: Running the test

 JUnit testing already can be automated by both Ant and Maven. Test Automation


 JUnit tutorial:   JUnit official web site:   List of unit testing frameworks:   Java Code Coverage Tools:   EclEmma:   Eclipse Update Site:  It is also available on Eclipse market place.  JaCoCo:  Home:  Git Hub: References

Add a comment

Related presentations

Presentación que realice en el Evento Nacional de Gobierno Abierto, realizado los ...

In this presentation we will describe our experience developing with a highly dyna...

Presentation to the LITA Forum 7th November 2014 Albuquerque, NM

Un recorrido por los cambios que nos generará el wearabletech en el futuro

Um paralelo entre as novidades & mercado em Wearable Computing e Tecnologias Assis...

Microsoft finally joins the smartwatch and fitness tracker game by introducing the...

Related pages

Unit Testing mit JUnit - Frank Westphal

24.6.2001 Unit Tests mit JUnit Automatisierte Unit Tests in Java. JUnit ist ein kleines, mächtiges Java-Framework zum Schreiben und Ausführen ...
Read more

JUnit – Wikipedia

JUnit ist ein Framework zum Testen von Java-Programmen, das besonders für automatisierte Unit-Tests einzelner Units (Klassen oder Methoden) geeignet ist.
Read more

Unit Testing with JUnit - Tutorial - vogella

This tutorial explains unit testing with JUnit 4.x. It explains the creation of JUnit tests. ... There are several testing frameworks available for Java.
Read more

Unit Testing for Java EE - Oracle

Unit Testing for Java EE. By Adam Bien. Think testing Java EE applications is difficult, inconvenient, or too complex? Learn why this isn't the case and ...
Read more

Java Unit Testing -

This trail is about unit testing in Java. Unit testing means testing the smaller units of your application, like classes and methods. The reason you test ...
Read more

Modultest – Wikipedia

Linkkatalog zum Thema Unit-Testing bei DMOZ ... Artikel zum Thema Komponententests unter Java (englisch) Einzelnachweise ...
Read more

JUnit - About

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.
Read more

List of unit testing frameworks - Wikipedia

This page is a list of tables of code-driven unit testing frameworks for various programming ... An integrated Java unit testing solution for Eclipse.
Read more

Unit Testing Lessons in Ruby, Java and .NET - The Art Of ...

Lots of videos about TDD and Unit Testing, and the official site for the book
Read more

jUnit Testing framework - JUnit Tutorial

JUnit is a unit testing framework for Java programming language. JUnit has been important in the development of test-driven development, and is one of a ...
Read more