I am new to JUnit and facing difficulty in writing JUnit.
I have an interface and a class
Interface
public interface Student{
List<String> getStudentList();
void setStudentList(List<String> studentList);
void createStudentList();
}
Class
public class StudentServiceImpl implements Student{
private List<String> studentList
public List<String> getStudentList() {
return studentList;
}
public void setStudentList(final List<String> studentList){
this.studentList = studentList;
}
public void createStudentList(){
if (studentList == null) {
studentList = new ArrayList<String>();
studentList.add("John");
studentList.add("Bill");
studentList.add("Ricky");
studentList.add("Jack");
}
setStudentList(studentList);
}
}
What is the best to write Junit for above class. What should be kept in mind while writing JUnit cases. Can somebody help me?
Here is an example of IMHO well-written test case (written in notepad, please correct if does not compile):
Few tips:
Keep tests short and simple
Test name should describe what use case is being tested
Separate setup code, tested logic and assertions
Code should be easy to read. Use fluent and descriptive libraries like FEST
Further improvements:
The first test should fail, do you know why?
There are still few tests missing, do you know which ones?