4

How to write test to current method? I use jUnit 4.

   public void setImage() {
        if(conditionOne){
            myView.setImageOne();
        } else {
            myView.setImageTwo();
        }
    }
5
  • 2
    You aim for 100% code coverage with unit testing, so two tests.
    – zubergu
    Oct 27, 2016 at 6:55
  • 1
    Can you show example code of those tests? Oct 27, 2016 at 6:58
  • Side note: it's not necessary to write == true, just write if (conditionOne) {.
    – Jesper
    Oct 27, 2016 at 7:08
  • What have you tried so far? -- github.com/junit-team/junit4/wiki/Getting-started Oct 27, 2016 at 8:13
  • I am trying to set different image according to my condition Oct 27, 2016 at 8:14

2 Answers 2

4

You need to write two tests to cover both the scenarios as below:

import org.junit.Test;

public class SetImageTest {

    @Test
    public void testSetImageForConditionOne() {
        //write test to make conditionOne true
    }

    @Test
    public void testSetImageForElseCondition() {
        //write test to make conditionOne false
    }
}
9
  • 7
    This is not an answer Oct 27, 2016 at 7:18
  • 4
    Can you elaborate more on your question ?
    – developer
    Oct 27, 2016 at 7:19
  • 1
    Hello... my question starts with "How to write test to current method?" Oct 27, 2016 at 7:35
  • 2
    @TeodorKolev But you did not state, what you've tried so far and what your actual problem is with testing this method. See How to ask. Oct 27, 2016 at 8:05
  • 4
    There is no one-to-one mapping between method under test and test-method. A method may have n number of test-methods to tests its functionality under different parameters. In the code that javaguy shared, the method has two corresponding test-methods, one for testing whether if block worked correctly, other for testing whether else block worked correctly.
    – Amresh
    Oct 27, 2016 at 11:13
4

Okay... there is a flaw in the way you wrote this method. There is something called testable code. Here is a link (how to write testable code and why it matters) that discusses testable code.

The method you wrote above is non-deterministic. Which means the method can exhibit different behaviors on different runs, even if it has the same input. In your case you have no input.

Currently, your original method is based on the environment of the method and not the input. This practice can make it very difficult and in some cases impossible to write proper test for your code.

So this is how you want it to look like...

  public void setImage(boolean conditionOne) {
    if(conditionOne){
        myView.setImageOne();
    } else {
        myView.setImageTwo();
    }
}

Now that the test is deterministic your either going to have to test the variables that are in the environment, or have a return statement.

So (adding a return statement) you can do this.

  public static void setImage(boolean conditionOne, type myView) {
    if(conditionOne){
        myView.setImageOne();
    } else {
        myView.setImageTwo();
    }
    return myView; 
  }

Now your test can look something like this

public class SetImageTest {
  @Test
  public void testSetImage() {
    type myViewOrig;
    //define myViewOrig
    type myView1;
    //define myView1
    type myView2;
    //define myView2

    assertEquals(setImage(<true>, type myViewOrig), myView1);
    assertEquals(setImage(<false>, type myViewOrig), myView2);
  }
}

Or you can just test the myView object after running your setImage method.

1
  • 1
    The setImage method has a return value, yet the signature is void. It needs to return view not void. Sep 17, 2019 at 1:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.