日本无码免费高清在线|成人日本在线观看高清|A级片免费视频操逼欧美|全裸美女搞黄色大片网站|免费成人a片视频|久久无码福利成人激情久久|国产视频一二国产在线v|av女主播在线观看|五月激情影音先锋|亚洲一区天堂av

  • 手機站
  • 小程序

    汽車測試網(wǎng)

  • 公眾號
    • 汽車測試網(wǎng)

    • 在線課堂

    • 電車測試

首頁 > 汽車技術 > 正文

Java單元測試技巧之PowerMock

2021-03-12 18:47:07·  來源:高德技術  作者:常意  
 
前言 高德的技術大佬向老師在談論方法論時說到:“復雜的問題要簡單化,簡單的問題要深入化?!?這句話讓我感觸頗深,這何嘗不是一套編寫代碼的方法——把一個復雜邏輯拆分為許多簡單邏輯,然后把每一個簡單邏輯進行深入實現(xiàn),最后把這些簡單邏輯整合為復雜邏
前言

高德的技術大佬向老師在談論方法論時說到:“復雜的問題要簡單化,簡單的問題要深入化。”

這句話讓我感觸頗深,這何嘗不是一套編寫代碼的方法——把一個復雜邏輯拆分為許多簡單邏輯,然后把每一個簡單邏輯進行深入實現(xiàn),最后把這些簡單邏輯整合為復雜邏輯,總結為八字真言即是“化繁為簡,由簡入繁”。

編寫Java單元測試用例,其實就是把“復雜的問題要簡單化”——即把一段復雜的代碼拆解成一系列簡單的單元測試用例;寫好Java單元測試用例,其實就是把“簡單的問題要深入化”——即學習一套方法、總結一套模式并應用到實踐中。這里,作者根據(jù)日常的工作經(jīng)驗,總結了一些Java單元測試技巧,以供大家交流和學習。

一  準備環(huán)境

PowerMock是一個擴展了其它如EasyMock等mock框架的、功能更加強大的框架。PowerMock使用一個自定義類加載器和字節(jié)碼操作來模擬靜態(tài)方法、構造方法、final類和方法、私有方法、去除靜態(tài)初始化器等等。

1  引入PowerMock包

為了引入PowerMock包,需要在pom.xml文件中加入下列maven依賴:

<dependency> <groupId>org.powermockgroupId> <artifactId>powermock-module-junit4artifactId> <version>2.0.9version> <scope>testscope>dependency><dependency> <groupId>org.powermockgroupId> <artifactId>powermock-api-mockito2artifactId> <version>2.0.9version> <scope>testscope>dependency>
2  集成SpringMVC項目

在SpringMVC項目中,需要在pom.xml文件中加入JUnit的maven依賴:

<dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>4.12version> <scope>testscope>dependency>

3  集成SpringBoot項目

在SpringBoot項目中,需要在pom.xml文件中加入JUnit的maven依賴:

<dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-testartifactId> <scope>testscope>dependency>

4  一個簡單的測試用例

這里,用List舉例,模擬一個不存在的列表,但是返回的列表大小為100。

public class ListTest { @Test public void testSize() { Integer expected = 100; List list = PowerMockito.mock(List.class); PowerMockito.when(list.size()).thenReturn(expected); Integer actual = list.size(); Assert.assertEquals("返回值不相等", expected, actual); }}

二  mock語句

1  mock方法

聲明:
 
T PowerMockito.mock(Class clazz);

用途: 可以用于模擬指定類的對象實例。

當模擬非final類(接口、普通類、虛基類)的非final方法時,不必使用 @RunWith 和 @PrepareForTest 注解。當模擬final類或final方法時,必須使用 @RunWith 和 @PrepareForTest 注解。注解形如:

@RunWith(PowerMockRunner.class)
 
@PrepareForTest({TargetClass.class})

模擬非final類普通方法

@Getter@Setter@ToStringpublic class Rectangle implements Sharp { private double width; private double height; @Override public double getArea() { return width * height; }}
public class RectangleTest { @Test public void testGetArea() { double expectArea = 100.0D; Rectangle rectangle = PowerMockito.mock(Rectangle.class); PowerMockito.when(rectangle.getArea()).thenReturn(expectArea); double actualArea = rectangle.getArea(); Assert.assertEquals("返回值不相等", expectArea, actualArea, 1E-6D); }}

