Mocking a void method with EasyMock
Issue: BAEL-2952
This commit is contained in:
parent
0d6d9129c4
commit
51e1014b26
26
testing-modules/easymock/pom.xml
Normal file
26
testing-modules/easymock/pom.xml
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<project
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>testing-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>easymock</artifactId>
|
||||||
|
<name>easymock</name>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.easymock</groupId>
|
||||||
|
<artifactId>easymock</artifactId>
|
||||||
|
<version>4.0.2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.testing.easymock;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
public class ForecastProcessor {
|
||||||
|
private WeatherService weatherService;
|
||||||
|
|
||||||
|
public BigDecimal getMaximumTemperature(String locationName) {
|
||||||
|
|
||||||
|
Location location = new Location(locationName);
|
||||||
|
|
||||||
|
try {
|
||||||
|
weatherService.populateTemperature(location);
|
||||||
|
} catch (ServiceUnavailableException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return location.getMaximumTemparature();
|
||||||
|
}
|
||||||
|
|
||||||
|
public WeatherService getWeatherService() {
|
||||||
|
return weatherService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWeatherService(WeatherService weatherService) {
|
||||||
|
this.weatherService = weatherService;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,33 @@
|
|||||||
|
package com.baeldung.testing.easymock;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
public class Location {
|
||||||
|
private String name;
|
||||||
|
private BigDecimal minimumTemperature;
|
||||||
|
private BigDecimal maximumTemparature;
|
||||||
|
|
||||||
|
public Location(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getMinimumTemperature() {
|
||||||
|
return minimumTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMinimumTemperature(BigDecimal minimumTemperature) {
|
||||||
|
this.minimumTemperature = minimumTemperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getMaximumTemparature() {
|
||||||
|
return maximumTemparature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaximumTemparature(BigDecimal maximumTemparature) {
|
||||||
|
this.maximumTemparature = maximumTemparature;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.testing.easymock;
|
||||||
|
|
||||||
|
public class ServiceUnavailableException extends Exception {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 6961151537340723535L;
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.testing.easymock;
|
||||||
|
|
||||||
|
public interface WeatherService {
|
||||||
|
void populateTemperature(Location location) throws ServiceUnavailableException;
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.testing.easymock;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
import org.easymock.EasyMock;
|
||||||
|
import org.easymock.EasyMockRule;
|
||||||
|
import org.easymock.Mock;
|
||||||
|
import org.easymock.TestSubject;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class ForecastProcessorUnitTest {
|
||||||
|
private static int MAX_TEMP = 90;
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public EasyMockRule rule = new EasyMockRule(this);
|
||||||
|
|
||||||
|
@TestSubject
|
||||||
|
private ForecastProcessor forecastProcessor = new ForecastProcessor();
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private WeatherService mockWeatherService;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
forecastProcessor.setWeatherService(mockWeatherService);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
@Test
|
||||||
|
public void givenLocationName_whenWeatherServicePopulatesTemperatures_thenMaxTempReturned() throws ServiceUnavailableException {
|
||||||
|
mockWeatherService.populateTemperature(EasyMock.anyObject(Location.class));
|
||||||
|
EasyMock.expectLastCall()
|
||||||
|
.andAnswer(() -> {
|
||||||
|
Location passedLocation = (Location) EasyMock.getCurrentArguments()[0];
|
||||||
|
passedLocation.setMaximumTemparature(new BigDecimal(MAX_TEMP));
|
||||||
|
passedLocation.setMinimumTemperature(new BigDecimal(MAX_TEMP - 10));
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
EasyMock.replay(mockWeatherService);
|
||||||
|
BigDecimal maxTemperature = forecastProcessor.getMaximumTemperature("New York");
|
||||||
|
EasyMock.verify(mockWeatherService);
|
||||||
|
assertThat(maxTemperature, equalTo(new BigDecimal(MAX_TEMP)));
|
||||||
|
}
|
||||||
|
}
|
@ -33,6 +33,7 @@
|
|||||||
<module>testing</module>
|
<module>testing</module>
|
||||||
<module>testng</module>
|
<module>testng</module>
|
||||||
<module>junit-5-basics</module>
|
<module>junit-5-basics</module>
|
||||||
|
<module>easymock</module>
|
||||||
<module>junit-5-advanced</module>
|
<module>junit-5-advanced</module>
|
||||||
</modules>
|
</modules>
|
||||||
</project>
|
</project>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user