Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

An instance of new object in PowerMockito simulation method in java

Shulou Source: shulou.com Published: 2022-06-02 08:55:50 10月18日 Update

This article focuses on "PowerMockito simulation method in java new object instance", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the new object instance in the PowerMockito simulation method in java.

In doing unit testing, we sometimes need to isolate the objects that come out of the new in the method, which is why we need to use PowerMockito.

Add dependency

Org.powermock powermock-module-junit4 1.7.4 test org.powermock powermock-api-mockito 1.7.4 test org.mockito mockito-core 1.10.19 test

Note: the version should be consistent, and different versions may fail to start!

Tested class

@ Slf4j@Componentpublic class UserService {@ Autowired private UserMapper userMapper; public User userCreate (String name, String password) throws BusinessException {User user = userMapper.query (name, password); if (user = = null) {user = new User (); user.setName (name) User.setPassword (password); user.setGroup (1); user.setCreateTime (new Date ()); TokenApi tokenApi = new TokenApi (); / / you need to isolate this class when testing, which depends on other services! Try {user.setToken (tokenApi.getToken ());} catch (Exception e) {throw new BusinessException ("- 2", "failed to get token") } try {userMapper.create (user);} catch (Exception e) {throw new BusinessException ("- 1", "system exception") }} return user;}}

Test class

@ RunWith (PowerMockRunner.class) @ PrepareForTest ({UserService.class}) / / what is configured in this note is the class public class UserServiceTest {@ InjectMocks private UserService userService; @ Mock private UserMapper userMapper; private TokenApi tokenApi; @ Test public void userCreate () {String name = "xiaoming" in which the new object code of mock is required. String password = "000000"; when (userMapper.query (name, password)) .thenReturn (null); tokenApi = PowerMockito.mock (TokenApi.class); try {PowerMockito.whenNew (TokenApi.class). WithNoArguments (). ThenReturn (tokenApi) } catch (Exception e) {e.printStackTrace ();} PowerMockito.when (tokenApi.getToken ()) .thenReturn ("11111111"); User user = userService.userCreate (name, password); assertEquals (name,user.getName); assertEquals ("11111111", user.getToken ()) }}

When () and PowerMockito.when () are two different methods! When () = Mockito.when ()

At this point, I believe you have a deeper understanding of the "PowerMockito object instance in the PowerMockito simulation method in java", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Methods objects tests instances different content version learning isolation practicality deeper consistency two code interest unit practicality practicality location simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Redmi vpn MySQL NVidia