JUnit Training - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

JUnit Training

Description:

Verify the correctness of our programs. Document our programs ... Tautology, sure, but it's the way it works. Failure is an option. Default is to pass. ... – PowerPoint PPT presentation

Number of Views:228
Avg rating:3.0/5.0
Slides: 43
Provided by: AndrewWil8
Category:

less

Transcript and Presenter's Notes

Title: JUnit Training


1
JUnit Training
  • Technology Planning

2
Why are we here?
  • Understand what JUnit is
  • Some concrete examples

3
Who are we?
  • Programmers who want to
  • Verify the correctness of our programs
  • Document our programs
  • Support future modifications / refactoring by
    providing Regression Tests
  • Use others tests
  • Or just have another tool in the tool chest

4
Ground Rules
  • Doubts, objections, concerns deferred until end
    of presentation

Keep an open mind!
5
Martin Fowler says of JUnit
  • Never in the field of software development was
    so much owed by so many to so few lines of code.

Ribbit.
6
Zeroth Exercise
  • Open Eclipse
  • Find any class with Test in its name in the
    Package Explorer (left pane).
  • Right-click, Run ? JUnit Test.
  • Congratulations, you have run a JUnit test.

7
Testing
  • Functional testing Poke it. Does it work?
  • Integration testing Plays well with others.
  • Black box testing Testing the API the object
    exposes.
  • White box testing Testing the implementations
    internals.

8
Unit testing
  • Black box testing for small components (objects)

9
Aside testing an XSLT
  • A story about Stylus Studio, an XSLT, an XML, and
    a YaleInfo Channel

10
JUnit testing
  • Unit testing for Java
  • Formal, programmatic, executable tests
  • Unambiguous results

11
Antipattern Looks right
  • Well, it looks pretty good to me.

12
Antipattern Guru verification
  • I dont know if this is right, ask the Guru.

13
A test case
  • Extends junit.framework.TestCase
  • Named FooBarTest
  • Has test methods (testSplat())
  • setUp() and tearDown() methods

14
JUnit, the Framework
  • Remember the Hollywood Principle?
  • Use Reflection to find the tests
  • Use Reflection to invoke each of the testSplat()
    methods of the test
  • Call setUp() before and tearDown() after each
    test.

15
How to test
  • Make assertions.
  • assertTrue(predicate), assertFalse(predicate),
    assertEquals(expected, actual)
  • You can explicitly call fail()
  • All of these calls can take a String first
    parameter providing a message to print when the
    assertion fails.

16
You succeed unless you fail.
  • Tautology, sure, but its the way it works.

17
Failure is an option
  • Default is to pass.
  • Tests fail by throwing any kind of Throwable.
  • fail() and assertSplat() all throw
    AssertionFailedError.

18
First exercise
  • Add the junitTraining project to your Eclipse 3
    workspace.
  • Run NumberToolTest. Watch it fail.
  • Finish implementing NumberToolTest. It will
    still fail.
  • Implement NumberTool. Run your test.

19
NumberTool
  • Methods
  • isEven(int numer)
  • isOdd(int number)
  • Throw IllegalArgumentException for negative
    arguments.

20
NumberToolTest
  • Needs to test isEven() and isOdd() for all
    behaviors true, false, and exception throw.
  • Test stubs there for you.
  • Please dont change method names

21
Going for the Green
  • No, not a new FA initiative.
  • The fabled Green Bar

Green is good.
22
NumberToolTestTest
  • Tests your testcases
  • Dont try this at home?

23
JUnit as Language
  • Formal test cases as
  • A communication tool
  • A characterization of a bug
  • A characterization of a feature
  • A specification of an object

24
uPortal PropertiesManager
  • A story about JUnit

25
Rod Johnson Says
  • Testing should occur throughout the development
    lifecycle. Testing should never be an
    afterthought. Integrating testing into the
    development process brings many benefits.

26
Aside Test Driven Development
  • What is Test Driven Development?
  • How Eclipse / JUnit enables TDD.

27
Optional Exercise Counter
  • An exercise in Test Driven Development.
  • Counter.txt describes the Counter.
  • Write its Test First. Only create the Counter
    class after you first invoke it. Only create its
    methods after you invoke them.

28
Lets pretend
  • Creating a Counter is expensive.
  • Can we get away with instantiating only one of
    them to serve our entire testcase?

29
Side Effects and Transactions
  • JUnit wants you to write atomic, transactional,
    independent test methods.
  • It will call setUp() before invoking each test,
    and tearDown() afterwards.

30
Test Suites
  • Generate a Test Suite
  • Right-click the number package. New? Other
    ? Test Suite.
  • Select both NumberToolTest and CounterTest to be
    included in suite.
  • Run the suite. Sweet!

31
JUnit / Ant Integration
  • See portalCasClient distribution.
  • http//pantheon.yale.edu/awp9/casrelease/portalCa
    sClient/report/html/index.html

32
MockObjects
  • MockObjects take the place of dependencies.
  • Mock Servlet API components, mock JDBC
    components, mock whatever.

Are you mocking me?
33
Exercise a Filter
  • A filter to require the length of the proxy chain
    to be at most 1.
  • Demonstrates using Mock Objects

34
Aside From Whence JUnit?
  • Kent Beck and Erich Gamma

35
Youre not pushing that XP stuff, are you?
  • Extreme Programming and JUnit
  • Test Driven Development
  • Empowering Refactoring

36
HttpUnit
  • Utilities for testing HTTP activities (gets,
    posts, forms, etc.)

37
XMLUnit
  • Utilities for processing / making assertions
    about XML.
  • XMLAssert.assertXMLEqual()

38
Cactus
  • Testing web applications is prickly
  • Cactus is a tool for putting test framework both
    server-side and client-side

39
Some closing thoughts
  • About the economics of software development
  • Automated tests are a bet.
  • The bet is the cost of writing the test will be
    recovered in the value of the code being more
    correct sooner (and staying that way).

40
When is this true?
  • When the value of the code being correct is high
    framework, utility code. The uPortal
    PropertiesManager. The CASClient (alpha!).
  • When the cost of producing the test is low. We
    get good at it. Eclipse helps us produce the
    test. Tools like XMLUnit and MockObjects make
    test composing easier.

41
DDJ Start Testing Today
  • Jan 2004 article by a Hewlett-Packard developer
  • The simple testing practices I've described
    would instantly improve any software development
    process.

42
Why Test?
  • Instant gratification
  • Better design testability requires modularity
  • Demonstrates how to use the code
  • Enables safer refactoring
Write a Comment
User Comments (0)
About PowerShow.com