package com.jtester.pojo;
// a getter setter class for testing
public class Student {
private String name, id, roll_no, department;
private List<String> projects;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getRollNo() {
return roll_no;
}
public void setRollNo(String roll_no) {
this.roll_no = roll_no;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public List<String> getProjects() {
return projects;
}
public void setProjects(List<String> projects) {
this.projects = projects;
}
}
import org.junit.Test;
import com.jtester.pojo.Student;
public class ClassTest {
@Test
public void studentTest(){
Student student = new Student();
student.setName("Student1");
assertEqual("Student1",student.getName());
student.setId("1");
assertEqual("1",student.getId());
student.setRollNo("1234");
assertEqual("1234",student.getRollNo());
student.setDepartment("Science");
assertEqual("Science",student.getDepartment());
List<String> projects = Arrays.asList("quantamMechanis","gravityIllusion");
student.setProjects(projects);
List<String> returnedProjects = student.getProjects();
assertEqual(projects.size(),returnedProjects.size());
for(int i = 0 ; i< project.size() ; i++) {
assertEqual(projects.get(i),returnedProjects.get(i));
}
// other getter setter can go here.
}
}
import org.junit.Test;
import com.jtester.pojo.Student;
public class ClassTest {
@Test
public void classTest(){
new ClassUnit(Student.class) // specify the class to be tested
.addStrategy(GetterSetterStrategy.class) // specify the strategy to be followed while testing
.test(); // test the class
}
}
The JTester Test Engine automates the tedious task of testing each getter setter to a few lines of
code, The GetterSetterStrategy.class
scans the given class by the following convention :
set
is a setter method.get
or is
is a getter method.Method Name | Separation | Association |
setName | set + Name | Name |
getName | get + Name | Name |
setValid | set + Valid | Valid |
isValid | is + Valid | Valid |
GetterSetterStrategy.class
uses convention over configuration.import org.junit.Test;
import com.jtester.units.PackageUnit;
public class PackageTest {
@Test
public void classTest(){
// this line will test all classes in package
new PackageUnit("com.jtester.pojo")
.addStrategy(GetterSetterStrategy.class)
.test();
}
}
package com.jtester.pojo;
public class Student {
protected Database db = Database.getDefaultConnection() ;
protected String department;
protected String loadDept() {
this.department = db.getDepartmentOfStudent(1);
}
public void getDept(String name) {
if(department==null)
this.loadDept();
return this.department;
}
public List<String> getProjects(String subjects){
return db.getProjectsFromSubjects(subjects);
}
public void setTeacher(Teacher teacher){
db.setTeacherForStudent(1,teacher);
}
}
import org.junit.Test;
import com.jtester.units.ClassUnit;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
public class PackageTest {
@InjectMock
Student student;
@Mock
private Database db;
@Test
public void classTest(){
List<String> projects = Arrays.asList("quantamMechanis","gravityIllusion");
Mockito.when(db.getDepartmentOfStudent()).thenReturn("Science");
Mockito.when(db.getProjectsFromSubjects()).thenReturn(projects);
String department = student.getDept();
assertEqual("Science",department);
List<String> returnedProjects = student.getProjects();
assertEqual(projects.size(),returnedProjects.size());
for(int i = 0 ; i< project.size() ; i++) {
assertEqual(projects.get(i),returnedProjects.get(i));
}
}
}
import org.junit.Test;
import com.jtester.units.ClassUnit;
public class GenericClassTest {
@Test
public void classTest(){
ClassUnit classUnit = new ClassUnit(Student.class);
classUnit.addStrategy(GenericStrategy.class);
classUnit.test();
}
}
These kinds of tests are completely Automated, no need to write mocks and asserts, all will be done
automatically. GenericStrategy will call all the visible Declared methods in Student.class
and
test them automatically.
db.getProjectsFromSubjects(subjects)
&db.getDepartmentOfStudent(1)
are automatically intercepted and a default object of thedb.getDepartmentOfStudent(1)
returns a String, so an emptyIf you want to even customize the generic test scroll on.
import org.junit.Test;
import com.jtester.units.ClassUnit;
public class GenericClassTest {
@Test
public void classTest(){
ClassUnit<Student> classUnit = new ClassUnit(Student.class);
classUnit.addStrategy(GenericStrategy.class);
List<String> projects = Arrays.asList("quantamMechanis","gravityIllusion");
classUnit.caseReturn(projects).getProjects();
classUnit.caseReturn("Science").getDept();
classUnit.caseVoid().setName("Dummy");
classUnit.test();
}
}
classUnit.caseReturn(projects) // the expected return
.getProjects(); // on the method
classUnit.caseVoid() // expect anything on return
.setName("Dummy"); // the parameter to be passed when tested
db.getDepartmentOfStudent(1)
is called.import org.junit.Test;
import com.jtester.units.ClassUnit;
public class GenericClassTest {
@JMock
Database database;
@Before
public void setUp(){
MockUnit.init(this);
}
@Test
public void classTest(){
MockUnit.when(database.getDepartmentOfStudent(1))
.thenReturn("Science");
ClassUnit classUnit = new ClassUnit(Student.class);
classUnit.addStrategy(GenericStrategy.class);
classUnit.test();
}
}
package com.jtester.mock.data; // all default mocks has to be in this package.
import com.jtester.interfaces.MockData;
import com.jtester.pojo.Teacher;
public class MockTeacherData extends MockData {
@Override
public Object load() {
Database database = MockUnit.mock(Database.class);
MockUnit.when(database.getDepartmentOfStudent(1))
.thenReturn("Science");
}
}
The process of creating test cases for each method and then Testing them is tedious, Hence an alternative Default Mock Data, i.e. Test Data is mapped to class names, and every time this class is encountered the mapped data is returned.
e.g.
package com.jtester.mock.data; // all default mocks has to be in this package.
import com.jtester.interfaces.DefaultData;
import com.jtester.pojo.Teacher;
public class MockTeacherData extends DefaultData {
public Class<?> getKey() {
return Teacher.class;
}
@Override
public Object getObject() {
Teacher teacher = new Teacher();
teacher.setName("Dummy");
return teacher;
}
}
we have set a mock data Teacher.class
so every time every Teacher class needs this
The mock object is returned. i.e. Whenever setTeacher
is called the default object is sent as a
parameter instead of creating a default object. The default object can be used if the Teache class is
in the following places.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 | |
1 |