Java课程学习笔记 — JUnit accuracy/failure/stress test区别
Posted 王景迁
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java课程学习笔记 — JUnit accuracy/failure/stress test区别相关的知识,希望对你有一定的参考价值。
accuracy test(结果准确性测试)
例如,Assert.assertEquals(expected, actual)。
如果结果不符合期望则产生failure。说明程序逻辑有问题。
failure test(抛出异常测试)
expected属性用来指示期望抛出的异常类型。例如,@Test(expected = IllegalArgumentException.class)。
如果结果不符合期望则产生failure。说明程序逻辑有问题。
stress test(运行时间测试)
timeout属性用来指示时间上限,单位是毫秒。例如,@Test(timeout = 300)。
如果超时则产生error。说明程序本身性能达不到要求。
举个例子
1 package edu.zju.cst.Student; 2 3 public class Student { 4 private String name; 5 6 public String getName() { 7 return name; 8 } 9 10 public void setName(String name) { 11 this.name = name; 12 } 13 14 public String speak(String stm) { 15 try { 16 Thread.sleep(400); 17 } catch (InterruptedException e) { 18 e.printStackTrace(); 19 } 20 21 return name + ": " + stm; 22 } 23 }
1 package edu.zju.cst; 2 3 import org.junit.After; 4 import org.junit.Before; 5 import org.junit.Test; 6 7 import edu.zju.cst.Student.Student; 8 import junit.framework.Assert; 9 10 public class StudentAccuracyTest { 11 private Student student; 12 13 @Before 14 public void setUp() { 15 student = new Student(); 16 student.setName("Tom"); 17 } 18 19 @Test 20 public void testSpeak() { 21 String expected = "Tom: hello"; 22 String actual = student.speak("hell"); 23 Assert.assertEquals(expected, actual); 24 } 25 26 @After 27 public void tearDown() { 28 29 } 30 }
1 package edu.zju.cst; 2 3 import org.junit.After; 4 import org.junit.Before; 5 import org.junit.Test; 6 7 import edu.zju.cst.Student.Student; 8 9 public class StudentFailureTest { 10 private Student student; 11 12 @Before 13 public void setUp() { 14 student = new Student(); 15 student.setName("Tom"); 16 } 17 18 @Test(expected = IllegalArgumentException.class) 19 public void testSpeak() throws IllegalArgumentException { 20 student.speak("hell"); 21 } 22 23 @After 24 public void tearDown() { 25 26 } 27 }
1 package edu.zju.cst; 2 3 import org.junit.After; 4 import org.junit.Before; 5 import org.junit.Test; 6 7 import edu.zju.cst.Student.Student; 8 9 public class StudentStressTest { 10 private Student student; 11 12 @Before 13 public void setUp() { 14 student = new Student(); 15 student.setName("Tom"); 16 } 17 18 @Test(timeout = 300) 19 public void testSpeak() { 20 student.speak("hell"); 21 } 22 23 @After 24 public void tearDown() { 25 26 } 27 }
1 package edu.zju.cst; 2 3 import org.junit.runner.RunWith; 4 import org.junit.runners.Suite; 5 import org.junit.runners.Suite.SuiteClasses; 6 7 @RunWith(Suite.class) 8 @SuiteClasses({StudentAccuracyTest.class, StudentFailureTest.class, 9 StudentStressTest.class}) 10 public class TestSuite { 11 12 }
参考资料
以上是关于Java课程学习笔记 — JUnit accuracy/failure/stress test区别的主要内容,如果未能解决你的问题,请参考以下文章