1. 程式人生 > 其它 >springboot2.0使用PowerMockito模擬java方法中的new物件

springboot2.0使用PowerMockito模擬java方法中的new物件

技術標籤:testng單元測試javaspring boot單元測試spring後端

一、環境說明

1、jdk8

2、springboot2.0.3

3、主要jar包準備

         <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy</artifactId>
            <version>1.10.10</version>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy-agent</artifactId>
            <version>1.10.10</version>
            <scope>test</scope>
        </dependency>
     <!--以上3個包放在springboot包之前 -->
     <!-- powermockito -->
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.0-beta.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-testng</artifactId>
            <version>2.0.0-beta.5</version>
            <scope>test</scope>
        </dependency>
      <!-- testNG -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.10</version>
            <scope>test</scope>
        </dependency>
       

這個版本的springboot會自帶依賴mockito-core2.15.0和net.bytebuddy1.7,都需要升級版本。另外,有mockito-core就不需要mockito-all包了。

這裡我們使用testng進行單元測試。

二、程式碼演示

1、定義介面

public interface UserService{
    User queryUserByUserId(String userId);
}

public class UserServiceImpl1 implements UserService{
     public User queryUserByUserId(String userId) {
          User user1 = new User();
          user1.setName("u1");
          return user1;
        }
    }
}

public class UserServiceImpl2 implements UserService{
     public User queryUserByUserId(String userId) {
          User user2 = new User();
          user2.setName("u2");
          return user2;
        }
    }
}

2、業務處理類

上述介面有2個實現類,我們想顯式的在業務處理類中呼叫指定的實現類,增加程式碼的可讀性。

public interface HandleService{
    void handle();
}

@Service
public class HandleServiceImpl implements HandleService{
     public void handle() {
          UserService userService = new UserServiceImpl1();
          User user = userService.queryUserByUserId("12345");
          if(user! = null){
          //to-do
          }
    }
}

3、測試類

import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockTestCase;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

@PrepareForTest({HandleServiceImpl.class})
public class HandleServiceImplTest extends PowerMockTestCase {

    @InjectMocks
    HandleServiceImpl handleService;

    @BeforeClass
    public void init() {
        MockitoAnnotations.openMocks(this);
    }

    @Test
    public void handleTest() {

        UserServiceImol1 userService= PowerMockito.mock(UserServiceImol1.class);
        try {
        PowerMockito.whenNew(UserServiceImol1.class).withNoArguments().thenReturn(userService);
        } catch (Exception e) {
        }

        User user = new User();
        //注意下面when呼叫是PowerMockito,不是Mockito
        //注意Mockito.anyString()作為入參會報錯
        PowerMockito.when(userService.queryUserByUserId(Mockito.any())).thenReturn(user);
        handleService.handle();
       
    }

}

這裡的extends PowerMockTestCase必須有,@PrepareForTest必須有,不過PrepareForTest裡面也可以放2個類{HandleServiceImpl.class,UserServiceImol1.class}。

好了,springboot2.0使用PowerMockito模擬java方法中的new物件的內容介紹到這裡就結束了。有不足之處,歡迎小夥伴們指正留言,多多交流!