public MethodTester assertDouble(final double expected) { final Object[] localParameters = this.parameters; this.expected.add( clazz -> { final Object obj = this.execute(clazz, localParameters); assertThat(obj).isInstanceOf(Double.class); assertThat((Double) obj).isCloseTo(expected, Offset.offset(0.0001)); }); return this; }
@Test(dataProvider = "aggregateCases") public void shouldAggregate( @NotNull final Iterable<BigDecimal> values, final boolean expectedIsCompleted, @Nullable final BigDecimal expectedAggregatedValue) { // Given final ValueAggregator instance = createInstance(); // When for (BigDecimal value : values) { instance.aggregate(value); } // Then then(instance.isCompleted()).isEqualTo(expectedIsCompleted); if (expectedAggregatedValue == null) { then(instance.tryGetAggregatedValue()).isNull(); } else { then(instance.tryGetAggregatedValue()) .isCloseTo(expectedAggregatedValue, Offset.offset(new BigDecimal(.0001))); } }
/** @see com.kevinrschultz.weatherbuoy.ui.BuoyDetailViewModel */ @SmallTest @RunWith(AndroidJUnit4.class) public class BuoyDetailViewModelTest { private static final Offset<Double> PRECISION = Offset.offset(0.01); private static final double SPEED = 10.0; private static final int WIND_DIRECTION = 90; private static final WindCondition WIND_CONDTIONS = new WindCondition(SPEED, WIND_DIRECTION); private static final double HEIGHT = 5.0; private static final double PERIOD = 8.1; private static final int WAVE_DIRECTION = 225; private static final WaveCondition WAVE_CONDITION = new WaveCondition(HEIGHT, PERIOD, WAVE_DIRECTION); private MockInstrument mockInstrument; private Compass mockCompass; @Before public void setUp() throws Exception { mockInstrument = new MockInstrument(); mockCompass = mock(Compass.class); } @Test public void testGetWindDirection() { BuoyDetailViewModel viewModel = new BuoyDetailViewModel(WIND_CONDTIONS, WAVE_CONDITION, makeNauticalPreferences()); viewModel.updateWindDirection(mockInstrument, mockCompass); verify(mockCompass).setWindDirection(WIND_DIRECTION); assertThat(mockInstrument.value).isEqualTo("90"); assertThat(mockInstrument.units).isEqualTo(""); } @Test public void testGetWindSpeed_Imperial() { BuoyDetailViewModel viewModel = new BuoyDetailViewModel(WIND_CONDTIONS, WAVE_CONDITION, makeImperialPreferences()); viewModel.updateWindSpeed(mockInstrument); assertThat(mockInstrument.value).isEqualTo("11.5"); assertThat(mockInstrument.units).isEqualTo("mph"); } @Test public void testGetWindSpeed_Metric() { BuoyDetailViewModel viewModel = new BuoyDetailViewModel(WIND_CONDTIONS, WAVE_CONDITION, makeMetricPreferences()); viewModel.updateWindSpeed(mockInstrument); assertThat(mockInstrument.value).isEqualTo("18.5"); assertThat(mockInstrument.units).isEqualTo("kph"); } public void testGetWindSpeed_Nautical() { BuoyDetailViewModel viewModel = new BuoyDetailViewModel(WIND_CONDTIONS, WAVE_CONDITION, makeNauticalPreferences()); viewModel.updateWindSpeed(mockInstrument); assertThat(mockInstrument.value).isEqualTo("10.0"); assertThat(mockInstrument.units).isEqualTo("kts"); } @Test public void testGetWaveDirection() { BuoyDetailViewModel viewModel = new BuoyDetailViewModel(WIND_CONDTIONS, WAVE_CONDITION, makeNauticalPreferences()); viewModel.updateWaveDirection(mockCompass); verify(mockCompass).setWaveDirection(WAVE_CONDITION.getDirection()); } @Test public void testGetWavePeriod() { BuoyDetailViewModel viewModel = new BuoyDetailViewModel(WIND_CONDTIONS, WAVE_CONDITION, makeNauticalPreferences()); viewModel.updateWavePeriod(mockInstrument); assertThat(mockInstrument.value).isEqualTo("8.1"); assertThat(mockInstrument.units).isEqualTo("s"); } @Test public void testGetWaveHeight_Imperial() { BuoyDetailViewModel viewModel = new BuoyDetailViewModel(WIND_CONDTIONS, WAVE_CONDITION, makeImperialPreferences()); viewModel.updateWaveHeight(mockInstrument); assertThat(mockInstrument.value).isEqualTo("5.0"); assertThat(mockInstrument.units).isEqualTo("ft"); } @Test public void testGetWaveHeight_Metric() { BuoyDetailViewModel viewModel = new BuoyDetailViewModel(WIND_CONDTIONS, WAVE_CONDITION, makeMetricPreferences()); viewModel.updateWaveHeight(mockInstrument); assertThat(mockInstrument.value).isEqualTo("1.5"); assertThat(mockInstrument.units).isEqualTo("m"); } @Test public void testGetWaveHeight_Nautical() { BuoyDetailViewModel viewModel = new BuoyDetailViewModel(WIND_CONDTIONS, WAVE_CONDITION, makeNauticalPreferences()); viewModel.updateWaveHeight(mockInstrument); assertThat(mockInstrument.value).isEqualTo("5.0"); assertThat(mockInstrument.units).isEqualTo("ft"); } private WeatherBuoyPreferences makeImperialPreferences() { WeatherBuoyPreferences mockPreferences = mock(WeatherBuoyPreferences.class); when(mockPreferences.getUserUnitSystem()).thenReturn(UnitSystem.IMPERIAL); return mockPreferences; } private WeatherBuoyPreferences makeMetricPreferences() { WeatherBuoyPreferences mockPreferences = mock(WeatherBuoyPreferences.class); when(mockPreferences.getUserUnitSystem()).thenReturn(UnitSystem.METRIC); return mockPreferences; } private WeatherBuoyPreferences makeNauticalPreferences() { WeatherBuoyPreferences mockPreferences = mock(WeatherBuoyPreferences.class); when(mockPreferences.getUserUnitSystem()).thenReturn(UnitSystem.NAUTICAL); return mockPreferences; } private class MockInstrument implements Instrument { public String label; public String value; public String units; @Override public void setLabel(String label) { this.label = label; } @Override public void updateReading(String value, String units) { this.value = value; this.units = units; } } }
@Test public void should_create_Offset() { Float value = 0.8f; Offset<Float> offset = Offset.offset(value); assertThat(offset.value).isSameAs(value); }
@Test public void should_throw_error_if_value_is_negative() { thrown.expectIllegalArgumentException(offsetValueIsNotPositive()); Offset.offset(-1f); }
@Test public void should_throw_error_if_value_is_null() { thrown.expect(NullPointerException.class); Float value = null; Offset.offset(value); }