|
CS 580 Client-Server Programming
Fall Semester, 2000
Testing Hidden Methods
|
|
|
Previous   
Lecture Notes Index
   Next    
© 2000, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 04-Oct-00
|
|
Contents of Doc 11, Testing Hidden Methods
References
XP
mailing list
Inner
Class lecture notes at:
http://www.eli.sdsu.edu/courses/fall98/cs596/notes/nested/nested.html
How
NASA Navigates Space Craft
- Send
the craft in the general direction of the target
- As
you get closer to the target, adjust the craft's direction
- Repeat
2 until you reach the target
Testing
and Hidden Methods/State
Issues:
- How
does one test hidden methods?
- Direct
access to an object's state can reduce the time needed to write a test
Testing
Hidden Methods One Position Don't Do it
Pro:
- Can
not test everything
- Clients
of an object only care if public interface works correctly
- Testing
public interface also tests hidden methods
- Hidden
methods are more likely to change, requiring changes to the tests
Con:
- The
closer the test is to the code it tests the easier the test
- Bugs
in hidden methods can make it hard to debug public methods
How
to Test Hidden Methods Directly?
Method
1: Relax the protection level
In
Java one can
- Make
the method package level access
- Place
the test class in the same package as the tested code
Pro:
- Makes
it possible to test the hidden method
- Clients
outside the package can not access the method
Con:
- Clients
in the package may then use the method
- Requires
organizational discipline to avoid using the method
How
to Test Hidden Methods Directly?
Method
2: Use inner classes
import junit.framework.TestCase;
public class Foo {
private int value;
private void bar() {
value = 10;
}
public static class FooTest extends TestCase {
public FooTest(String name) {
super(name );
}
public void testBar() {
Foo a = new Foo();
a.bar();
assert( 10 == a.value );
}
}
}
Pro:
- Provides
access to all methods/fields
- Test
does not have to be shipped with production code
- Test
stays with tested class
Con:
- Source
files are bit harder to read
- Must
remove all inner $class files from production code
- Test
not with other test classes
How
to Test Hidden Methods Directly?
Method
3: Use reflection
Pro:
- Java
reflection provides access to all methods/fields of a class
- Does
not require any changes to tested class
Con:
- Reflection
can be slow
- Reflection
is cumbersome to use
- Requires
setting permission files
See
http://www.eli.sdsu.edu/courses/fall98/cs596/notes/reflection/reflection.html
for more information about reflection
Copyright ©, All rights reserved.
2000 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.
Previous   
visitors since 04-Oct-00
   Next