Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An instance of new object in PowerMockito simulation method in java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report