Hi Thomas!

Thomas Calivera wrote:
> A general question regarding mock objects: When unit testing with mock
> objects, there is a general desire to break algorithms up into distinct
> parts and pass mock objects in and out of these algorithms. These
> fine-grained algorithms become separate methods that are usually candidates
> for private protection (in Java). However, since most unit tests are a
> separate class ([ClassToTest]Test.java), these algorithms become friendly at
> best (test class in package), public at worst. When this happens, the
> contract of the class is significantly extended (at least, within the
> package). Is this an unavoidable externality of fine-grained, comprehensive
> unit testing? Or is there a way, perhaps with inner classes, to retain
> private protections on methods not useful for other package or general
> classes, but enable granular unit testing?

Why not test the class from inside that same class? Why have another
class to test it? If a class needs considerable structure to test, and a
method will not do, I'd say that's a fair warning that the class needs
more work (i.e., the dreaded Refactoring).

If you test class A from class ATest, you would need to instantiate A
from ATest. Sometimes, even this is not possible, since constructors are
private. If ATest has some methods to allow the testing, you will need a
third class ATestTest to test that ATest is working properly. Ugh!

But, if you simply use a static method in A, you can instantiate A, and
call all of its methods, from a localized place. A simple class with a
simple job will always be testable this way. We use main() out of
convenience, since most IDE's will allow you to execute main() directly.
So, you code class A, you execute class A.

Class A
{
        private A()
        {
        ...
        }

        private int method1(int param)
        {
        ...
        }

        private String method2(String param)
        {
        ...
        }

        public static main(String[] params) throws Exception
        {
                A a = new A();
                System.out.println(a.method1(3));
                System.out.println(a.method2("hello"));
        }
}

My formation in eXtreme Programming is mostly self-taught, so I don't
know if this is in violation of XP principles.

Un saludo,

Alex.

Reply via email to