views:

145

answers:

6

How would you assert code like this? What would be the best approach to do so?

public void doSomething(int myProperty){
    if (myProperty == 0) return;
    // If myProperty is not zero, do something in the method
}
+6  A: 

If this code returns nothing, what is its purpose? What will be wrong if you remove this line?

If you find a valuable answer to these questions, you will find hints to test your code.

If you find no answer, then you can safely remove the line (and thus have no test to write).

mouviciel
The line's there to make sure the code won't continu if myProperty is 0, changed the question content a bit to clarify.
Bas
This was already clear for me. You've made a first step to design your test.
mouviciel
I really like this answer. Give a man a fish and all that.
fearofawhackplanet
+1 give a man a fish indeed.
Lieven
+3  A: 

You could assert that whatever happens after the return doesn't happen when myProperty is zero.

For example, if the method is (pseudo code!)

if myProperty == 0 return

myOtherProperty = 2

then your unit test could

  • Arrange that myProperty is set to zero, myOtherProperty is set to something other than 2
  • Act by calling the method under test
  • Assert that myOtherProperty is still set to what it was set to before.
AakashM
Hmm changed the code a bit to clarify the situation. But your reply did help me :] I could ofcourse assert that whatever happens after the if(), isn't set.
Bas
A: 

The question I ask when I see this is: what happens when myProperty is not zero?

Is there code after this return that alters class level/shared state? Can you assert against the class level/shared state to verify the behaviour?

When myProperty is zero, then the state should be unaffected by the method

BrianB
A: 

You didn't post the method signature, so I suppose that it has void as return type. In this case there's no way to test it like you pointed; but if the method interacts with object scope properties you can test them to see if they change or not.

mamoo
+2  A: 

No matter what you do, you need to test against observable effects. Surely your method must be doing something.

When using state-based testing, you can inspect the state of your SUT or the Fixture after having executed the method to see if the state matches your expecations.

When that is not possible, you will need to perform Behavior Verification (aka interaction-based testing. This is often done with mock objects.

Mark Seemann
+1  A: 

I think Mark Seemann gave you complete answer - this is just an illustration to it.

SomeVerifiableClass actor;

public void doSomething(int myProperty){
    if (myProperty == 0) return;

    // If myProperty is not zero, do something in the method
    actor.doesSomething(myProperty);
}

Then your choices are to mock SomeVerifiableClass or to test with real one. If you use DI properly then mocking is a better option.

Mocking: validating behavior (pseudo code):

  verify(mockedActor).noMethodsCalled();

In case of real object you validate state:

  assert(isPrestine(actor));

If there is no actor in your case then you should be able to verify state of tested object (the one that executes doSomething).

grigory
I'll have to go for that last one :] I'm used to test every bit of logic in the code and thus also test every if() explicitly. But since this if() doesn't return anything, I questioned myself how to approach this. But I could test that whenever the if() returns, objects after that if() don't change.
Bas