Junit4 简单基本模板

1 概述

在本文中,我们将看到所有JUnit测试用例都遵循的JUnit 4基本模板。这篇文章还解释了JUnit批注的基本用法。
 
以下模板是一个很好的起点。复制/粘贴和编辑这些模板以适合您的编码样式。

2 基本测试模板

import org.junit.*;
import static org.junit.Assert.*;
/**
 * 一点教程网: http://www.yiidian.com
 */
public class SampleTest {
 
    private java.util.List emptyList;
 
    /**
     * Sets up the test fixture. 
     * (Called before every test case method.)
     */
    @Before
    public void setUp() {
        emptyList = new java.util.ArrayList();
    }
 
    /**
     * Tears down the test fixture. 
     * (Called after every test case method.)
     */
    @After
    public void tearDown() {
        emptyList = null;
    }
    
    @Test
    public void testSomeBehavior() {
        assertEquals("Empty list should have 0 elements", 0, emptyList.size());
    }
 
    @Test(expected=IndexOutOfBoundsException.class)
    public void testForException() {
        Object o = emptyList.get(0);
    }
}

3 带Fixtures的基本测试模板

您可以将@BeforeClass批注添加到要在类中的所有测试之前运行的方法,并将@AfterClass批注添加到要在类中的所有测试之后运行的方法。
这是一个简单的例子:
package junit;
 
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
/**
 * 一点教程网: http://www.yiidian.com
 */
public class SimpleTest {
 
    private Collection collection;
    
    @BeforeClass
    public static void oneTimeSetUp() {
        // one-time initialization code        
    }
 
    @AfterClass
    public static void oneTimeTearDown() {
        // one-time cleanup code
    }
 
    @Before
    public void setUp() {
        collection = new ArrayList();
    }
    
    @After
    public void tearDown() {
        collection.clear();
    }
 
    @Test
    public void testEmptyCollection() {
        assertTrue(collection.isEmpty());
    }
    
    @Test
    public void testOneItemCollection() {
        collection.add("itemA");
        assertEquals(1, collection.size());
    }
}

在进行此测试后,这些方法将按以下顺序执行:

oneTimeSetUp()
setUp()
testEmptyCollection()
tearDown()
setUp()
testOneItemCollection()
tearDown()
oneTimeTearDown()

4 使用JUnit Framework的实际示例

让我们了解更多有关在实际示例中使用JUnit测试的信息。
考虑我们有一个 School Management System 应用程序,并且我们想为School实体编写一个示例测试用例。 
在此示例中,我们将看到School 实体的示例CRUD JUnit测试用例 :
/**
 * 一点教程网: http://www.yiidian.com
 */
public class SchoolServiceImplTest { 
    private SchoolService schoolService;
       
    /**
     * This method initialize all domain objects required for test methods.
     */
    @Before
    public final void setUp() throws Exception {

        // Instantiates a School instance;
        school = new School();
        school.setName("sms School");
        school.setAddress("sms School");
        school.setContactNo("0123456789");
        school.setFaxNo("0123456789");
        school.setWebsite("yiidian.com");
        school.setStartedDate(date);
        school.setModifiedTime(date);        
    }
  
    @Test
    public final void testAddSchool() throws Exception {
            School newSchool = schoolService.addSchool(school);
            assertNotNull("School Type object should not null ", newSchool);  
    }

   
    @Test
    public final void testUpdateSchool() {
            School newSchool = schoolService.addSchool(school);
            assertNotNull(newSchool);
            newSchool.setContactNo("0145785545");
            schoolService.updateSchool(newSchool);
    }

    @Test
    public final void testFindSchool() throws AkuraAppException {

            School newSchool = schoolService.addSchool(school);
            assertNotNull(newSchool);
            School findSchool = schoolService.findSchool(
                    newSchool.getSchoolId());
            assertNotNull("School Type object should not null ", findSchool);
    }

   
    @Test
    public final void testGetSchoolList() throws AkuraAppException {
            School newSchool = schoolService.addSchool(school);
            assertNotNull(newSchool);
            List<School> schoolList = schoolService.getSchoolList();
    }

   
    @After
    public final void teardown() throws SchoolNotFoundException {   
         schoolDao.delete(school);
    }
}

5 结论

在本文中,我们学习了有关JUnit基本模板的信息,从中我们可以编辑和编写测试用例。我们还看到了Fixtures的用法,并将测试用例编写到了学校管理系统应用程序的真实示例中。

热门文章

优秀文章