JUnitTest suitesExample: Old way vs. new wayXP approach to testingSlide 5TerminologyOnce more, in picturesWriting a JUnit test class, IWriting a JUnit test class, IIA simple exampleAssert methods IExample: Counter classJUnit tests for CounterThe Counter class itselfWarning: equalsAssert methods IIAssert methods IIIWriting a JUnit test class, IIISpecial features of @TestTest-Driven Development (TDD)StubsIgnoring a testSlide 23JUnit in EclipseViewing results in EclipseRecommended approachThe EndJan 14, 2019JUnit2Test suitesObviously you have to test your code to get it working in the first placeYou can do ad hoc testing (testing whatever occurs to you at the moment), orYou can build a test suite (a thorough set of tests that can be run at any time)Disadvantages of writing a test suiteIt’s a lot of extra programmingTrue—but use of a good test framework can help quite a bitYou don’t have time to do all that extra workFalse—Experiments repeatedly show that test suites reduce debugging time more than the amount spent building the test suiteAdvantages of having a test suiteYour program will have many fewer bugsIt will be a lot easier to maintain and modify your programThis is a huge win for programs that, unlike class assignments, get actual use!3Example: Old way vs. new wayint max(int a, int b) { if (a > b) { return a; } else { return b; }}void testMax() { int x = max(3, 7); if (x != 7) { System.out.println("max(3, 7) gives " + x); } x = max(3, -7); if (x != 3) { System.out.println("max(3, -7) gives " + x); }}public static void main(String[] args) { new MyClass().testMax();}@Testvoid testMax() { assertEquals(7, max(3, 7)); assertEquals(3, max(3, -7));}4XP approach to testingIn the Extreme Programming approach,Tests are written before the code itselfIf code has no automated test case, it is assumed not to workA test framework is used so that automated testing can be done after every small change to the codeThis may be as often as every 5 or 10 minutesIf a bug is found after development, a test is created to keep the bug from coming backConsequencesFewer bugsMore maintainable codeContinuous integration—During development, the program always works—it may not do everything required, but what it does, it does right5JUnitJUnit is a framework for writing testsJUnit was written by Erich Gamma (of Design Patterns fame) and Kent Beck (creator of XP methodology)JUnit uses Java’s reflection capabilities (Java programs can examine their own code)JUnit helps the programmer:define and execute tests and test suitesformalize requirements and clarify architecturewrite and debug codeintegrate code and always be ready to release a working versionJUnit is not included in Sun’s SDK, but almost all IDEs include it6TerminologyA test fixture sets up the data (both objects and primitives) that are needed to run testsExample: If you are testing code that updates an employee record, you need an employee record to test it onA unit test is a test of a single classA test case tests the response of a single method to a particular set of inputsA test suite is a collection of test casesA test runner is software that runs tests and reports resultsAn integration test is a test of how well classes work togetherJUnit provides some limited support for integration tests7test suiteOnce more, in picturesA unit test tests the methods in a single classA test case tests (insofar as possible) a single methodYou can have multiple test cases for a single methodA test suite combines unit testsThe test fixture provides software support for all thisThe test runner runs unit tests or an entire test suiteIntegration testing (testing that it all works together) is not well supported by JUnitunit test (for one class)another unit testtest case (for one method)another test casetest case (for one method)another unit testanother test caseanother test caseanother test casetest fixturetest runneranother test case8Writing a JUnit test class, IStart by importing these JUnit 4 classes:import org.junit.*;import static org.junit.Assert.*; // note static importDeclare your test class in the usual waypublic class MyProgramTest {Declare an instance of the class being testedYou can declare other variables, but don’t give them initial values herepublic class MyProgramTest { MyProgram program; int someVariable;9Writing a JUnit test class, IIDefine a method (or several methods) to be executed before each testInitialize your variables in this method, so that each test starts with a fresh set of values@Beforepublic void setUp() { program = new MyProgram(); someVariable = 1000;}You can define one or more methods to be executed after each testTypically such methods release resources, such as filesUsually there is no need to bother with this method@Afterpublic void tearDown() {}10A simple exampleSuppose you have a class Arithmetic with methods int multiply(int x, int y), and boolean isPositive(int x)import org.junit.*;import static org.junit.Assert.*; public class ArithmeticTest {}@Testpublic void testMultiply() { assertEquals(4, Arithmetic.multiply(2, 2)); assertEquals(-15, Arithmetic.multiply(3, -5));}@Testpublic void testIsPositive() { assertTrue(Arithmetic.isPositive(5)); assertFalse(Arithmetic.isPositive(-5)); assertFalse(Arithmetic.isPositive(0));}11Assert methods IWithin a test,Call the method being tested and get the actual resultAssert what the correct result should be with one of the assert methodsThese steps can be repeated as many times as necessaryAn assert method is a JUnit method that performs a test, and throws an AssertionError if the test failsJUnit catches these Errors and shows you the resultstatic void assertTrue(boolean test)static void assertTrue(String message, boolean test)Throws an AssertionErrorif the test failsThe optional message is included in the Errorstatic void assertFalse(boolean test)static void assertFalse(String message, boolean test)Throws an AssertionErrorif the test fails12Example: Counter classFor the sake of example, we will create and test a trivial “counter” classThe constructor will create a counter and set it to zeroThe increment method will add one to the
View Full Document