In
this post, I am going to show, how to test model class employee.
Requirements
1.
Employee should have fields id of type int, firstName of type String and
lastName of type String
2.
Model class always stores the firstName and lastName in capital letters
TestCase
Define
Employee object and make sure id, firstName and lastName set correctly.
'org.hamcrest.MatcherAssert.assertThat'
method is used to test given value against a matcher.
For example,
assertThat("Employee
id should be equal to 1", emp.getId(), equalTo(1));
Above
statement pass the test case, if getId() of emp returns 1, else fail the test case.
First argument of assertThat method gives you the reason in failed test scenarios.
First argument of assertThat method gives you the reason in failed test scenarios.
Find
the below working application.
Employee.java
package com.sample.model; public class Employee { private int id; private String firstName; private String lastName; public Employee(int id, String firstName, String lastName){ this.id = id; this.firstName = firstName.toUpperCase(); this.lastName = lastName.toUpperCase(); } public int getId() { return id; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } }
EmployeeTest.java
package com.sample.model; import org.junit.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; public class EmployeeTest { @Test public void employeeConstructor_properInput_firstNameAndLastNameInUpper() { Employee emp = new Employee(1, "Chamu", "Majety"); assertThat("Employee id should be equal to 1", emp.getId(), equalTo(1)); assertThat("Employee firstName should be equal to CHAMU", emp.getFirstName(), equalTo("CHAMU")); assertThat("Employee lastName should be equal to MAJETY", emp.getLastName(), equalTo("MAJETY")); } }
No comments:
Post a Comment