PAN-11 Wrote unit tests for getAllDefinitions()
This commit is contained in:
@@ -1,9 +1,14 @@
|
|||||||
package edu.msudenver.tsp.services;
|
package edu.msudenver.tsp.services;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
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;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration(exclude = {DevToolsDataSourceAutoConfiguration.class,
|
||||||
|
HibernateJpaAutoConfiguration.class,
|
||||||
|
DataSourceAutoConfiguration.class})
|
||||||
public class ServiceConfig {
|
public class ServiceConfig {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package edu.msudenver.tsp.services;
|
package edu.msudenver.tsp.services;
|
||||||
|
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
import edu.msudenver.tsp.services.dto.Definition;
|
import edu.msudenver.tsp.services.dto.Definition;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -13,6 +14,8 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import static junit.framework.TestCase.assertTrue;
|
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.junit.Assert.*;
|
||||||
import static org.mockito.Matchers.*;
|
import static org.mockito.Matchers.*;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
@@ -23,6 +26,43 @@ public class DefinitionServiceTest {
|
|||||||
@Mock private RestService restService;
|
@Mock private RestService restService;
|
||||||
@InjectMocks private DefinitionService definitionService;
|
@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
|
@Test
|
||||||
public void testCreateDefinition() {
|
public void testCreateDefinition() {
|
||||||
final Definition testDefinition = createDefinition();
|
final Definition testDefinition = createDefinition();
|
||||||
|
|||||||
Reference in New Issue
Block a user