Unit Testing Action Classes in Seam

Posted on Friday, February 26, 2010

0


As we had already seen in the Recommended Seam Architecture, the UI event handling is done with the help of Action classes. These are Seam Action classes and their main purpose is UI event handling and handling the presentation logic. Testing action classes in Seam is not very different from the way we test the Service layer.

Here the idea is to again use Mockito and SeamTest. We would be mocking all interactions that the Action class would be making with any other component.

The idea for testing the Action classes would be to see that the presentation logic and the UI event handling logic is correct. Refer to the following code segment.


@RunWith(MockitoJUnitRunner.class)
public class SearchActionTest extends SeamTest {

 private SearchAction searchAction;

 @Mock
 private Log log;

 @Mock
 private CompanyService companyService;

 @Mock
 private CompanySearchCriteria searchCriteria;

 @Before
 public void setUp() {
 searchAction = new SearchAction();
 searchCriteria = new CompanySearchCriteria();
 searchCriteria.setCompanyName("google");

 setField(searchAction, "companyService", companyService);
 setField(searchAction, "log", log);
 setField(searchAction, "companySearchCriteria", searchCriteria);
 }

 @Test
 public void shouldSearchCompany() {
 List<Company> companies = new ArrayList<Company>();
 Company company = new Company("00-884-2922", "GOOGLE INC.", "?", new Location("2001 6TH AVE", "SEATTLE", "98121-2855", "WA", "US",
 LocationType.HEADQUARTERS));
 companies.add(company);

 when(companyService.findCompanies(searchCriteria)).thenReturn(companies);

 searchAction.findCompanies(searchCriteria);

 verify(companyService, times(1)).findCompanies(searchCriteria);

 Company searchedCompany = companyService.findCompanies(searchCriteria).iterator().next();

 Assert.assertEquals(company.getName(), searchedCompany.getName());
 }
}

Advertisements
Posted in: Java