DOC PREVIEW
Penn CIT 597 - JUnit Revisited

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 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 18 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 18 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 18 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 18 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 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

JUnit, RevisitedJUnitWhat JUnit doesTest classes in BlueJCreating a test class in BlueJCreating the setUp() methodImplementing the tearDown() methodRecording test casesThe structure of a test methodassertX methodsExample: Counter classJUnit tests for CounterThe Counter class itselfViewing test resultsProblems with unit testingThe GUI problemThe printing problemThe EndJan 14, 2019JUnit, Revisited2JUnitJUnit is a framework for writing unit testsA unit test is a test of a single classA test case is a single test of a single methodA test suite is a collection of test casesUnit testing is particularly important when software requirements change frequentlyCode often has to be refactored to incorporate the changesUnit testing helps ensure that the refactored code continues to workJUnit helps the programmer:define and execute tests and test suitesformalize requirements and clarify architecturewrite and debug codeintegrate code and always be ready to release a working version3What JUnit doesJUnit runs a suite of tests and reports resultsFor each test in the test suite:JUnit calls setUp()This method should create any objects you may need for testingJUnit calls one test methodThe test method may comprise multiple test cases; that is, it may make multiple calls to the method you are testingIn fact, since it’s your code, the test method can do anything you wantThe setUp() method ensures you entered the test method with a virgin set of objects; what you do with them is up to youJUnit calls tearDown()This method should remove any objects you created4Test classes in BlueJThe class tobe testedThe classto test itThe menu youget when youright-click thetest classUse theseto createtestsUse theseto run tests5Creating a test class in BlueJIf you have an existing class, right-click on it and choose Create Test ClassIf your class is named MyClass, the new test class will be named MyClassTestTo create the test class first, just choose New Class... and give the test class the name of a future class, with ‘Test’ appendedLater, after you create the class to be tested,you can right-click it and choose Create Test ClassBlueJ will complain that the class already exists, but it will also correctly associate the test class with the class to be tested6Creating the setUp() methodBlueJ has an Object Bench (at the bottom of the main window)You can create objects on the Object Bench by right-clicking on a class and choosing one of its new constructorsYou can right-click on a test class and choose:Object Bench To Test Fixture orTest Fixture To Object BenchSince setUp() is your code, you can modify it any way you like (such as creating new objects in it)7Implementing the tearDown() methodIn most cases, the tearDown() method doesn’t need to do anythingThe next time you run setUp(), your objects will be replaced, and the old objects will be available for garbage collectionIt doesn’t hurt to set to null the objects you created in setUp()Like the finally clause in a try-catch-finally statement, tearDown() is where you would release system resources (such as streams) that might not otherwise be released8Recording test casesAn easy way to create a test method is to right-click a compiled test class and choose Create Test Method...Enter the name of the method you want to test; you don’t have to say “test”BlueJ will capitalize your method name and prefix it with testIf you wish, you can copy Test Fixture To Object BenchUse BlueJ to make calls to the methodAfter each call, BlueJ will tell you its result, and ask you to type in the result you expectedThe result can be equal to, the same as, not the same as, null, not null, or equal to (double or float)You can even create a new object to use as a resultWhen you are done click the End button (under Recording)Review the results!This is a new feature in BlueJ, and sometimes it produces bad syntaxA comma in your expected result will confuse BlueJ 1.3.09The structure of a test methodA test method doesn’t return a resultIf the tests run correctly, a test method does nothingIf a test fails, it throws an AssertionFailedErrorHence, a test method just calls some assertion method, any of which may throw an AssertionFailedErrorThe JUnit framework catches the error and deals with it; you don’t have to do anything10assertX methodsstatic void assertTrue(boolean test)static void assertFalse(boolean te st)assertEquals(expected, actual )assertSame(Object,expected, Object,actual)assertNotSame(Object,expected, Object,actual)assertNull(Object,object)assertNotNull(Object,obje ct) fail()All the above may take an optional String message as the first argument, for example,static void assertTrue(String message, boolean test)11Example: 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We write the test methods before we write the codeThis has the advantages described earlierDepending on the JUnit tool we use, we may have to create the class first, and we may have to populate it with stubs (methods with empty bodies)Don’t be alarmed if, in this simple example, the JUnit tests are more code than the class itself12JUnit tests for Counter public class CounterTest extends junit.framework.TestCase { Counter counter1; public CounterTest() { } // default constructor protected void setUp() { // creates a (simple) test fixture counter1 = new Counter(); } protected void tearDown() { } // no resources to release public void testIncrement() { assertTrue(counter1.increment() == 1); assertTrue(counter1.increment() == 2); } public void testDecrement() { assertTrue(counter1.decrement() == -1); }}Note that each test begins with a brand new counterThis means you don’t have to worry about the order in which the tests are run13The Counter class itselfpublic class Counter {int count = 0;public int increment() { return ++count;}public int decrement() { return --count;} public int getCount() { return count; }}Is JUnit testing overkill for this little class?The Extreme Programming


View Full Document

Penn CIT 597 - JUnit Revisited

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download JUnit Revisited
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 Revisited 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 Revisited 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?