Reputation:
I wish to test the following class/method, It is a basic method that if u enter e.g. 2 then it will return "feb" and so on for each month.
package sem2pract3;
public class Ex2 {
public String month(int opt){
String month=null;
switch(opt){
case 1: month="Jan";
break;
case 2: month= "feb";
break;
case 3: month= "march";
break;
case 4: month= "april";
break;
case 5: month= "may";
break;
case 6: month= "June";
break;
case 7: month= "July";
break;
case 8: month= "Aug";
break;
case 9: month= "Sept";
break;
case 10: month= "Oct";
break;
case 11: month= "Nov";
break;
case 12: month= "Dec";
break;
default: System.out.println("Enter valid no");
}
return month;
}
}
This is my (incorrect) test class so far, however I am not sure how to implement the "actual" in order to be able to use assert equals?
package sem2pract3;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class Ex2Test {
int num;
String month;
Ex2 ex2;
@Before
public void setUp() throws Exception {
num=2;
ex2= new Ex2();
}
@Test
public void testMonth() {
String expected= month;
String actual= //Not sure what to put here
assertEquals(expected, actual);
}
}
Upvotes: 0
Views: 68
Reputation: 818
actual its the result you obtain calling to your actual code or in other words the code under test.
yo don't need the setUp method, at least not for the first test, the setUp method its used to establish or create a context needed for all your test, normally this setUp methods appears through refactoring when you are doing TDD.
an example code:
public void test_month_1_is_january() {
assertEquals(new Ex2().month(1), "jan");
}
A test its a specific example of use of your class, not a general rule, because this you need a test for each possibility, of perhaps thinks in your code in another way.
In this simple example you can do something like:
public void test_all_months() {
assertEquals(new Ex2().month(1), "jan");
assertEquals(new Ex2().month(2), "feb");
}
Upvotes: 0
Reputation: 311823
actual
is what your method returns. So, e.g., you could do something like this:
@Test
public void testMonth() {
String expected = "feb";
String actual = ex2.month(2);
assertEquals(expected, actual);
}
Upvotes: 3