模擬final類或final方法

@Getter@Setter@ToStringpublic final class Circle { private double radius; public double getArea() { return Math.PI * Math.pow(radius, 2); }}
@RunWith(PowerMockRunner.class)@PrepareForTest({Circle.class})public class CircleTest { @Test public void testGetArea() { double expectArea = 3.14D; Circle circle = PowerMockito.mock(Circle.class); PowerMockito.when(circle.getArea()).thenReturn(expectArea); double actualArea = circle.getArea(); Assert.assertEquals("返回值不相等", expectArea, actualArea, 1E-6D); }}
 
2  mockStatic方法

聲明:

PowerMockito.mockStatic(Class clazz);

用途: 可以用于模擬類的靜態(tài)方法,必須使用“@RunWith”和“@PrepareForTest”注解。

@RunWith(PowerMockRunner.class)@PrepareForTest({StringUtils.class})public class StringUtilsTest { @Test public void testIsEmpty() { String string = "abc"; boolean expected = true; PowerMockito.mockStatic(StringUtils.class); PowerMockito.when(StringUtils.isEmpty(string)).thenReturn(expected); boolean actual = StringUtils.isEmpty(string); Assert.assertEquals("返回值不相等", expected, actual); }}

三  spy語句

如果一個對象,我們只希望模擬它的部分方法,而希望其它方法跟原來一樣,可以使用 PowerMockito.spy 方法代替 PowerMockito.mock 方法。于是,通過when語句設置過的方法,調用的是模擬方法;而沒有通過when語句設置的方法,調用的是原有方法。

1  spy類

聲明:
PowerMockito.spy(Class clazz);

用途: 用于模擬類的部分方法。

案例:

public class StringUtils { public static boolean isNotEmpty(final CharSequence cs) { return !isEmpty(cs); } public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0; }}
@RunWith(PowerMockRunner.class)@PrepareForTest({StringUtils.class})public class StringUtilsTest { @Test public void testIsNotEmpty() { String string = null; boolean expected = true; PowerMockito.spy(StringUtils.class); PowerMockito.when(StringUtils.isEmpty(string)).thenReturn(!expected); boolean actual = StringUtils.isNotEmpty(string); Assert.assertEquals("返回值不相等", expected, actual); }}

2  spy對象

聲明:
T PowerMockito.spy(T object);
用途: 用于模擬對象的部分方法。

案例:

public class StringUtils { public static boolean isNotEmpty(final CharSequence cs) { return !isEmpty(cs); } public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0; }}
@RunWith(PowerMockRunner.class)@PrepareForTest({StringUtils.class})public class StringUtilsTest { @Test public void testIsNotEmpty() { String string = null; boolean expected = true; PowerMockito.spy(StringUtils.class); PowerMockito.when(StringUtils.isEmpty(string)).thenReturn(!expected); boolean actual = StringUtils.isNotEmpty(string); Assert.assertEquals("返回值不相等", expected, actual); }}
 
四  when語句

1  when().thenReturn()模式

聲明:
PowerMockito.when(mockObject.someMethod(someArgs)).thenReturn(expectedValue);
 
PowerMockito.when(mockObject.someMethod(someArgs)).thenThrow(expectedThrowable);
 
PowerMockito.when(mockObject.someMethod(someArgs)).thenAnswer(expectedAnswer);
 
PowerMockito.when(mockObject.someMethod(someArgs)).thenCallRealMethod();

用途: 用于模擬對象方法,先執(zhí)行原始方法,再返回期望的值、異常、應答,或調用真實的方法。

返回期望值

public class ListTest { @Test public void testGet() { int index = 0; Integer expected = 100; List mockList = PowerMockito.mock(List.class); PowerMockito.when(mockList.get(index)).thenReturn(expected); Integer actual = mockList.get(index); Assert.assertEquals("返回值不相等", expected, actual); }}
 
返回期望異常

