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:

  1. Please take notice of the API documentation. The JMockit jar files (even the Maven ones) contain source code precisely to facilitate access to API documentation from any Java IDE.
  2. "Value classes" like 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.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top