Unit Testing using JUnit-Software Engineering-Lab Manual, Lecture notes of Software Engineering

Software engineering is about the development and application of processes and tools for managing the complexities inherent in creating high quality software systems. It introduces the fundamental software engineering concepts and terminology. This lab manual includes: Unit, Testing, Framework, Parts, Driven, Development, COnventions, Robust, Money, Currency, Classes, Bank, Account

Typology: Lecture notes

2011/2012

Uploaded on 08/09/2012

parthivi
parthivi 🇮🇳

4.1

(8)

85 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Unit Testing using JUnit
What is JUnit?
JUnit is a framework for testing parts, or units of your code. In general, these units are
considered to be the methods of each class. JUnit can help you to make sure that each of your
classes work as expected. In unit testing you will usually write one test class for each of the
classes that you want to test. Your test class will often include a test method for each method
implemented by the class being tested. But keep in mind that this is not always feasible, or
necessary. A test case will, and should often touch on more than a single method. These tests
should be written to make it likely that when all the tests pass, the code is functioning as
required.
Unit testing is very common when you are doing test driven development (TDD). TDD is a
methodology in which you write the test code first, and then write the code you wish to test.
With JUnit, if you know what classes and methods needs to be in your system, and what their
functionality should be, you can start by writing a set of test cases first, and then fill in the body
of your methods to pass those test cases later. And when you later need to change your code,
the test cases can be a great help in making sure that your code stays robust and relatively bug
free.
Basic Information
Before you begin, we would like to call your attention to the following conventions:
A Test Case Class is named [classname]Test.java, where classname is the name of the class that
is tested.
A Test Case Method is a method of the Test Case Class which is used to test one or more of the
methods of the target class. Test Case Methods are are annotated with @Test to indicate them
to JUnit. Cases without @Test will not be noticed by JUnit.
JUnit assertions are used to assert that a condition must be true at some point in the test
method. JUnit has many types of assertions. The following is a selection of the most commonly
used assertions:
assertEquals(expected, actual): Assert that expected value is equal to the actual value. The
expected and actual value can be of any type, for example integer, double, byte, string, char or
any Java object. If the expected and actual values are of type double or oat, you should add a
third parameter indicating the delta. It represents the maximum difierence between expected
and actual value for which both numbers are still considered equal.
assertTrue(condition): Asserts that the Boolean condition is True.
assertFalse(condition): Asserts that the Boolean condition is False.
assertNull(object): Asserts that an object is null.
docsity.com
pf3

Partial preview of the text

Download Unit Testing using JUnit-Software Engineering-Lab Manual and more Lecture notes Software Engineering in PDF only on Docsity!

Unit Testing using JUnit

What is JUnit?

JUnit is a framework for testing parts, or units of your code. In general, these units are considered to be the methods of each class. JUnit can help you to make sure that each of your classes work as expected. In unit testing you will usually write one test class for each of the classes that you want to test. Your test class will often include a test method for each method implemented by the class being tested. But keep in mind that this is not always feasible, or necessary. A test case will, and should often touch on more than a single method. These tests should be written to make it likely that when all the tests pass, the code is functioning as required.

Unit testing is very common when you are doing test driven development (TDD). TDD is a methodology in which you write the test code first, and then write the code you wish to test. With JUnit, if you know what classes and methods needs to be in your system, and what their functionality should be, you can start by writing a set of test cases first, and then fill in the body of your methods to pass those test cases later. And when you later need to change your code, the test cases can be a great help in making sure that your code stays robust and relatively bug‐ free.

Basic Information

Before you begin, we would like to call your attention to the following conventions: A Test Case Class is named [classname]Test.java, where classname is the name of the class that is tested. A Test Case Method is a method of the Test Case Class which is used to test one or more of the methods of the target class. Test Case Methods are are annotated with @Test to indicate them to JUnit. Cases without @Test will not be noticed by JUnit.

JUnit assertions are used to assert that a condition must be true at some point in the test method. JUnit has many types of assertions. The following is a selection of the most commonly used assertions: assertEquals(expected, actual): Assert that expected value is equal to the actual value. The expected and actual value can be of any type, for example integer, double, byte, string, char or any Java object. If the expected and actual values are of type double or oat, you should add a third parameter indicating the delta. It represents the maximum difierence between expected and actual value for which both numbers are still considered equal. assertTrue(condition): Asserts that the Boolean condition is True. assertFalse(condition): Asserts that the Boolean condition is False. assertNull(object): Asserts that an object is null.

assertNotNull(object): Asserts that an object is not null. assertSame(expected object, actual object): Asserts that two variables refer to the same object. assertNotSame(expected object, actual object): Asserts that two variables do not refer to the same object.

Whenever an assertion fails, an AssertionError is thrown, which is caught by the JUnit framework and presented as a red bar, indicating test failure. Assert statements accept an extra message parameter before the other parameters. This parameter is a String which will be displayed if the assertion fails. Exceptions will also be caught by JUnit, and cause the test to fail.

Assignment Tasks

Money and Currency Classes You have been given a template for the Currency and Money classes, with JavaDoc comments explaining what each method should do. There is also a Bank and Account class, but we will come back to that in the following section. All the methods of Currency.java and Money.java are empty. First, write test cases for the methods of each class, and then fill in the methods with code that will make your test cases pass. The template test methods are a guideline you can use for constructing your tests. Unless you have a good idea for how to otherwise structure the tests, you should simply fill in those template test methods. Bear in mind that the teaching assistants have their own test cases for these methods, so write some good tests yourself, to make it likely that your code will pass our tests as well. Important note: The decimal numbers representing money are implemented as integers. The last two digits denote two decimals. So Money(200050, SEK) will mean 2000.50 SEK.

Bank The Bank and Account classes were written by a bad programmer. When you are confident you’re Money and Currency classes work as intended, write test cases for the Bank and Account classes and find the bugs. Again, the specification is provided in the JavaDoc comments.