JUnit test if else case
Solution 1
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
}
}
Solution 2
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.
IntoTheDeep
Like most of us, I was born. Some years later I created my first website. I love traveling and I'm an espresso snob. I share my knowledge about the web with my students in college and I would love to teach at your company. For those that are interested in old school paper, I have a bachelor degree in IT and a master degree in commercial sciences. Yup, that last one is for making sure I earn more than I spend. I love running, I even made an app for making running with others easier. If you ever feel like going for a run and talk shop, I’m cool with that.
Updated on October 29, 2020Comments
-
IntoTheDeep over 3 years
How to write test to current method? I use jUnit 4.
public void setImage() { if(conditionOne){ myView.setImageOne(); } else { myView.setImageTwo(); } }
-
IntoTheDeep over 7 yearsThis is not an answer
-
developer over 7 yearsCan you elaborate more on your question ?
-
IntoTheDeep over 7 yearsTo elaborate what exactly? I have posted method with condition and that change execution of other two methods. I want to test this method
-
developer over 7 yearsSo, you need two test methods (scenarios) as explained/provided in my answer.
-
IntoTheDeep over 7 yearsThis is if I want to test other two methods, not current one
-
walen over 7 years@TeodorKolev You asked if you should write two methods or not, and javaguy gave you an answer. If you are asking for us to write the tests for you, that's a different question, that can be answered by reading some JUnit tutorial.
-
IntoTheDeep over 7 yearsHello... my question starts with "How to write test to current method?"
-
Markus Mitterauer over 7 years@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.
-
Amresh over 7 yearsThere 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.
-
The_Martian over 4 yearsThe setImage method has a return value, yet the signature is void. It needs to return view not void.