Topic Junit Presenters Govindaramanujam Sama Jansen Erwin What

  • Slides: 10
Download presentation
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin

Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin

What is Junit? It is a simple framework to write repeatable tests. How to

What is Junit? It is a simple framework to write repeatable tests. How to use it? When you need to test something, here is what you do: 1. Create an instance of Test. Case: 2. Create a constructor which accepts a String as a parameter and passes it to the superclass. 3. Override the method run. Test() 4. When you want to check a value, call assert. True() and pass a boolean that is true if the test succeeds

A Simple Example To test that the sum of two Moneys with the same

A Simple Example To test that the sum of two Moneys with the same currency contains a value which is the sum of the values of the two Moneys, write: public void test. Simple. Add() { Money m 12 CHF= new Money(12, "CHF"); Money m 14 CHF= new Money(14, "CHF"); Money expected= new Money(26, "CHF"); Money result = m 12 CHF. add(m 14 CHF); assert. True(expected. equals(result)); }

What is a text fixture? If tests need to run against the background of

What is a text fixture? If tests need to run against the background of a known set of objects, the set of objects is called a test fixture. When you have a fixture, here is what you do: 1. Create a subclass of Test. Case 2. Create a constructor which accepts a String as a parameter and passes it to the superclass. 3. Add an instance variable for each part of the fixture 4. Override set. Up() to initialize the variables 5. Override tear. Down() to release any permanent resources you allocated in set. Up

An Example: To write several test cases that want to work with different combinations

An Example: To write several test cases that want to work with different combinations of 12 Swiss Francs, 14 Swiss Francs, and 28 US Dollars, first create a fixture: public class Money. Test extends Test. Case { private Money f 12 CHF; private Money f 14 CHF; private Money f 28 USD; protected void set. Up() { f 12 CHF= new Money(12, "CHF"); f 14 CHF= new Money(14, "CHF"); f 28 USD= new Money(28, "USD"); } }

How do you write and invoke an individual test case when you have a

How do you write and invoke an individual test case when you have a Fixture? Here is what you do: 1. Write the test case method in the fixture class. Be sure to make it public, or it can't be invoked through reflection. 2. Create an instance of the Test. Case class and pass the name of the test case method to the constructor

How do you run several tests at once? Use a Test Suite. An Example

How do you run several tests at once? Use a Test Suite. An Example To create a suite of two test cases and run them together, execute: Test. Suite suite= new Test. Suite(); suite. add. Test(new Money. Test("test. Money. Equals")); suite. add. Test(new Money. Test("test. Simple. Add")); Test. Result result= suite. run();

How do you run your tests and collect their results? Use a Test Runner

How do you run your tests and collect their results? Use a Test Runner An Example public static Test suite() { Test. Suite suite= new Test. Suite(); suite. add. Test(new Money. Test("test. Money. Equals")); suite. add. Test(new Money. Test("test. Simple. Add")); return suite; }

JUnit provides both a graphical and a textual version of a Test. Runner tool.

JUnit provides both a graphical and a textual version of a Test. Runner tool. Start it by typing java junit. awtui. Test. Runner Or junit. swingui. Test. Runner.

References • junit. org - a site for software developers using JUnit.

References • junit. org - a site for software developers using JUnit.