DOC PREVIEW
Penn CIT 591 - JUnit

This preview shows page 1-2-22-23 out of 23 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 23 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

JUnitTerminologyOnce 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 testTest suitesJUnit in EclipseViewing results in EclipseRecommended approachThe EndJan 14, 2019JUnit2TerminologyA test fixture sets up the data (both objects and primitives) that are needed to run testsExample: If you are testing code that updates an employee record, you need an employee record to test it onA unit test is a test of a single classA test case tests the response of a single method to a particular set of inputsA test suite is a collection of test casesA test runner is software that runs tests and reports resultsAn integration test is a test of how well classes work togetherJUnit provides some limited support for integration tests3test suiteOnce more, in picturesA unit test tests the methods in a single classA test case tests (insofar as possible) a single methodYou can have multiple test cases for a single methodA test suite combines unit testsThe test fixture provides software support for all thisThe test runner runs unit tests or an entire test suiteIntegration 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 case4Writing a JUnit test class, IStart by importing these JUnit 4 classes:import org.junit.*;import static org.junit.Assert.*; // note static importDeclare your test class in the usual waypublic class MyProgramTest {Declare an instance of the class being testedYou can declare other variables, but don’t give them initial values herepublic class MyProgramTest { MyProgram program; int someVariable;5Writing a JUnit test class, IIDefine a method (or several methods) to be executed before each testInitialize 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 testTypically such methods release resources, such as filesUsually there is no need to bother with this method@Afterpublic void tearDown() {}6A simple exampleSuppose 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));}7Assert methods IWithin a test,Call the method being tested and get the actual resultAssert what the correct result should be with one of the assert methodsThese steps can be repeated as many times as necessaryAn assert method is a JUnit method that performs a test, and throws an AssertionError if the test failsJUnit catches these Errors and shows you the resultstatic void assertTrue(boolean test)static void assertTrue(String message, boolean test)Throws an AssertionErrorif the test failsThe optional message is included in the Errorstatic void assertFalse(boolean test)static void assertFalse(String message, boolean test)Throws an AssertionErrorif the test fails8Example: Counter classFor the sake of example, we will create and test a trivial “counter” classThe constructor will create a counter and set it to zeroThe increment method will add one to the counter and return the new valueThe decrement method will subtract one from the counter and return the new valueIn this simple example, the JUnit tests are more code than the class itselfThis isn’t usually the case for “real” classes and methods9JUnit tests for Counter public class CounterTest { Counter counter1; // declare a Counter here @Before void setUp() { counter1 = new Counter(); // initialize the Counter here } @Test public void testIncrement() { assertTrue(counter1.increment() == 1); assertTrue(counter1.increment() == 2); } @Test public void testDecrement() { assertTrue(counter1.decrement() == -1); }}Note that each test begins with a brand new counter This means you don’t have to worry about the order in which the tests are run10The Counter class itselfpublic class Counter {int count = 0;public int increment() { return count += 1;}public int decrement() { return count -= 1;} public int getCount() { return count; }}Is JUnit testing overkill for this little class?The Extreme Programming view is: If it isn’t tested, it doesn’t workYou are not likely to have many classes this trivial in a real program, so writing JUnit tests for those few trivial classes is no big dealOften even XP programmers don’t bother writing tests for simple getter methods such as getCount()We only used assertTrue in this example, but there are additional assert methods11Warning: equalsYou can compare primitives with ==Java has a method x.equals(y), for comparing objectsThis method works great for Strings and a few other Java classesFor objects of classes that you create, you have to define equalsassertEquals(expected, actual) uses == or equalsTo define equals for your own objects, define exactly this method:public boolean equals(Object obj) { ... }The argument must be of type Object, which isn’t what you want, so you must cast it to the correct type (say, Person):public boolean equals(Object something) { Person p = (Person)something; return this.name == p.name; // test whatever you like here}We’ll talk much more about equals later12Assert methods IIassertEquals(expected, actual)assertEquals(String message, expected, actual)expected and actual must be both objects or the same primitive typeFor objects, uses your equals method, if you have defined it properly, as described on the previous


View Full Document

Penn CIT 591 - JUnit

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download JUnit
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view JUnit and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view JUnit 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?