Archives
Defeat "Static Cling"
Submitted by noreply@blogger.com (dastels) on Thu, 26/06/2008 - 17:49.You're pair programming and, as many brilliant people are apt to do, talking out loud. "I'll make a mock, inject it, and rerun the test. It should pa- ...D'OH" Your partner notices the exception "ConnectionFactory not initialized". "What?" she says, "Something is using the database? Dang, and this was supposed to be a small test."
Upon inspection you find that your class is calling a static method on some other class. You've got Static Cling! If you're (ab)using a data persistence layer that generates code which relies on static methods, and weren't careful, your code might look something like this:
Upon inspection you find that your class is calling a static method on some other class. You've got Static Cling! If you're (ab)using a data persistence layer that generates code which relies on static methods, and weren't careful, your code might look something like this:
public class MyObject {
public int doSomething(int id) {
return TheirEntity.selectById(id).getSomething();
}
}
ClassMock - Test Tool for Metadata-Based and Reflection-Based Components
Submitted by webmaster@testdriven.com (News) on Fri, 27/06/2008 - 03:22.ClassMock is a framework that helps the creation of unit tests for components that use reflection or annotations. In this kind of classes, the behavior is dependent of the class structure. This way, each test case usually works with a different class created specifically for the test. With ClassMock is possible to define and generate classes in runtime, allowing a better test readability and logic sharing between tests.
