PAN-11 Wrote unit tests for getAllDefinitions()

This commit is contained in:
2019-03-24 21:23:03 -06:00
parent 978b58fe2c
commit 92bea81ae5
2 changed files with 46 additions and 1 deletions
@@ -1,9 +1,14 @@
package edu.msudenver.tsp.services;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfiguration;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableAutoConfiguration
@EnableAutoConfiguration(exclude = {DevToolsDataSourceAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
DataSourceAutoConfiguration.class})
public class ServiceConfig {
}
@@ -1,6 +1,7 @@
package edu.msudenver.tsp.services;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import edu.msudenver.tsp.services.dto.Definition;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -13,6 +14,8 @@ import java.util.List;
import java.util.Optional;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.verify;
@@ -23,6 +26,43 @@ public class DefinitionServiceTest {
@Mock private RestService restService;
@InjectMocks private DefinitionService definitionService;
@Test
public void testGetAllDefinitions() {
final List<Definition> definitionList = new ArrayList<>();
final Definition testDefinition = createDefinition();
definitionList.add(testDefinition);
definitionList.add(testDefinition);
when(restService.get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()))
.thenReturn(Optional.of(definitionList));
final Optional<List<Definition>> listOfDefinitions = definitionService.getAllDefinitions();
assertTrue(listOfDefinitions.isPresent());
assertThat(listOfDefinitions.get().size(), is(2));
listOfDefinitions.get().forEach(definition -> assertThat(definition, equalTo(testDefinition)));
}
@Test
public void testGetAllDefinitions_ReturnsEmptyOptional() {
when(restService.get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()))
.thenReturn(Optional.empty());
final Optional<List<Definition>> listOfDefinitions = definitionService.getAllDefinitions();
assertFalse(listOfDefinitions.isPresent());
}
@Test
public void testGetAllDefinitions_ExceptionThrown() {
when(restService.get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString()))
.thenThrow(new UnsupportedOperationException("Test exception"));
final Optional<List<Definition>> listOfDefinitions = definitionService.getAllDefinitions();
assertFalse(listOfDefinitions.isPresent());
}
@Test
public void testCreateDefinition() {
final Definition testDefinition = createDefinition();