Here is my code and I am doing code coverage testing
public class RegisterTest {
@Tested Register register;
@Test
public void testGetStudentName(@Injectable final Student student) {
new NonStrictExpectations(){
{
student.getRollNo();
result="ab1";
}
};
assertEquals(register.getStudentNo(), "ab1");
}
}
I got assertion error for the above testcase because the injectable instance doesnt work here..I dont know y?
Here is my testclass...
Register.class
public class Register {
Student student=new Student();
public String getStudentNo(){
return student.getRollNo();
}
}
Here is my dependency class
Student.class
public class Student {
String name;
int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
How can I resolve that assertion error??
The @Injectable
mock used in the test does work. However, it is never injected into the tested object, which instead creates its own Student
instance. So, in situations like this you use @Mocked
, not @Injectable
.
I should point out two other things, though:
Student
(which merely contain getters/setters) are not good candidates for mocking. In general, they should not be mocked. Instead, real instances should be used.