public class ListTest { @Test(expected = IndexOutOfBoundsException.class) public void testGet() { int index = -1; Integer expected = 100; List mockList = PowerMockito.mock(List.class);PowerMockito.when(mockList.get(index)).thenThrow(new IndexOutOfBoundsException()); Integer actual = mockList.get(index); Assert.assertEquals("返回值不相等", expected, actual); }}

返回期望應答

public class ListTest { @Test public void testGet() { int index = 1; Integer expected = 100; List mockList = PowerMockito.mock(List.class); PowerMockito.when(mockList.get(index)).thenAnswer(invocation -> { Integer value = invocation.getArgument(0); return value * 100; }); Integer actual = mockList.get(index); Assert.assertEquals("返回值不相等", expected, actual); }}

調用真實方法

public class ListTest { @Test public void testGet() { int index = 0; Integer expected = 100; List oldList = new ArrayList<>(); oldList.add(expected); List spylist = PowerMockito.spy(oldList);PowerMockito.when(spylist.get(index)).thenCallRealMethod(); Integer actual = spylist.get(index); Assert.assertEquals("返回值不相等", expected, actual); }}

2  doReturn().when()模式

聲明:
PowerMockito.doReturn(expectedValue).when(mockObject).someMethod(someArgs);
 
PowerMockito.doThrow(expectedThrowable).when(mockObject).someMethod(someArgs);
 
PowerMockito.doAnswer(expectedAnswer).when(mockObject).someMethod(someArgs);
 
PowerMockito.donothing().when(mockObject).someMethod(someArgs);
 
PowerMockito.doCallRealMethod().when(mockObject).someMethod(someArgs);

用途: 用于模擬對象方法,直接返回期望的值、異常、應答,或調用真實的方法,無需執(zhí)行原始方法。

注意, 千萬不要使用以下語法:

PowerMockito.doReturn(expectedValue).when(mockObject.someMethod(someArgs));
 
PowerMockito.doThrow(expectedThrowable).when(mockObject.someMethod(someArgs));
 
PowerMockito.doAnswer(expectedAnswer).when(mockObject.someMethod(someArgs));
 
PowerMockito.donothing().when(mockObject.someMethod(someArgs));
 
PowerMockito.doCallRealMethod().when(mockObject.someMethod(someArgs));

雖然不會出現(xiàn)編譯錯誤,但是在執(zhí)行時會拋出UnfinishedStubbingException異常。

返回期望值

public class ListTest { @Test public void testGet() { int index = 0; Integer expected = 100; List mockList = PowerMockito.mock(List.class); PowerMockito.doReturn(expected).when(mockList).get(index); Integer actual = mockList.get(index); Assert.assertEquals("返回值不相等", expected, actual); }}

返回期望異常

public class ListTest { @Test(expected = IndexOutOfBoundsException.class) public void testGet() { int index = -1; Integer expected = 100; List mockList = PowerMockito.mock(List.class); PowerMockito.doThrow(new IndexOutOfBoundsException()).when(mockList).get(index); Integer actual = mockList.get(index); Assert.assertEquals("返回值不相等", expected, actual); }}

返回期望應答

public class ListTest { @Test(expected = IndexOutOfBoundsException.class) public void testGet() { int index = -1; Integer expected = 100; List mockList = PowerMockito.mock(List.class); PowerMockito.doThrow(new IndexOutOfBoundsException()).when(mockList).get(index); Integer actual = mockList.get(index); Assert.assertEquals("返回值不相等", expected, actual); }}

模擬無返回值

public class ListTest { @Test public void testGet() { int index = 1; Integer expected = 100; List mockList = PowerMockito.mock(List.class); PowerMockito.doAnswer(invocation -> { Integer value = invocation.getArgument(0); return value * 100; }).when(mockList).get(index); Integer actual = mockList.get(index); Assert.assertEquals("返回值不相等", expected, actual); }}

調用真實方法

public class ListTest { @Test public void testGet() { int index = 0; Integer expected = 100; List oldList = new ArrayList<>(); oldList.add(expected); List spylist = PowerMockito.spy(oldList);PowerMockito.doCallRealMethod().when(spylist).get(index); Integer actual = spylist.get(index); Assert.assertEquals("返回值不相等", expected, actual); }}

3  兩種模式的主要區(qū)別

