BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)

This commit is contained in:
Vivek Balasubramaniam
2019-10-29 22:27:15 +05:30
parent db85c8f275
commit d3d5b060e7
20517 changed files with 1642290 additions and 0 deletions
@@ -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)));
}
}