Unit testing with Seam
rossiweasel Apr 23, 2008 12:56 PMI just download the mbooking-mavenized example and added a service called LovService.After that, I created a unit test for it : LovServiceTest.
import junit.framework.Assert;
import org.jboss.seam.mock.SeamTest;
import org.jboss.seam.mock.BaseSeamTest.ComponentTest;
import org.testng.annotations.Test;
import com.gvn.platform.lov.entity.LovNode;
import com.gvn.platform.lov.service.LovService;
/**
* @author huynq
*
*/
public class LovServiceTest extends SeamTest{
/*
* Test the persistLovNode method in LovService
*/
@Test
public void testPersistLovNode() throws Exception {
new ComponentTest() {
@Override
protected void testComponents() throws Exception {Assert.assertNotNull(getInstance("lovService"));
LovService lovService = (LovService) getInstance("lovService");
LovNode newNode = lovService.newLovNode();
newNode.setName("LOV1");
lovService.persistLovNode(newNode);
}
}.run();
}
/*
* Test the updateLovNode method in LovService
*/
@Test
public void testUpdateLovNode() throws Exception {
new ComponentTest() {
@Override
protected void testComponents() throws Exception {
LovService lovService = (LovService) getInstance("lovService");
LovNode newNode = lovService.newLovNode();
newNode.setName("LOV2");
lovService.persistLovNode(newNode);
}
}.run();
}
/*
* Test the findLovNodeById method in LovService
*/
@Test
public void testFindLovNodeById() throws Exception {
new ComponentTest() {
@Override
protected void testComponents() throws Exception {
Assert.assertNotNull(getInstance("lovService"));
LovService lovService = (LovService) getInstance("lovService");
Assert.assertNotNull(lovService.findLovNodeById(1L));
Assert.assertEquals(1, lovService.findLovNodeById(1L).getId().longValue());
}
}.run();
}
/*
* Test the findLovNodeByName method in LovService
*/
@Test
public void testFindLovNodeByName() throws Exception {
new ComponentTest() {
@Override
protected void testComponents() throws Exception {
Assert.assertNotNull(getInstance("lovService"));
LovService lovService = (LovService) getInstance("lovService");
Assert.assertNotNull(lovService.findLovNodeByName("LOV2"));
}
}.run();
}
/*
* Test the findLovNodeByName method in LovService
*/
@Test
public void testDeleteLovNode() throws Exception {
new ComponentTest() {
@Override
protected void testComponents() throws Exception {
Assert.assertNotNull(getInstance("lovService"));
LovService lovService = (LovService) getInstance("lovService");
Assert.assertNotNull(lovService.findLovNodeByName("LOV2"));
Assert.assertEquals("LOV2", lovService.findLovNodeByName("LOV2").getName());
// lovService.deleteLovNode(lovService.findLovNodeByName("LOV2-rename"));
}
}.run();
}
/*
* Test the findAllLovNode method in LovService
*/
@Test
public void testFindAllLovNodes() throws Exception {
new ComponentTest() {
@Override
protected void testComponents() throws Exception {
Assert.assertNotNull(getInstance("lovService"));
LovService lovService = (LovService) getInstance("lovService");
Assert.assertNotNull(lovService.findAllLovNodes());
Assert.assertEquals(2, lovService.findAllLovNodes().size());
}
}.run();
}
}But there is a fail test.
Tests run: 6, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 50.076 sec <<< FAILURE! testFindAllLovNodes(com.gvn.platform.lov.test.LovServiceTest) Time elapsed: 0.056 sec <<< FAILURE! junit.framework.AssertionFailedError: expected:<2> but was:<1> at junit.framework.Assert.fail(Assert.java:47) at junit.framework.Assert.failNotEquals(Assert.java:282) at junit.framework.Assert.assertEquals(Assert.java:64) at junit.framework.Assert.assertEquals(Assert.java:201) at junit.framework.Assert.assertEquals(Assert.java:207) at com.gvn.platform.lov.test.LovServiceTest$6.testComponents(LovServiceTest.java:145) at org.jboss.seam.mock.BaseSeamTest$ComponentTest.run(BaseSeamTest.java:169) at com.gvn.platform.lov.test.LovServiceTest.testFindAllLovNodes(LovServiceTest.java:139)
I didn't know how can it fail. :(. But when i checked my test database there is 2 LovNode objects.
I think all objects will be persist after the tests end. Before that only 1 object is kept.
Or i made mistakes with my test class?