当前位置:网站首页>Unit test (II) -- JUnit
Unit test (II) -- JUnit
2022-07-19 01:16:00 【Rain melody】
As mentioned above Junit yes Java One of the most commonly used testing frameworks in . Let's say Junit4 Examples to illustrate Junit Usage of .
- A basic Junit Test case template :
package junit;
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
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());
}
}
- Junit Commonly used annotations
(1)@org.junit.Test
Mark this is a test method .Junit An instance of this test class will be constructed first , Then call the test method .
@Test Comments provide 2 Parameters :
a. “expected”, Define the exceptions that the test method should throw , If the test method does not throw an exception or throws a different exception , Test to fail .
b.“timeout”, If the test runs longer than the defined time , Test to fail ( Unit is millisecond )
(2)@org.junit.BeforeClass
All unit test methods Execute the method with this annotation before execution , If the parent class inherited by the current unit test class also has a @BeforeClass Method of annotation , Then execute @BeforeClass Method of annotation , Then execute the subclass with @BeforeClass The method of annotation is annotated in public static void No parameter method
(3)@org.junit.AfterClass
All unit test methods After execution, execute the method with this annotation again , If the parent class inherited by the current unit test class also has a @AfterClass Method of annotation , Then execute the band in the subclass first @AfterClass Method of annotation , Then execute the parent class with @AfterClass Method of annotation , Be careful , If the belt @Before Method of annotation , belt @AfterClass The annotation method will certainly be implemented , Even with @BeforeClass Error in annotation method , It will also execute belt @AfterClass Method of annotation . in other words , If the belt @BeforeClass Method of annotation , Then the belt will be executed @AfterClass Method of annotation . Annotations in public static void No parameter method
(4)@org.junit.Before
Each unit test method The method with this annotation will be executed before execution , If the parent class inherited by the current unit test class also has a Before Method of annotation , Then execute the band in the parent class first @Before Method of annotation , Then execute the subclass with @Before Method of annotation Annotations in public void On the way
**(5)@org.junit.After **
After each unit test method is executed, the method with this annotation will be executed . If the parent class inherited by the current unit test class also has a After Method of annotation , Then execute the band in the subclass first @After Method of annotation , Then execute the parent class with @After Method of annotation , Be careful , If the belt @Before Method of annotation , belt @After The annotation method will certainly be implemented , Even if the test method or @Before Method error reporting , It will also execute belt @After Method of annotation . Of course , If you are taking @BeforeClass There is an error in the annotation method , Or it reports an error in the construction method , Then the belt will definitely not be executed @After Method of annotation .
in other words , If the belt @Before Method of annotation , Then the belt will be executed @After Method of annotation . Annotations in public void On the way
(6)@org.junit.Ignore
Configure on the test method , Then this test method will not be executed , Configure on the test class , Then all test methods of this test class will not be executed
- Junit Assertion

- Junit Execution order
have access to **@FixMethodOrder** You can specify the execution order of unit test methods , The accepted parameter value is :
MethodSorters.NAME_ASCENDING: By method name Alphabetical order Sort
MethodSorters.DEFAULT: By method name HashCode value Sequential execution . If the method name is HashCode Value consistent , Then follow MethodSorters.NAME_ASCENDING Sort .
MethodSorters.JVML: according to JVM The way to get it Sequential execution .
It's usually used MethodSorters.NAME_ASCENDING, That is, execute in the parent order of method names . The execution order of the final test class in the other two methods is actually unpredictable .
package junit;
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class SimpleTest {
@Test
public void testEmptyCollection() {
assertTrue(collection.isEmpty());
}
@Test
public void testOneItemCollection() {
collection.add("itemA");
assertEquals(1, collection.size());
}
}
- Runner and @Runwith
JUnit Use cases are all in Runner( Actuator ) To perform the . and RunWith You can specify a specific Runner. So most of the time we don't use @RunWith This annotation , This is because we use a default Runner, It is called BlockJunit4ClassRunner,** But it's in JUnit4.4 It was introduced later ,4.4 The previous version
other
@RunWith(Suite.class) And @Suite.SuiteClasses
@RunWith(Parameterized.class) And @Parameters
@RunWith(Categories.class) And @Category
@RunWith(Theories.class) And @Theory
@Rule,@ClassRule Of JUnit, It is called Junit4ClassRunner**.
- A practical Junit Single test example
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("javaguides.net");
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);
}
}
边栏推荐
- 【Liunx】发布Jar包、日志动态查看、查看程序进程、结束程序
- KQ permission control
- Service层需要接口吗
- 渗透测试信息收集总结
- Watermelon book chapter 4
- json相关图形
- 2021-3-22-腾讯-最少守卫数量
- Résolution de code aléatoire lors de l'insertion de valeurs chinoises dans la base de données MySQL
- The logical architecture of MySQL
- How to right-click to create a servlet in the idea development servlet project
猜你喜欢

西瓜书第三章(第一次&第二次)

Solve the garbled code when inserting Chinese values in MySQL database

在mysql數據庫插入中文值出現的亂碼解决

Factorybean usage scenario
![[MariaDB] solution: error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: yes)](/img/a6/410d81ad374e32e5cabd2eb242a183.png)
[MariaDB] solution: error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: yes)

记录一次easy_sql堆叠注入

西瓜书+南瓜书第1-2章

在mysql数据库插入中文值出现的乱码解决

【集合】常见操作ArrayList集合的方法

Oracle自动存储管理18c分步安装-1
随机推荐
2021-3-22-腾讯-最少守卫数量
Detailed evaluation of current popular redis visual management tools
JVM内存模型
记录定时任务中调用feign接口认证不通过的一次bug
@ConfigurationProperties注解使用
Matlab drawing heart
Lambda related graphics
JWT and token
The logical architecture of MySQL
virtualbox 菜单栏控制
使用Pytorch中的nn来实现线性回归(简洁实现)
[singleton mode] hungry, lazy, double lock security verification
Computer Graphics From Scratch - Chapter 3
Pytoch uses NN to realize softmax regression
Solve the garbled code when inserting Chinese values in MySQL database
[MariaDB] solution: error 1045 (28000): access denied for user 'root' @ 'localhost' (using password: yes)
How to right-click to create a servlet in the idea development servlet project
Advanced ROS communication mechanism
在centos安装mysql出现的问题
在mysql数据库插入中文值出现的乱码解决