Skip to content

Instantly share code, notes, and snippets.

@tkrueger
Created September 28, 2013 12:43
Show Gist options
  • Save tkrueger/6741722 to your computer and use it in GitHub Desktop.
Save tkrueger/6741722 to your computer and use it in GitHub Desktop.
Mocking static method calls with Powermock vs. simple POJOs
package de.tk.mocks.staticaccess.custommock;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Configuration {
private SystemPropertyAccessor systemProperties;
private Properties properties = new Properties();
public Configuration() {
this(new SystemPropertyAccessor());
}
public Configuration(SystemPropertyAccessor systemProperties) {
this.systemProperties = systemProperties;
try {
InputStream configFile = getClass().getResourceAsStream(
"/config.properties");
if (configFile == null)
reportMissingConfigurationFile();
properties.load(configFile);
} catch (IOException e) {
reportMissingConfigurationFile();
}
}
private void reportMissingConfigurationFile() {
throw new RuntimeException(
"could not load properties from /config.properties");
}
public String getValue(String key) {
String valueFromSystem = systemProperties.get(key);
return valueFromSystem == null ? properties.getProperty(key)
: valueFromSystem;
}
}
package de.tk.mocks.staticaccess.custommock;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
public class ConfigurationTest {
private static final String KEY = "example.key";
private Configuration config;
@Before
public void setUp() {
config = new Configuration();
}
@Test
public void returnsValueFromProperties() throws Exception {
assertThat(config.getValue(KEY), equalTo("original value"));
}
@Test
public void prefersSystemPropertiesOverValuesFromConfigFile()
throws Exception {
SystemPropertyAccessor accessorMock = new SystemPropertyAccessor() {
@Override
public String get(String key) {
if (KEY.equals(key))
return "mocked value";
return super.get(key);
}
};
Configuration config = new Configuration(accessorMock);
assertThat(config.getValue(KEY), equalTo("mocked value"));
}
}
package de.tk.mocks.staticaccess.custommock;
public class SystemPropertyAccessor {
public String get(String key) {
return System.getenv(key);
}
}
[...]
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.5.1</version>
<scope>test</scope>
</dependency>
</dependencies>
[...]
package de.tk.mocks.staticaccess.powermock;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Configuration {
private Properties properties = new Properties();
public Configuration() {
try {
InputStream configFile = getClass().getResourceAsStream(
"/config.properties");
if (configFile == null)
reportMissingConfigurationFile();
properties.load(configFile);
} catch (IOException e) {
reportMissingConfigurationFile();
}
}
private void reportMissingConfigurationFile() {
throw new RuntimeException(
"could not load properties from /config.properties");
}
public String getValue(String key) {
String valueFromSystem = System.getProperty(key);
return valueFromSystem == null ? properties.getProperty(key)
: valueFromSystem;
}
}
package de.tk.mocks.staticaccess.powermock;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest({ System.class, Configuration.class })
public class ConfigurationTest {
private static final String KEY = "example.key";
private Configuration config;
@Before
public void setUp() {
config = new Configuration();
}
@Test
public void returnsValueFromProperties() throws Exception {
assertThat(config.getValue(KEY), equalTo("original value"));
}
@Test
public void prefersSystemPropertiesOverValuesFromConfigFile()
throws Exception {
PowerMockito.mockStatic(System.class);
Mockito.when(System.getProperty(KEY)).thenReturn("mocked value");
assertThat(config.getValue(KEY), equalTo("mocked value"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment