随笔记

平凡人平凡路,沉下心迈出步

0%

PowerMock学习

PowerMock 学习研究

  • 添加maven依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>${spring.boot.version}</version>
    <scope>test</scope>
    <exclusions>
    <exclusion>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito2</artifactId>
    <version>2.0.7</version>
    <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>2.0.7</version>
    <scope>test</scope>
    </dependency>

非spring环境的mock测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@RunWith(PowerMockRunner.class)//等效 在@Before方法中加MockitoAnnotations.initMocks(this);
@PrepareForTest(SampleUtil.class)
public class UnitTestWithOutSpring {
/**
* 被测试服务
*/
@InjectMocks
private ServerConfig serverConfig;

@Before
public void beforeInit() throws IllegalAccessException {
System.out.println("before logic ");
serverConfig = new ServerConfig();
serverConfig.setHost("127.0.0.1");
System.out.println(serverConfig.getHost());
serverConfig.setHost("localhost");

mockStatic(SampleUtil.class);//mock 静态方法
when(SampleUtil.staticMethod()).thenReturn("static");

}

@After
public void after() throws Exception {
System.out.println("after logic ...");
}

@Test
@DisplayName("名字configTest...")
public void configTest() {
assertNotNull(serverConfig);
assertEquals("localhost", serverConfig.getHost());
}
}

含有spring环境的mock测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@ExtendWith(SpringExtension.class)
@RunWith(PowerMockRunner.class)
//@PowerMockRunnerDelegate(SpringRunner.class)
@SpringBootTest(classes = ServerConfig.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@PowerMockIgnore("javax.management.*")
@DisplayName("testName")
public class UnitTestWithSpring {
@Autowired
@InjectMocks
private ServerConfig serverConfig;


@Before
public void beforeTest() {
// System.out.println(serverConfig.getHost());
}

@DisplayName("ip测试")
@Test
public void doTest() {
Assert.assertEquals("127.0.0.1", serverConfig.getHost());
}
}

如何mock静态方法

1
2
mockStatic(SampleUtil.class);//mock 静态方法
when(SampleUtil.staticMethod()).thenReturn("static");

如何mock私有方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Test
public void testPrivate() throws IllegalAccessException {
//模拟私有字段mock
// 类实例(class instance)
PrivateObject instance = new PrivateObject("private");

// 模拟私有字段/变量(private field/variable)
MemberModifier
.field(PrivateObject.class, "privateString").set(instance, "hello mock private");

// 模拟私有方法(private method)
MemberModifier
.stub(MemberMatcher.method(PrivateObject.class, "getPrivateString"))
.toReturn("Power Mock");
System.out.println(instance.doTest());
}