兩種模式都用于模擬對象方法,在mock實例下使用時,基本上是沒有差別的。但是,在spy實例下使用時,when().thenReturn()模式會執(zhí)行原方法,而doReturn().when()模式不會執(zhí)行原方法。

測試服務類

@Slf4j@Servicepublic class UserService { public long getUserCount() { log.info("調用獲取用戶數(shù)量方法"); return 0L; }}
 
使用when().thenReturn()模式

@RunWith(PowerMockRunner.class)public class UserServiceTest { @Test public void testGetUserCount() { Long expected = 1000L; UserService userService = PowerMockito.spy(new UserService()); PowerMockito.when(userService.getUserCount()).thenReturn(expected); Long actual = userService.getUserCount(); Assert.assertEquals("返回值不相等", expected, actual); }}

在測試過程中,將會打印出"調用獲取用戶數(shù)量方法"日志。

使用doReturn().when()模式

@RunWith(PowerMockRunner.class)public class UserServiceTest { @Test public void testGetUserCount() { Long expected = 1000L; UserService userService = PowerMockito.spy(new UserService()); PowerMockito.doReturn(expected).when(userService).getUserCount(); Long actual = userService.getUserCount(); Assert.assertEquals("返回值不相等", expected, actual); }}
 
在測試過程中,不會打印出"調用獲取用戶數(shù)量方法"日志。 

4  whenNew模擬構造方法

聲明:
PowerMockito.whenNew(MockClass.class).withNoArguments().thenReturn(expectedObject);
 
PowerMockito.whenNew(MockClass.class).withArguments(someArgs).thenReturn(expectedObject);

用途: 用于模擬構造方法。

案例:

public final class FileUtils { public static boolean isFile(String fileName) { return new File(fileName).isFile(); }}
@RunWith(PowerMockRunner.class)@PrepareForTest({FileUtils.class})public class FileUtilsTest { @Test public void testIsFile() throws Exception { String fileName = "test.txt"; File file = PowerMockito.mock(File.class); PowerMockito.whenNew(File.class).withArguments(fileName).thenReturn(file); PowerMockito.when(file.isFile()).thenReturn(true); Assert.assertTrue("返回值為假", FileUtils.isFile(fileName)); }}

注意:需要加上注解 @PrepareForTest({FileUtils.class}) ,否則模擬方法不生效。

五  參數(shù)匹配器

在執(zhí)行單元測試時,有時候并不關心傳入的參數(shù)的值,可以使用參數(shù)匹配器。

1  參數(shù)匹配器(any)

Mockito提供 Mockito.anyInt() 、 Mockito.anyString 、 Mockito.any(Class clazz) 等來表示任意值。

public class ListTest { @Test public void testGet() { int index = 1; Integer expected = 100; List mockList = PowerMockito.mock(List.class); PowerMockito.when(mockList.get(Mockito.anyInt())).thenReturn(expected); Integer actual = mockList.get(index); Assert.assertEquals("返回值不相等", expected, actual); }}
 
2  參數(shù)匹配器(eq)

當我們使用參數(shù)匹配器時,所有參數(shù)都應使用匹配器。如果要為某一參數(shù)指定特定值時,就需要使用 Mockito.eq() 方法。

@RunWith(PowerMockRunner.class)@PrepareForTest({StringUtils.class})public class StringUtilsTest { @Test public void testStartWith() { String string = "abc"; String prefix = "b"; boolean expected = true; PowerMockito.spy(StringUtils.class); PowerMockito.when(StringUtils.startsWith(Mockito.anyString(), Mockito.eq(prefix))).thenReturn(expected); boolean actual = StringUtils.startsWith(string, prefix); Assert.assertEquals("返回值不相等", expected, actual); }}

3  附加匹配器

Mockito的AdditionalMatchers類提供了一些很少使用的參數(shù)匹配器,我們可以進行參數(shù)大于(gt)、小于(lt)、大于等于(geq)、小于等于(leq)等比較操作,也可以進行參數(shù)與(and)、或(or)、非(not)等邏輯計算等。

