Add tests

This commit is contained in:
Iulian Manda 2021-05-07 17:05:31 +03:00
parent 5cb49ba8a9
commit 0b590dece2
4 changed files with 145 additions and 0 deletions

View File

@ -4,6 +4,22 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>hexagonal</artifactId>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>12</source>
<target>12</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</build>
<name>hexagonal</name>
<description>A quick and practical example of Hexagonal Architecture in Java</description>
@ -14,4 +30,41 @@
<relativePath>../../parent-java</relativePath>
</parent>
<properties>
<maven.compiler.target>12</maven.compiler.target>
<maven.compiler.source>12</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.21.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,39 @@
package com.baeldung.pattern.hexagonal.repository;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class MockWeatherRepositoryUnitTest {
private final WeatherRepository tested = new MockWeatherRepository();
@Test
void givenLocationClujNapoca_whenGetTemperature_thenExpect17() {
double result = tested.getTemperature("cluj-napoca");
assertEquals(17, result);
}
@Test
void givenLocationBucharest_whenGetTemperature_thenExpect22() {
double result = tested.getTemperature("bucharest");
assertEquals(22, result);
}
@Test
void givenLocationNewYork_whenGetTemperature_thenExpect15() {
double result = tested.getTemperature("new york");
assertEquals(15, result);
}
@Test
void givenLocationDefault_whenGetTemperature_thenExpect20() {
double result = tested.getTemperature("default");
assertEquals(20, result);
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.pattern.hexagonal.repository;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
class RepositoryFactoryUnitTest {
@Test
void whenGetMockWeatherRepository_thenExpectMockWeatherRepositoryInstance() {
WeatherRepository result = RepositoryFactory.getMockWeatherRepository();
assertTrue(result instanceof MockWeatherRepository);
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.pattern.hexagonal.service;
import com.baeldung.pattern.hexagonal.repository.WeatherRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
@RunWith(JUnitPlatform.class)
class WeatherForecasterServiceUnitTest {
@Mock
private WeatherRepository repository;
@InjectMocks
private WeatherForecasterService tested;
@Test
void givenLocation10_whenForecast_thenExpect10() {
when(repository.getTemperature("location")).thenReturn(10d);
double result = tested.forecast("location");
assertEquals(10d, result);
}
@Test
void givenNullLocation_whenForecast_thenExpectEmptyString() {
when(repository.getTemperature("")).thenReturn(10d);
double result = tested.forecast(null);
assertEquals(10d, result);
}
}