Andate A La Concha De Tu Madre
Johan Haleby and Jan Kronquist www.powermock.org
Certified Java Professionals
1
Audience survey
Writing unit tests? Using mock framework? Using EasyMock? Frustrated withcurrent limitations?
Certified Java Professionals
2
Automated + isolated testing
Mock A A TestCase Code Under Test Mock B B
Certified Java Professionals
3
Mock objects and stubsSimulated objects that mimic behavior of real objects Stubs provide canned replies Mocks verify expectations
Certified Java Professionals
4
EasyMock
// Create a mock SomeInterface mock =createMock(SomeInterface.class); // Record behavior expect(mock.doStuff("argument")).andReturn("returnValue"); // Replay behavior replay(mock); // Executing the code we want to test assertEquals("returnValue",test.perform()); // Verify behavior verify(mock);
Certified Java Professionals
5
Traditional assumptions
Testable = good design Testable = easy to understand Testable = limitations
CertifiedJava Professionals
6
Problem - Static methods
Not possible to mock Still 3rd party libraries that use them
Eclipse, Java ME
Certified Java Professionals
7
Problem - Static initializersYou cannot unit test a class with evil static initializer
public class EvilStaticInitializer { static { System.loadLibrary(“evil.dll”); } }
Certified Java Professionals
8
Problem - Final class/ method
Final methods cannot be mocked Why use final?
Ensure that your classes are not misused Correct object construction
Examples: Wicket, Apache Mina
Certified Java Professionals
9Problem - Private methods/state
Invocation is hard Mocking not possible State access not possible Private is used to enforce encapsulation
Certified Java Professionals
10
Problem - Using newCode not designed for testing Not everything needs to be injected
File file = new File(fileName); if (file.exists()) { doStuff(); }
Certified Java Professionals
11
Traditional solutions
Better...
Regístrate para leer el documento completo.