mockito模拟静态方法

这里要用到使用powerMock
注意点:
1 @RunWith(PowerMockRunner.class)
2 PowerMockito.mockStatic(StaticTest.class);


package com.eyu.ahxy.module.staticd;

import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class StaticTest {

	@Test
	public void test1() {
		PowerMockito.mockStatic(StaticTest.class);
		PowerMockito.when(StaticTest.static1()).thenReturn("static");
		String result = StaticTest.static1();
		assertThat(result, equalTo("static"));
	}

	public static String static1() {
		return "test1";
	}

}




powmock的maven依赖:

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.1</version>
<scope>test</scope>
</dependency>

你可能感兴趣的:(mockito)