定義:單元測試是對軟件或程序的基本(最小)組成單元的測試
對象:方法、類
特色:less
只要程序能運行就能夠了,寫單元測試浪費時間
代碼很簡單不須要單元測試
我保證不會有什麼問題的
測試人員測試一下就能夠了
此次沒有時間寫了,下次補上吧函數
單元測試->集成測試->端到端測試工具
各測試佔比單元測試
import org.junit.*; import static org.junit.Assert.fail; public class ClassNameTest { @BeforeClass //公開表態無返回值 public static void beforeClass() throws Exception{ //每次測試類執行前執行一次,主要用來初使化公共資源等 } @AfterClass //公開表態無返回值 public static void afterClass() throws Exception{ //每次測試類執行完成後執行一次,主要用來釋放資源或清理工做 } @Before public void setup() throws Exception { //每一個測試案例執行前都會執行一次 } @After public void teardown() throws Exception { //每一個測試案例執行完成後都會執行一次 } @Test public void testMethodName_give_…_when_…_then_…() { fail("失敗"); } }
@Ignore 該註解標記的測試方法在測試中會被忽略 @Test @Test(expected=xxxException.class) 斷言該方法會拋出異常 @Test(timeout=1000) 執行時間超過設置的值該案例會失敗 @RunWith @RunWith(Suite.class) 測試集運行器配合使用測試集功能 @RunWith(JUnit4.class) 默認運行器 @RunWith(Parameterized.class) 參數化運行器 @RunWith(Suite.class) @Suite.SuiteClasses({ CalculatorTest.class,SquareTest.class}) @Rule public class ExpectedExceptionsTest { @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void verifiesTypeAndMessage() { thrown.expect(RuntimeException.class); thrown.expectMessage("Runtime exception occurred"); throw new RuntimeException("Runtime exception occurred"); } }
@RunWith(Parameterized.class) public class PrimeFactorTest { private PrimeFactor primeFactor; private int input; private List<Integer> expected; //構造函數 public PrimeFactorTest(int input, List<Integer> expected) { this.input = input; this.expected = expected; } @Parameterized.Parameters public static Collection init() { return Arrays.asList(new Object[][]{ {18, Arrays.asList(2, 3, 3)} }); } @Test public void testFactor_when_input_18_then_must_return_2_3_3() { Assert.assertEquals(expected, primeFactor.factor(input)); } }
經常使用的斷言方法以下: assertEquals(a, b) 測試a是否等於b(a和b是原始類型數值(primitive value)或者必須爲實現比較而具備equal方法) assertFalse(a) 測試a是否爲false(假),a是一個Boolean數值。 assertTrue(a) 測試a是否爲true(真),a是一個Boolean數值 assertNotNull(a) 測試a是否非空,a是一個對象或者null。 assertNull(a) 測試a是否爲null,a是一個對象或者null。 assertNotSame(a, b) 測試a和b是否沒有都引用同一個對象。 assertSame(a, b) 測試a和b是否都引用同一個對象。 fail(string) Fail讓測試失敗,並給出指定信息。 assertThat(expected, Matcher) 經過Matcher斷言 Hamcrest :greaterThan,greaterThanOrEqualTo,lessThan,anything,anyOf,containsString
建議測試
要寫註釋,建議分爲以下4步驟。ui
1、mock就是在測試過程當中,對於某些不容易構造或者不容易獲取的對象,用一個虛擬的對象來建立以便測試的測試方法,這個虛擬的對象就是mock對象。mock對象就是真實對象在調試期間的代替品。
Java經常使用Mock
EasyMock、JMock、PowerMock、Mockit等this
2、Mock工具的原理
mock工具工做的原理大都以下:spa
3、Mockito使用介紹3d
經過代碼建立 1. public class UserServiceTest { 2. private UserService userService; 3. private UserDao mockUserDao; 4. @Before 5. public void setUp() { 6. mockUserDao = mock(UserDao.class); 7. userService = new UserServiceImpl(); 8. userService.setUserDao(mockUserDao); 9. } 經過註解 1. public class UserServiceTest { 2. 3. @InjectMocks 4. private UserServiceImpl userService; 5. 6. @Mock 7. private UserDao mockUserDao; 8. 9. @Before 10. public void setUp() { 11. MockitoAnnotations.initMocks(this); 12. }
4、Mockito經常使用方法調試
verify verify(mock, never()).add("twice"); 驗證add方法沒有被調用 verify(mock, times(2)).add("twice"); 驗證add方法被調用了2次 verify(mock, atLeast(n)).someMethod(); 方法至少被調用n次 verify(mock, atMost(n)).someMethod(); 方法最多被調用n次 when when(mock.someMethod()).thenReturn(value1).thenReturn(value2); when(mock.get(0)).thenReturn("first"); when(mock.get(1)).thenThrow(new RuntimeException()); when(mock.get(anyInt())).thenReturn("element"); spy List spy = spy(new LinkedList()); when(spy.get(0)).thenReturn(「foo"); doReturn("foo").when(spy).get(0);
假如你沒法給你程序寫單元測試,那麼意味着你的程序結構有問題,須要調整或重構。對待測試代碼要向生產代碼同樣,測試代碼也須要重構和維護。
3Q