Trong bài trước chúng ta đã cùng tìm hiểu về PowerMockito, trong bài viết này chúng ta sẽ cùng tìm hiểu cách sử dụng PowerMockito để viết test cho một số trường hợp đặc biệt như các static initializer, constructor, method, … gọi đến các 3rd party hay phương thức chưa được implement.
Nội dung
- 1 Suppress own constructor – Tạo instance một class mà không gọi constructor của chính nó
- 2 Suppress super class constructor – Tạo instance của một class nhưng không gọi constructor của super class
- 3 Suppress method – Ngăn thực thi một phương thức
- 4 Suppress fields – Ngăn khởi tạo giá trị mặc định cho một field
- 5 Suppress static initializer – Ngăn khởi tạo giá trị cho static method, static block
Suppress own constructor – Tạo instance một class mà không gọi constructor của chính nó
Giả sử chúng ta có một class cần test như sau:
package com.gpcoder.powermock.suppress.newInstance; public class WhiteboxNewInstance { public WhiteboxNewInstance(String message) { throw new UnsupportedOperationException("This function is unimplemented yet"); } public String getValue() { return "gpcoder.com"; } }
Như bạn thấy, chúng ta không thể gọi constructor để tạo instance để test phương thức getValue(). Với phương thức PowerMockito.mock(), nó cho chúng ta một object mock, không thể gọi thực thi phương thức getValue() thực sự. Với PowerMockito.spy(), chúng ta có thể gọi thực thi phương thức thực sự nhưng cần cung cấp một instance làm đối số của spy(). Để test phương thức này, chúng ta có thể sử dụng phương thức được hỗ trợ bởi PowerMockito Whitebox.newInstance() như sau:
package com.gpcoder.powermock.suppress.newInstance; import org.junit.Assert; import org.junit.Test; import org.powermock.reflect.Whitebox; public class WhiteboxNewInstanceTest { @Test public void testSuppressOwnConstructor() { WhiteboxNewInstance tested = Whitebox.newInstance(WhiteboxNewInstance.class); Assert.assertNotNull(tested.getValue()); } }
Suppress super class constructor – Tạo instance của một class nhưng không gọi constructor của super class
Giả sử chúng ta có một class cần test như sau:
public class BaseService { public BaseService() { throw new UnsupportedOperationException("This function is unimplemented yet"); } } public class ChildService extends BaseService { private final String message; public ChildService(String message) { this.message = message; } public String getMessage() { return message; } }
Tạo instance của một class nhưng không gọi constructor của super class, chúng ta cần khai báo:
- @PrepareForTest(ChildService.class)
- PowerMockito.suppress(PowerMockito.constructor(BaseService.class))
package com.gpcoder.powermock.suppress.suppress_constructor; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(ChildService.class) public class SuppressConstructorTest { @Test public void testSuppressConstructorOfEvilParent() throws Exception { PowerMockito.suppress(PowerMockito.constructor(BaseService.class)); final String message = "gpcoder.com"; ChildService tested = new ChildService(message); Assert.assertEquals(message, tested.getMessage()); } }
Suppress method – Ngăn thực thi một phương thức
Ví dụ chúng ta có class cần test như sau:
public class SuppressMethod { private final String username; public SuppressMethod(String username) { this.username = username; } public String getUsername() { checkPermission(); return "admin@" + username; } private void checkPermission() { throw new UnsupportedOperationException("This function is unimplemented yet"); } }
Với mong muốn, có thể thực thi phương thức getUsername(), nhưng không thực thi phương thức checkPermission(). Chúng ta có thể sử dụng PowerMockito.doNothing().when(mockedObject, “methodName”) hoặc sử dụng PowerMockito.suppress(PowerMockito.method(classToSuppressMethod, “methodName”)).
package com.gpcoder.powermock.suppress.suppress_method; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(SuppressMethod.class) public class SuppressMethodTest { @Test public void testMockDoNothing() throws Exception { SuppressMethod tested = PowerMockito.spy(new SuppressMethod("gpcoder.com")); PowerMockito.doNothing().when(tested, "checkPermission"); Assert.assertEquals("admin@gpcoder.com", tested.getUsername()); } @Test public void testSuppressMethod() throws Exception { SuppressMethod tested = new SuppressMethod("gpcoder.com"); PowerMockito.suppress(PowerMockito.method(SuppressMethod.class, "checkPermission")); Assert.assertEquals("admin@gpcoder.com", tested.getUsername()); } }
Suppress fields – Ngăn khởi tạo giá trị mặc định cho một field
Ví dụ chúng ta cần test đoạn code sau:
public class SuppressField { private String username = "gpcoder.com"; public String getUsername() { return username; } }
Giả sử bây giờ chúng ta không muốn khởi tạo các giá trị mặc định khi một instance của nó được tạo. Chúng ta có thể sử dụng PowerMockito.suppress(PowerMockito.field(classToSuppressField, “fieldName”)).
package com.gpcoder.powermock.suppress.suppress_field; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(SuppressField.class) public class SuppressFieldTest { @Test public void testSuppressField() throws Exception { PowerMockito.suppress(PowerMockito.field(SuppressField.class, "username")); SuppressField tested = new SuppressField(); Assert.assertNull(tested.getUsername()); } }
Chạy test trên, bạn thấy giá trị của field username bây giờ là null.
Suppress static initializer – Ngăn khởi tạo giá trị cho static method, static block
Giả sử chúng ta cần test class sau:
public class GoogleService { public GoogleService() { throw new UnsupportedOperationException("This function is unimplemented yet"); } } public class LoginService { private static final GoogleService GOOGLE_SERVICE = new GoogleService(); private final String message; public LoginService(String message) { this.message = message; } public String getMessage() { return message; } }
Trong class LoginService, có một static final field được cần được khởi tạo giá trị khi class được load. Tuy nhiên, class này sẽ bị lỗi ngay khi chúng ta gọi đến nó, do class GoogleService chưa được implement.
Để giải quyết vấn đề này chúng ta sẽ thêm khai báo
@SuppressStaticInitializationFor("packageName.ClassName") ở mức class
package com.gpcoder.powermock.suppress.suppress_static_initialization; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor("com.gpcoder.powermock.suppress.suppress_static_initialization.LoginService") public class SuppressStaticInitializationForTest { @Test public void testSuppressStaticInitializer() throws Exception { final String message = "gpcoder.com"; LoginService tested = new LoginService(message); Assert.assertEquals(message, tested.getMessage()); } }
Tài liệu tham khảo: