OO Design and testing

Hi there,

If one has a private variable and a public "setter" method in a class how would one go about testing this "setter"? Like to test if a particular object was actually set it would require a public "getter" which is exposing implementation.

Would it be best then to infer the validitity of the "setter" indirectly through testing another public business level method?

Regards,

James.

[421 byte] By [colliedunka] at [2007-10-2 3:01:52]
# 1

> Hi there,

> If one has a private variable and a public "setter"

> method in a class how would one go about testing this

> "setter"? Like to test if a particular object was

> actually set it would require a public "getter" which

> is exposing implementation.

> Would it be best then to infer the validitity of the

> "setter" indirectly through testing another public

> business level method?

> Regards,

> James.

You don't have to unit test every little method. Setters & getters are the perfect examples of methods I never test. You only need to test them when they contain meaningful business code, not just a plain assignment or return statement.

hth

Torajiroua at 2007-7-15 21:28:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
But if you assign an Object, say an Account obj or such, a Manager obj or even a Handler obj, would it business logic to ensure an Account has a Manager or a Handler assigned?Guess it depends on your perspective, I'm trying to see develope a "testing strategy".
colliedunka at 2007-7-15 21:28:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

Ok so I'm testing an example from "Junit in Action" by V. Massol. In the example there is an AccountService that has a transfer method to transfer funds between Accounts. To my mind this should be a private method as one would not want it abused. But it contains business logic I want to test. So how should one treat private methods in testing?

Should one test only public methods and in that way (maybe) indirectly test private ones thus ascertain their correctness?

James.

colliedunka at 2007-7-15 21:28:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

Your JUnit test is a client of the class that's being tested, so it should have no more access to private methods than any other client. That's one of the beauties of JUnit: it shifts your thinking and puts you in the shoes of your clients. You'll start designing that API to match their needs.

That does not mean that you should change that private method to public for the sake of testing. If it's not useful as a public method to clients, then leave it as private. Test the public ones.

%

duffymoa at 2007-7-15 21:28:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 5

excerpt from JUnit in Action, 3.3 (p.56) :

__

JUnit best practices: test anything that could possibly fail

Unit tests help ensure that your methods are keeping their API contracts with

other methods. If the contract is based solely on other components?keeping

their contracts, then there may not be any useful behavior for you to test. But

if the method changes the parameter抯 or field抯 value in any way, then you

are providing unique behavior that you should test. The method is no longer

a simple go-between梚t抯 a filtering or munging method with its own behavior

that future changes could conceivably break. If a method is changed so it

is not so simple anymore, then you should add a test when that change takes

place, but not before. As the JUnit FAQ puts it, 揟he general philosophy is this:

if it can抰 break on its own, it抯 too simple to break.?br>But what about things like JavaBean getters and setters? Well, that depends. If

you are coding them by hand in a text editor, then yes, you might want to test

them. It抯 surprisingly easy to miscode a setter in a way that the compiler

won抰 catch. But if you are using an IDE that watches for such things, then

your team might decide not to test simple JavaBean properties.

__

A good piece of advice to make your mind about all this ;-)

Torajiroua at 2007-7-15 21:28:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 6
One example of what you can test is how your object reacts on calling a setter with null argument.Denis Krukovsky http://dotuseful.sourceforge.net/ http://dkrukovsky.blogspot.com/
dkrukovskya at 2007-7-15 21:28:08 > top of Java-index,Other Topics,Patterns & OO Design...
# 7
Thanks guys,guess because I'm just new to the TDD mantra that I couldn't see the trees from the wood, but once I steped back a bit a few questions came to mind.Regards,James.
colliedunka at 2007-7-15 21:28:08 > top of Java-index,Other Topics,Patterns & OO Design...