SpringBoot整合PowerMockito

PowerMockito可以用来mock静态方法
目录结构:
SpringBoot整合PowerMockito_第1张图片
pom文件:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.dagroupId>
    <artifactId>java-testartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>java-testname>
    <description>Demo project for Spring Bootdescription>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.17.RELEASEversion>
        <relativePath/> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
        <mockito.version>2.8.9mockito.version>
        <powermock.version>1.7.4powermock.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

        
        <dependency>
            <groupId>org.powermockgroupId>
            <artifactId>powermock-module-junit4artifactId>
            <version>${powermock.version}version>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.powermockgroupId>
            <artifactId>powermock-api-mockito2artifactId>
            <version>${powermock.version}version>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.mockitogroupId>
            <artifactId>mockito-coreartifactId>
            <version>${mockito.version}version>
            <scope>testscope>
        dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

测试类:

package com.da.javatest;

import java.util.Date;

import com.da.javatest.demo.OvertimeUtil;
import com.da.javatest.service.MySecondService;
import com.da.javatest.service.MyService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
// 这里 Ignore的类都会通过spring的ClassLoader来加载,尽量要让类都从spring的ClassLoader来加载,否则有问题
@PowerMockIgnore({ "com.*", "io.*", "org.*", "ch.*", "javax.validation.*" })
@PrepareForTest({OvertimeUtil.class})
@SpringBootTest(classes = { Application.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class JavaTestApplicationTests {

    @Autowired
    private MyService myService;
    @Mock
    private MySecondService mySecondService;

    @Test
    public void contextLoads() {
        PowerMockito.mockStatic(OvertimeUtil.class);
        Mockito.when(OvertimeUtil.calculationTime(Mockito.any(), Mockito.any())).thenReturn("4d");
        System.out.println(OvertimeUtil.calculationTime(new Date(), new Date()));
        System.out.println(myService.sayHello("Da."));
        Mockito.when(mySecondService.show(Mockito.anyString())).thenReturn("show test!");
        System.out.println(mySecondService.show("asd"));
    }
}

启动类:

package com.da.javatest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

接口1:

package com.da.javatest.service;

import org.springframework.stereotype.Service;

@Service
public class MyService {
    public String sayHello(String name) {
        return "hello " + name;
    }
}

接口2:

package com.da.javatest.service;

import org.springframework.stereotype.Service;

@Service
public class MySecondService {
    public String show(String text) {
        return text;
    }
}

你可能感兴趣的:(学习笔记)