public class EHI_employee {
private String name;
private Integer empid;
public EHI_Employee(String name, Integer empid) {
this.name = name;
this.empid = empid;
}
public int calculatesal(int empid) {
return empid * 3;
}
public void findname(int empid,String name) {
System.out.println("Employee with "+empid+" has name "+name);
}
}
My Test Class:
public class EHI_EmployeeTest {
@Mock
EHI_Employee mock;
@Test
public void calculatesalTest() {
assertEquals(mock.calculatesal(2), 6);
}}
Output Error
junit.framework.AssertionFailedError: Expected :0 Actual :6
But When I do:
public class EHI_EmployeeTest {
@InjectMocks
EHI_Employee mock;
@Test
public void calculatesalTest() {
assertEquals(mock.calculatesal(2), 6);
}}
Output:
:) test passed
I have read this thread: Difference between @Mock and @InjectMocks , but i dont understand this in my context, I would love if someone can explain me with my example.
When I do this , still my test is passed, but the actual method is not checked, right??
public class EHI_EmployeeTest {
@Mock
EHI_Employee mock;
@Test
public void calculatesalTest() {
when(mock.calculatesal(234)).thenReturn(268);
assertEquals(mock.calculatesal(234), 268);
}}
Please explain me!