Exercise Sheet 4: November 13Th: Junit

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

HIS POS

Wintersemester 2014/2015
Fachbereich 2

Exercise Sheet 4
Prof. Dr. Jrg Schfer

Exercise Sheet 4
November 13th: JUnit
Note: You can use JUnit (V <= 3.8) for exercise 2 and 3 or (better) my source code of the core
classes for all exercises of this sheet!
Exercise 1
Fill the gaps in my source code of the core classes of JUnit (JUnit.zip).
Exercise 2
a) Extend JUnit with Conditional Test Cases:
1. Implement a ConditionalTestCase extending TestCase to support a public boolean
shouldRun() method. Subclasses can override the default and henceforth developers can
ex- or include certain test cases depending on context (i.e. for different platforms etc.)
2. What Design Patterns are in action here?
b) Extend JUnit with Performance Measurement:
1. Add code to ConditionalTestCase to automatically measure execution time of test methods!
2. Where would you put the results?
c) Extend JUnit with Performance Testing:
1. Add code to ConditionalTestCase to automatically test execution time of test methods!
If execution takes too long, throw an AssertionFailedError with a proper message!
2. Which Design Pattern are you using?
Exercise 3
a) Build a TestSuite tree by executing code like:
public static
TestSuite
TestSuite
TestSuite

void main(String[] args) {


suite1 = new TestSuite(MyTestCase.class);
suite2 = new TestSuite(MyTestCase.class);
suite3 = new TestSuite(MyTestCase.class);

// Dont uncomment next line ;-)


// suite1.addTest(suite1);
suite1.addTest(suite2);
suite2.addTest(suite3);
Test test = new MyTestCase("testMethod");
suite1.addTest(test);
test = new MyTestCase("testMethod");
suite2.addTest(test);
test = new MyTestCase();
//this fails Nullpointer Execption!
//suite3.addTest(test);

1/2

HIS POS
Wintersemester 2014/2015
Fachbereich 2

Exercise Sheet 4
Prof. Dr. Jrg Schfer

junit.textui.TestRunner.run(suite1);
}
b)

1. What happens if TestSuite adds itself?


2. What can one do to make code safer?
3. Is this a bug?

c)

1. What happens if you execute code like suite3.addTest(test);?


2. Is this a bug?
3. What is the root cause for this behavior?

Hints
Consult the literature!
You can work in pairs, if you want!
If you want to learn a Java API, look into the java docs!
Always use the same familiar IDE (suggestion Eclipse)!

2/2

You might also like