In this example, you will use mock objects to isolate your tests from external dependencies. This means you do not need a database set up to run these tests, and it will be easier to test different scenarios. In a large project, you could even test your Manager class before someone else finished writing the DAO implementation.You can write your own mock objects, but here you are going to use jMock.
Create ProjectManagerImplTest in src/test/java/**/service/impl:
package com.ibm.ics.service.impl;
import java.util.ArrayList;
import java.util.List;
import org.jmock.Expectations;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.ibm.ics.dao.ProjectDao;
import com.ibm.ics.model.Project;
import static org.junit.Assert.*;
public class ProjectManagerImplTest extends BaseManagerMockTestCase {
private ProjectManagerImpl manager = null;
private ProjectDao dao = null;
@Before
public void setUp() {
dao = context.mock(ProjectDao.class);
manager = new ProjectManagerImpl(dao);
}
@After
public void tearDown() {
manager = null;
}
@Test
public void testGetProject() {
log.debug("testing get...");
final Long id = 7L;
final Project project = new Project();
// set expected behavior on dao
context.checking(new Expectations() {
{
one(dao).get(with(equal(id)));
will(returnValue(project));
}
});
Project result = manager.get(id);
assertSame(project, result);
}
@Test
public void testGetProjects() {
log.debug("testing getAll...");
final List projects = new ArrayList();
// set expected behavior on dao
context.checking(new Expectations() {
{
one(dao).getAll();
will(returnValue(projects));
}
});
List result = manager.getAll();
assertSame(projects, result);
}
@Test
public void testSaveProject() {
log.debug("testing save...");
final Project project = new Project();
// enter all required fields
// set expected behavior on dao
context.checking(new Expectations() {
{
one(dao).save(with(same(project)));
}
});
manager.save(project);
}
@Test
public void testRemoveProject() {
log.debug("testing remove...");
final Long id = -11L;
// set expected behavior on dao
context.checking(new Expectations() {
{
one(dao).remove(with(equal(id)));
}
});
manager.remove(id);
}
}
Create a ProjectManager interface in the src/main/java/**/service directory and specify the finder method for any implementation classes.
package com.ibm.ics.service;
import java.util.List;
import com.ibm.ics.model.Project;
public interface ProjectManager extends GenericManager<Project, Long> {
List<Project> findByName(String name);
}
The next step is to create a ProjectManagerImpl class that implements the methods in ProjectManager.
Create ProjectManagerImpl.java in src/main/java/**/service/impl.
package com.ibm.ics.service.impl;
import java.util.List;
import com.ibm.ics.dao.ProjectDao;
import com.ibm.ics.model.Project;
import com.ibm.ics.service.ProjectManager;
import org.springframework.stereotype.Service;
@Service("projectManager")
public class ProjectManagerImpl extends GenericManagerImpl<Project, Long>
implements ProjectManager {
ProjectDao projectDao;
@Autowired
public ProjectManagerImpl(ProjectDao projectDao) {
super(projectDao);
this.projectDao = projectDao;
}
public List<Project> findByName(String lastName) {
return projectDao.findByName(lastName);
}
}
Now you can run the Manager Test by running the command below:
mvn test -Dtest=ProjectManagerImplTest
you can now commit all files below as one git commit.
* src/main/java/com/ibm/ics/service/ProjectManager.java
* src/main/java/com/ibm/ics/service/impl/ProjectManagerImpl.java
* src/test/java/com/ibm/ics/service/impl/ProjectManagerImplTest.java