0

Suppose I have a method I want to test, without a return statement:

public void func(){
    // some computing
    // call another method: anotherFunc(par);
}

Is it possible to change this method to this:

public boolean func(){
    // some computing
    // call another method: anotherFunc(par);
    return true;
}

If this method completes successfully, it returns true, otherwise false, to that a test would fail.

Evgenij Reznik
  • 17,916
  • 39
  • 104
  • 181
  • Very bad practice, use exceptions and assertions instead – Dici Sep 28 '14 at 18:37
  • 2
    What is that you want to test? – Oliver Charlesworth Sep 28 '14 at 18:38
  • This method returns nothing. If I change it to `boolean`, I can just go `assertTrue(tester.func());`. – Evgenij Reznik Sep 28 '14 at 18:41
  • 1
    But what behaviour are you trying to test? Without the return value, there is no observable behaviour, so there is nothing to test. – Oliver Charlesworth Sep 28 '14 at 18:42
  • You're trying to solve this the wrong way. I'm closing it as a duplicate of what it is you should be doing because the idea you're going after here is entirely implementation dependent which we can't help you with and it is very likely the wrong solution. You can test `void` methods; you just test them differently. – Jeroen Vannevel Sep 28 '14 at 18:43
  • You can just go assertTrue(tester.func()), but how does that better test that the method does what it should? – JB Nizet Sep 28 '14 at 18:44

0 Answers0