public class ListTest { @Test public void testGet() { int index = 1; Integer expected = 100; List mockList = PowerMockito.mock(List.class); PowerMockito.when(mockList.get(AdditionalMatchers.geq(0))).thenReturn(expected); PowerMockito.when(mockList.get(AdditionalMatchers.lt(0))).thenThrow(new IndexOutOfBoundsException()); Integer actual = mockList.get(index); Assert.assertEquals("返回值不相等", expected, actual); }}

六  verify語句

驗證是確認在模擬過程中,被測試方法是否已按預期方式與其任何依賴方法進行了交互。

格式:
Mockito.verify(mockObject[,times(int)]).someMethod(somgArgs);
用途: 用于模擬對象方法,直接返回期望的值、異常、應答,或調用真實的方法,無需執(zhí)行原始方法。

案例:

1  驗證調用方法

public class ListTest { @Test public void testGet() { List mockList = PowerMockito.mock(List.class); PowerMockito.donothing().when(mockList).clear(); mockList.clear(); Mockito.verify(mockList).clear(); }}
 
2  驗證調用次數(shù)

public class ListTest { @Test public void testGet() { List mockList = PowerMockito.mock(List.class); PowerMockito.donothing().when(mockList).clear(); mockList.clear(); Mockito.verify(mockList, Mockito.times(1)).clear(); }}
 
除times外,Mockito還支持atLeastOnce、atLeast、only、atMostOnce、atMost等次數(shù)驗證器。

3  驗證調用順序

public class ListTest { @Test public void testAdd() { List mockedList = PowerMockito.mock(List.class); PowerMockito.doReturn(true).when(mockedList).add(Mockito.anyInt()); mockedList.add(1); mockedList.add(2); mockedList.add(3); InOrder inOrder = Mockito.inOrder(mockedList); inOrder.verify(mockedList).add(1); inOrder.verify(mockedList).add(2); inOrder.verify(mockedList).add(3); }}
 
4  驗證調用參數(shù)

public class ListTest { @Test public void testArgumentCaptor() { Integer[] expecteds = new Integer[] {1, 2, 3}; List mockedList = PowerMockito.mock(List.class);PowerMockito.doReturn(true).when(mockedList).add(Mockito.anyInt()); for (Integer expected : expecteds) { mockedList.add(expected); } ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(Integer.class);Mockito.verify(mockedList, Mockito.times(3)).add(argumentCaptor.capture()); Integer[] actuals = argumentCaptor.getAllValues().toArray(new Integer[0]); Assert.assertArrayEquals("返回值不相等", expecteds, actuals); }}
 
5  確保驗證完畢

Mockito提供 Mockito.verifyNoMoreInteractions 方法,在所有驗證方法之后可以使用此方法,以確保所有調用都得到驗證。如果模擬對象上存在任何未驗證的調用,將會拋出 NoInteractionsWanted 異常。

