Последние тесты(надеюсь)
This commit is contained in:
parent
a2915a2295
commit
c992ddccd6
39
src/test/java/WeatherProviderRegistryTest.java
Normal file
39
src/test/java/WeatherProviderRegistryTest.java
Normal file
@ -0,0 +1,39 @@
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import ru.dima.weather.provider.WeatherProvider;
|
||||
import ru.dima.weather.provider.WeatherProviderRegistry;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
public class WeatherProviderRegistryTest {
|
||||
private WeatherProviderRegistry registry;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
registry = new WeatherProviderRegistry();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterAndGetWeatherProvider() {
|
||||
WeatherProvider provider = mock(WeatherProvider.class);
|
||||
registry.registerWeatherProvider(provider);
|
||||
|
||||
List<WeatherProvider> providers = registry.getAllWeatherProviders();
|
||||
assertEquals(1, providers.size());
|
||||
assertSame(provider, providers.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllWeatherProviders_Empty() {
|
||||
assertTrue(registry.getAllWeatherProviders().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAllWeatherProviders_Unmodifiable() {
|
||||
List<WeatherProvider> providers = registry.getAllWeatherProviders();
|
||||
assertThrows(UnsupportedOperationException.class, () -> providers.add(mock(WeatherProvider.class)));
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package ru.dima.weather.city;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import ru.dima.weather.http.HttpRequester;
|
||||
import ru.dima.weather.provider.MultiDaysWeatherInfo;
|
||||
import ru.dima.weather.provider.OpenWeatherMapWeatherProvider;
|
||||
import ru.dima.weather.provider.WeatherInfo;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class OpenWeatherMapWeatherProviderTest {
|
||||
|
||||
|
||||
@Test
|
||||
void TestGetName() {
|
||||
HttpRequester HttpRequester = mock(HttpRequester.class);
|
||||
OpenWeatherMapWeatherProvider OpenWeatherMapWeatherProvider = new OpenWeatherMapWeatherProvider(HttpRequester, "29ed5ca47c3d0c32385b18f1e82e522f");
|
||||
|
||||
OpenWeatherMapWeatherProvider.getName();
|
||||
assertEquals("open-weather-map", OpenWeatherMapWeatherProvider.getName(), "open-weather-map");
|
||||
}
|
||||
@Test
|
||||
void TestMultiDaysWeatherInfo() {
|
||||
HttpRequester httpRequester = mock(HttpRequester.class);
|
||||
String ft = "{\"list\": [{\"main\": {\"temp\": 22.5}}, {\"main\": {\"temp\": 19.85}}, {\"main\": {\"temp\": 20.97}}, {\"main\": {\"temp\": 22.19}}, {\"main\": {\"temp\": 25.41}}, {\"main\": {\"temp\": 26.21}}, {\"main\": {\"temp\": 22.53}}]}";
|
||||
|
||||
|
||||
when(httpRequester.getString(anyString())).thenReturn(ft);
|
||||
|
||||
OpenWeatherMapWeatherProvider OpenWeatherMapWeatherProvider = new OpenWeatherMapWeatherProvider(httpRequester , "29ed5ca47c3d0c32385b18f1e82e522f" );
|
||||
var weatherInfo = OpenWeatherMapWeatherProvider.getWeeklyWeatherInCity("Москва");
|
||||
assertEquals("MultiDaysWeatherInfo{perDayWeather=[WeatherInfo{temperature=22.5}, WeatherInfo{temperature=19.85}, WeatherInfo{temperature=20.97}, WeatherInfo{temperature=22.19}, WeatherInfo{temperature=25.41}, WeatherInfo{temperature=26.21}, WeatherInfo{temperature=22.53}]}",weatherInfo,"s");
|
||||
|
||||
}
|
||||
}
|
@ -1,17 +1,51 @@
|
||||
package ru.dima.weather.city;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
import ru.dima.weather.cli.CLIInputReader;
|
||||
import ru.dima.weather.cli.WeatherProviderCLISelector;
|
||||
import ru.dima.weather.provider.WeatherProvider;
|
||||
import ru.dima.weather.provider.WeatherProviderRegistry;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import java.util.Arrays;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class WeatherProviderCLISelectorTest {
|
||||
private CLIInputReader cliInputReader;
|
||||
private WeatherProviderRegistry weatherProviderRegistry;
|
||||
private WeatherProviderCLISelector selector;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
cliInputReader = mock(CLIInputReader.class);
|
||||
weatherProviderRegistry = mock(WeatherProviderRegistry.class);
|
||||
selector = new WeatherProviderCLISelector(cliInputReader, weatherProviderRegistry);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectWeatherProvider_ValidInput() {
|
||||
WeatherProvider provider = mock(WeatherProvider.class);
|
||||
when(provider.getName()).thenReturn("Provider 1");
|
||||
when(weatherProviderRegistry.getAllWeatherProviders()).thenReturn(Arrays.asList(provider));
|
||||
when(cliInputReader.readInteger()).thenReturn(1);
|
||||
|
||||
assertEquals(provider, selector.selectWeatherProviderFromCLI());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectWeatherProvider_InvalidInput() {
|
||||
WeatherProvider provider = mock(WeatherProvider.class);
|
||||
when(provider.getName()).thenReturn("Provider 1");
|
||||
when(weatherProviderRegistry.getAllWeatherProviders()).thenReturn(Arrays.asList(provider));
|
||||
when(cliInputReader.readInteger()).thenReturn(2).thenReturn(1);
|
||||
|
||||
assertEquals(provider, selector.selectWeatherProviderFromCLI());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSelectWeatherProvider_EmptyProviderList() {
|
||||
when(weatherProviderRegistry.getAllWeatherProviders()).thenReturn(Arrays.asList());
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> selector.selectWeatherProviderFromCLI());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user