jUnit - Copy

Embed Size (px)

Citation preview

  • 8/7/2019 jUnit - Copy

    1/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Unit Testing Object-Oriented SoftwareWith JUnit, JMock, and the Java Reflection API

    CSCE361: Software Engineering

    University of NebraskaLincoln

    September 15, 2010

    Slide 1/30

  • 8/7/2019 jUnit - Copy

    2/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Outline

    1 Motivation

    2

    Design

    3 Implementation

    4 Demo

    Slide 2/30

  • 8/7/2019 jUnit - Copy

    3/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    From Last Week

    Verification techniques can be static, dynamic, or mixed.

    Testing, an example of dynamic verification, exercisessystem behaviors and evaluates the results.

    Among the various types of testing, unit testing usuallycomes first in the software life cycle.

    Slide 3/30

  • 8/7/2019 jUnit - Copy

    4/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Unit Testing

    Unit testing focuses on small pieces of code in isolation,usually individual classes.

    Slide 4/30

  • 8/7/2019 jUnit - Copy

    5/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Properties of the Ideal Test Suite

    SoundnessWe claim that the system is buggy only

    when it really is.CompletenessWe claim that the system is correctonly when it really is.

    Slide 5/30

  • 8/7/2019 jUnit - Copy

    6/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Advantages of Unit Testing

    Soundness: Its easier to determine the oracle for a

    smaller piece of code.Completeness: We can easily test methods that aredifficult to reach in the system context.

    Slide 6/30

  • 8/7/2019 jUnit - Copy

    7/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Disadvantages of Unit Testing

    Soundness: We may uncover faults that can never be

    exposed at the system level.Completeness: We will miss bugs in the way units arecombined.

    Slide 7/30

  • 8/7/2019 jUnit - Copy

    8/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Properties of the Ideal Test Suite (cont.)

    TraceabilityGiven a failure, it takes little effort to findthe underlying fault.

    FeasibilityThe cost to implement and use the testsuite is minimal.

    MaintainabilityWhen the system changes, test suiteupdates arent overly costly.

    Slide 8/30

  • 8/7/2019 jUnit - Copy

    9/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Advantages of Unit Testing (cont.)

    Traceability: If we find a failure, we know that the faultis within the unit.

    Feasibility: We can run the tests early, before the entiresystem is assembled.

    Maintainability: If a unit is unchanged, its tests survive.

    Slide 9/30

  • 8/7/2019 jUnit - Copy

    10/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Disadvantages of Unit Testing (cont.)

    Feasibility: Each test only exercises a few behaviors, sowe must write many.

    Maintainability: The test cases must change wheneverthe implementation changes, even if the systembehavior is unaffected.

    Slide 10/30

  • 8/7/2019 jUnit - Copy

    11/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Outline

    1 Motivation

    2 Design

    3 Implementation

    4 Demo

    Slide 11/30

  • 8/7/2019 jUnit - Copy

    12/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Overview of Unit Testing

    1 Isolate the unit

    2 Setup the inputs

    3 Execute the unit

    4 Verify the outputs

    5 Tear-down

    Slide 12/30

  • 8/7/2019 jUnit - Copy

    13/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Isolation

    Problem: Units might depend on others.

    Solution: Usually, we mock-up the other units: wereplace the real objects with fake ones and script theirbehavior.

    Slide 13/30

    I l

  • 8/7/2019 jUnit - Copy

    14/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Isolation

    Problem: Units might depend on others.

    Solution: Usually, we mock-up the other units: wereplace the real objects with fake ones and script theirbehavior.

    Slide 14/30

    I S d T d

  • 8/7/2019 jUnit - Copy

    15/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Input Setup and Tear-down

    Problem: Test cases often share setup and tear-down

    procedures.Solution: We group common code into fixtures.

    Slide 15/30

    U i T iI S d T d

  • 8/7/2019 jUnit - Copy

    16/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Input Setup and Tear-down

    Problem: Test cases often share setup and tear-down

    procedures.Solution: We group common setup and tear-down codeinto fixtures.

    Slide 16/30

    U it T tiE ti d V ifi ti

  • 8/7/2019 jUnit - Copy

    17/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Execution and Verification

    Problem: The test case cannot call methods withaccess restrictions (private, protected, etc.). Likewise,

    the results of a test might include nonpublic fields.

    Solution: We change the method and field visibility fortesting only.

    Slide 17/30

    Unit TestingE ti d V ifi ti

  • 8/7/2019 jUnit - Copy

    18/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Execution and Verification

    Problem: The test case cannot call methods withaccess restrictions (private, protected, etc.). Likewise,

    the results of a test might include nonpublic fields.

    Solution: We change the method and field visibility fortesting only.

    Slide 18/30

    Unit TestingOutline

  • 8/7/2019 jUnit - Copy

    19/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Outline

    1 Motivation

    2 Design

    3 Implementation

    4 Demo

    Slide 19/30

    Unit TestingOverview of Unit Testing

  • 8/7/2019 jUnit - Copy

    20/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Overview of Unit Testing

    1 Isolate the unit

    2 Setup the inputs

    3 Execute the unit

    4 Verify the outputs

    5 Tear-down

    Slide 20/30

    Unit TestingPreliminaries

  • 8/7/2019 jUnit - Copy

    21/30

    Unit TestingObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Preliminaries

    1 import java.lang.reflect.*;2 import org.junit.*;

    3 import org.junit.runner.*;

    4 import org.jmock.*;

    5 import org.jmock.integration.junit4.*;

    7 import static org.junit.Assert.*;

    9 @RunWith(JMock.class)

    10 public class ExampleTester{

    11 //The unit under test

    12 protected ExampleUnit exampleUnit;13 //...

    14 }

    Slide 21/30

    Unit TestingIsolation

  • 8/7/2019 jUnit - Copy

    22/30

    U gObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Isolation

    1 Mockery mockery=new JUnit4Mockery();

    2 OtherUnit otherUnit=mockery.mock(OtherUnit.class);

    3 mockery.checking(new Expectations(){{

    4 //first, expect one invocation of foo()

    5

    oneOf(otherUnit).foo();6 //have it return 1123

    7 will(returnValue(1123));

    8 //then, expect one invocation of bar(9)

    9 oneOf(otherUnit).bar(9);

    10 //...

    11 }});

    Slide 22/30

    Unit TestingInput Setup

  • 8/7/2019 jUnit - Copy

    23/30

    gObject-Oriented

    Software

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Input Setup

    1 protected Input input;

    3 @Before

    4 public void setup(){

    5 //(mockery code goes here...)

    6 //create the unit under test7 exampleUnit=new ExampleUnit(otherUnit);

    8 //create input values

    9 input=new Input();

    10 input.setBaz(-106);

    11 input.setQuux(83);12 }

    Slide 23/30

    Unit TestingExecution

  • 8/7/2019 jUnit - Copy

    24/30

    Object-OrientedSoftware

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Execution

    1 @Test

    2 public void testXyzzy(){

    3 Output result=exampleUnit.xyzzy(input);4 //...

    5 }

    Slide 24/30

    Unit TestingExecution (alternative)

  • 8/7/2019 jUnit - Copy

    25/30

    Object-OrientedSoftware

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Execution (alternative)

    1 @Test

    2 public void testXyzzy() throws Exception{

    3 Method xyzzy=exampleUnit.getClass().

    getDeclaredMethod("xyzzy",Input.class);4 xyzzy.setAccessible(true);

    5 Output result=xyzzy.invoke(exampleUnit,input);

    6 //...

    7 }

    Slide 25/30

    Unit TestingObj O i dVerification

  • 8/7/2019 jUnit - Copy

    26/30

    Object-OrientedSoftware

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Verification

    1 @Test

    2 public void testXyzzy(){

    3 //...

    4 assertEquals("Xyzzy should always return anOutput of length 2",2,result.getSize());

    5 assertNotNull("The plugh field of xyzzys result

    should never be null",result.getPlugh());

    6 }

    Slide 26/30

    Unit TestingObj t O i t dTear-down

  • 8/7/2019 jUnit - Copy

    27/30

    Object-OrientedSoftware

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Tear down

    1 @After

    2 public void teardown() throws IOException{

    3 //free outstanding resources4 input.close();

    5 }

    Slide 27/30

    Unit TestingObject OrientedOutline

  • 8/7/2019 jUnit - Copy

    28/30

    Object-OrientedSoftware

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    Outline

    1 Motivation

    2 Design

    3 Implementation

    4 Demo

    Slide 28/30

    Unit TestingObject OrientedFor Next Time

  • 8/7/2019 jUnit - Copy

    29/30

    Object-OrientedSoftware

    CSCE361

    Motivation

    Design

    Implementation

    DemoWhich units should we test?

    What test cases should we use?When are we done testing?

    Slide 29/30

    Unit TestingObject-OrientedJump to Slide (130)

  • 8/7/2019 jUnit - Copy

    30/30

    Object-OrientedSoftware

    CSCE361

    Motivation

    Design

    Implementation

    Demo

    p ( )

    Slide 1 Slide 2 Slide 3 Slide 4

    Slide 5 Slide 6 Slide 7 Slide 8

    Slide 9 Slide 10 Slide 11 Slide 12

    Slide 13 Slide 14 Slide 15 Slide 16

    Slide 17 Slide 18 Slide 19 Slide 20

    Slide 21 Slide 22 Slide 23 Slide 24

    Slide 25 Slide 26 Slide 27 Slide 28

    Slide 29 Slide 30

    Slide 30/30