public class ListTest { @Test public void testVerifyNoMoreInteractions() { List mockedList = PowerMockito.mock(List.class); Mockito.verifyNoMoreInteractions(mockedList); // 執(zhí)行正常 mockedList.isEmpty(); Mockito.verifyNoMoreInteractions(mockedList); // 拋出異常 }}

備注: Mockito.verifyZeroInteractions 方法與 Mockito.verifyNoMoreInteractions 方法相同,但是目前已經(jīng)被廢棄。

6  驗證靜態(tài)方法

Mockito沒有靜態(tài)方法的驗證方法,但是PowerMock提供這方面的支持。

@RunWith(PowerMockRunner.class)@PrepareForTest({StringUtils.class})public class StringUtilsTest { @Test public void testVerifyStatic() { PowerMockito.mockStatic(StringUtils.class); String expected = "abc"; StringUtils.isEmpty(expected); PowerMockito.verifyStatic(StringUtils.class); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(String.class); StringUtils.isEmpty(argumentCaptor.capture()); Assert.assertEquals("參數(shù)不相等", argumentCaptor.getValue(), expected); }}
 
七  私有屬性

1  ReflectionTestUtils.setField方法

在用原生JUnit進行單元測試時,我們一般采用 ReflectionTestUtils.setField 方法設置私有屬性值。

@Servicepublic class UserService { @Value("${system.userLimit}") private Long userLimit; public Long getUserLimit() { return userLimit; }}
public class UserServiceTest { @Autowired private UserService userService; @Test public void testGetUserLimit() { Long expected = 1000L; ReflectionTestUtils.setField(userService, "userLimit", expected); Long actual = userService.getUserLimit(); Assert.assertEquals("返回值不相等", expected, actual); }}

注意:在測試類中,UserService實例是通過@Autowired注解加載的,如果該實例已經(jīng)被動態(tài)代理, ReflectionTestUtils.setField 方法設置的是代理實例,從而導致設置不生效。

2 Whitebox.setInternalState方法

現(xiàn)在使用PowerMock進行單元測試時,可以采用 Whitebox.setInternalState 方法設置私有屬性值。

@Servicepublic class UserService { @Value("${system.userLimit}") private Long userLimit; public Long getUserLimit() { return userLimit; }}
@RunWith(PowerMockRunner.class)public class UserServiceTest { @InjectMocks private UserService userService; @Test public void testGetUserLimit() { Long expected = 1000L; Whitebox.setInternalState(userService, "userLimit", expected); Long actual = userService.getUserLimit(); Assert.assertEquals("返回值不相等", expected, actual); }}
 
注意:需要加上注解 @RunWith(PowerMockRunner.class) 。

八  私有方法

1  模擬私有方法

通過when實現(xiàn)

public class UserService { private Long superUserId; public boolean isNotSuperUser(Long userId) { return !isSuperUser(userId); } private boolean isSuperUser(Long userId) { return Objects.equals(userId, superUserId); }}
@RunWith(PowerMockRunner.class)@PrepareForTest({UserService.class})public class UserServiceTest { @Test public void testIsNotSuperUser() throws Exception { Long userId = 1L; boolean expected = false; UserService userService = PowerMockito.spy(new UserService()); PowerMockito.when(userService, "isSuperUser", userId).thenReturn(!expected); boolean actual = userService.isNotSuperUser(userId); Assert.assertEquals("返回值不相等", expected, actual); }}
 
通過stub實現(xiàn)

通過模擬方法stub(存根),也可以實現(xiàn)模擬私有方法。但是,只能模擬整個方法的返回值,而不能模擬指定參數(shù)的返回值。

@RunWith(PowerMockRunner.class)@PrepareForTest({UserService.class})public class UserServiceTest { @Test public void testIsNotSuperUser() throws Exception { Long userId = 1L; boolean expected = false; UserService userService = PowerMockito.spy(new UserService()); PowerMockito.stub(PowerMockito.method(UserService.class, "isSuperUser", Long.class)).toReturn(!expected); boolean actual = userService.isNotSuperUser(userId); Assert.assertEquals("返回值不相等", expected, actual; }}
 
3  測試私有方法

@RunWith(PowerMockRunner.class)public class UserServiceTest9 { @Test public void testIsSuperUser() throws Exception { Long userId = 1L; boolean expected = false; UserService userService = new UserService(); Method method = PowerMockito.method(UserService.class, "isSuperUser", Long.class); Object actual = method.invoke(userService, userId); Assert.assertEquals("返回值不相等", expected, actual); }}

4  驗證私有方法

@RunWith(PowerMockRunner.class)@PrepareForTest({UserService.class})public class UserServiceTest10 { @Test public void testIsNotSuperUser() throws Exception { Long userId = 1L; boolean expected = false; UserService userService = PowerMockito.spy(new UserService()); PowerMockito.when(userService, "isSuperUser", userId).thenReturn(!expected); boolean actual = userService.isNotSuperUser(userId); PowerMockito.verifyPrivate(userService).invoke("isSuperUser", userId); Assert.assertEquals("返回值不相等", expected, actual); }}

這里,也可以用Method那套方法進行模擬和驗證方法。

九  主要注解

PowerMock為了更好地支持SpringMVC/SpringBoot項目,提供了一系列的注解,大大地簡化了測試代碼。

1  @RunWith注解

@RunWith(PowerMockRunner.class)

指定JUnit 使用 PowerMock 框架中的單元測試運行器。

2  @PrepareForTest注解

@PrepareForTest({ TargetClass.class })

當需要模擬final類、final方法或靜態(tài)方法時,需要添加@PrepareForTest注解,并指定方法所在的類。如果需要指定多個類,在{}中添加多個類并用逗號隔開即可。

3  @Mock注解

@Mock注解創(chuàng)建了一個全部Mock的實例,所有屬性和方法全被置空(0或者null)。

4  @Spy注解

@Spy注解創(chuàng)建了一個沒有Mock的實例,所有成員方法都會按照原方法的邏輯執(zhí)行,直到被Mock返回某個具體的值為止。

注意:@Spy注解的變量需要被初始化,否則執(zhí)行時會拋出異常。

5  @InjectMocks注解

@InjectMocks注解創(chuàng)建一個實例,這個實例可以調用真實代碼的方法,其余用@Mock或@Spy注解創(chuàng)建的實例將被注入到用該實例中。

@Servicepublic class UserService { @Autowired private UserDAO userDAO; public void modifyUser(UserVO userVO) { UserDO userDO = new UserDO(); BeanUtils.copyProperties(userVO, userDO); userDAO.modify(userDO); }}
@RunWith(PowerMockRunner.class)public class UserServiceTest { @Mock private UserDAO userDAO; @InjectMocks private UserService userService; @Test public void testCreateUser() { UserVO userVO = new UserVO(); userVO.setId(1L); userVO.setName("changyi"); userVO.setDesc("test user"); userService.modifyUser(userVO); ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(UserDO.class);Mockito.verify(userDAO).modify(argumentCaptor.capture()); UserDO userDO = argumentCaptor.getValue(); Assert.assertNotNull("用戶實例為空", userDO); Assert.assertEquals("用戶標識不相等", userVO.getId(), userDO.getId()); Assert.assertEquals("用戶名稱不相等", userVO.getName(), userDO.getName()); Assert.assertEquals("用戶描述不相等", userVO.getDesc(), userDO.getDesc()); }}
 
6  @Captor注解

@Captor注解在字段級別創(chuàng)建參數(shù)捕獲器。但是,在測試方法啟動前,必須調用 MockitoAnnotations.openMocks(this) 進行初始化。

@Servicepublic class UserService { @Autowired private UserDAO userDAO; public void modifyUser(UserVO userVO) { UserDO userDO = new UserDO(); BeanUtils.copyProperties(userVO, userDO); userDAO.modify(userDO); }}
@RunWith(PowerMockRunner.class)public class UserServiceTest { @Mock private UserDAO userDAO; @InjectMocks private UserService userService; @Captor private ArgumentCaptor argumentCaptor; @Before public void beforeTest() { MockitoAnnotations.openMocks(this); } @Test public void testCreateUser() { UserVO userVO = new UserVO(); userVO.setId(1L); userVO.setName("changyi"); userVO.setDesc("test user"); userService.modifyUser(userVO); Mockito.verify(userDAO).modify(argumentCaptor.capture()); UserDO userDO = argumentCaptor.getValue(); Assert.assertNotNull("用戶實例為空", userDO); Assert.assertEquals("用戶標識不相等", userVO.getId(), userDO.getId()); Assert.assertEquals("用戶名稱不相等", userVO.getName(), userDO.getName()); Assert.assertEquals("用戶描述不相等", userVO.getDesc(), userDO.getDesc()); }}
 
7  @PowerMockIgnore注解

為了解決使用PowerMock后,提示ClassLoader錯誤。

十  相關觀點

1 《Java開發(fā)手冊》規(guī)范

【強制】 好的單元測試必須遵守AIR原則。說明:單元測試在線上運行時,感覺像空氣(AIR)一樣感覺不到,但在測試質量的保障上,卻是非常關鍵的。好的單元測試宏觀上來說,具有自動化、獨立性、可重復執(zhí)行的特點。

  • A:Automatic(自動化)
  • I:Independent(獨立性)
  • R:Repeatable(可重復)

【強制】 單元測試應該是全自動執(zhí)行的,并且非交互式的。測試用例通常是被定期執(zhí)行的,執(zhí)行過程必須完全自動化才有意義。輸出結果需要人工檢查的測試不是一個好的單元測試。單元測試中不準使用System.out來進行人肉驗證,必須使用assert來驗證。

【強制】 單元測試是可以重復執(zhí)行的,不能受到外界環(huán)境的影響。

說明:單元測試通常會被放到持續(xù)集成中,每次有代碼check in時單元測試都會被執(zhí)行。如果單測對外部環(huán)境(網(wǎng)絡、服務、中間件等)有依賴,容易導致持續(xù)集成機制的不可用。

正例:為了不受外界環(huán)境影響,要求設計代碼時就把SUT的依賴改成注入,在測試時用spring 這樣的DI框架注入一個本地(內(nèi)存)實現(xiàn)或者Mock實現(xiàn)。

【推薦】 編寫單元測試代碼遵守BCDE原則,以保證被測試模塊的交付質量。

  • B:Border,邊界值測試,包括循環(huán)邊界、特殊取值、特殊時間點、數(shù)據(jù)順序等。
  • C:Correct,正確的輸入,并得到預期的結果。
  • D:Design,與設計文檔相結合,來編寫單元測試。
  • E:Error,強制錯誤信息輸入(如:非法數(shù)據(jù)、異常流程、業(yè)務允許外等),并得到預期的結果。
2  為什么要使用Mock?

根據(jù)網(wǎng)絡相關資料,總結觀點如下:

Mock可以用來解除外部服務依賴,從而保證了測試用例的獨立性

現(xiàn)在的互聯(lián)網(wǎng)軟件系統(tǒng),通常采用了分布式部署的微服務,為了單元測試某一服務而準備其它服務,存在極大的依耐性和不可行性。

Mock可以減少全鏈路測試數(shù)據(jù)準備,從而提高了編寫測試用例的速度

傳統(tǒng)的集成測試,需要準備全鏈路的測試數(shù)據(jù),可能某些環(huán)節(jié)并不是你所熟悉的。最后,耗費了大量的時間和經(jīng)歷,并不一定得到你想要的結果?,F(xiàn)在的單元測試,只需要模擬上游的輸入數(shù)據(jù),并驗證給下游的輸出數(shù)據(jù),編寫測試用例并進行測試的速度可以提高很多倍。

Mock可以模擬一些非正常的流程,從而保證了測試用例的代碼覆蓋率

根據(jù)單元測試的BCDE原則,需要進行邊界值測試(Border)和強制錯誤信息輸入(Error),這樣有助于覆蓋整個代碼邏輯。在實際系統(tǒng)中,很難去構造這些邊界值,也能難去觸發(fā)這些錯誤信息。而Mock從根本上解決了這個問題:想要什么樣的邊界值,只需要進行Mock;想要什么樣的錯誤信息,也只需要進行Mock。

Mock可以不用加載項目環(huán)境配置,從而保證了測試用例的執(zhí)行速度

在進行集成測試時,我們需要加載項目的所有環(huán)境配置,啟動項目依賴的所有服務接口。往往執(zhí)行一個測試用例,需要幾分鐘乃至幾十分鐘。采用Mock實現(xiàn)的測試用例,不用加載項目環(huán)境配置,也不依賴其它服務接口,執(zhí)行速度往往在幾秒之內(nèi),大大地提高了單元測試的執(zhí)行速度。

3  單元測試與集成測試的區(qū)別

在實際工作中,不少同學用集成測試代替了單元測試,或者認為集成測試就是單元測試。這里,總結為了單元測試與集成測試的區(qū)別:

測試對象不同

單元測試對象是實現(xiàn)了具體功能的程序單元,集成測試對象是概要設計規(guī)劃中的模塊及模塊間的組合。

測試方法不同

單元測試中的主要方法是基于代碼的白盒測試,集成測試中主要使用基于功能的黑盒測試。

測試時間不同

集成測試要晚于單元測試。

測試內(nèi)容不同

單元測試主要是模塊內(nèi)程序的邏輯、功能、參數(shù)傳遞、變量引用、出錯處理及需求和設計中具體要求方面的測試;而集成測試主要驗證各個接口、接口之間的數(shù)據(jù)傳遞關系,及模塊組合后能否達到預期效果。

分享到:
 
反對 0 舉報 0 收藏 0 評論 0
滬ICP備11026917號-25