Junit4 简单基本模板
精华
小牛编辑
178浏览
2023-03-14
1 概述
在本文中,我们将看到所有JUnit测试用例都遵循的JUnit 4基本模板。这篇文章还解释了JUnit批注的基本用法。
以下模板是一个很好的起点。复制/粘贴和编辑这些模板以适合您的编码样式。
2 基本测试模板
import org.junit.*;
import static org.junit.Assert.*;
/**
* 小牛知识库网: https://www.xnip.cn
*/
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.*;
/**
* 小牛知识库网: https://www.xnip.cn
*/
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测试用例 :
/**
* 小牛知识库网: https://www.xnip.cn
*/
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("xnip.cn");
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);
}
}