PAN-11 Wrote unit tests for the findById method

This commit is contained in:
2019-03-24 21:41:16 -06:00
parent bc0d631e6c
commit ae88fd356d
@@ -1,7 +1,6 @@
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;
@@ -18,8 +17,7 @@ import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is; 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.*;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class DefinitionServiceTest { public class DefinitionServiceTest {
@@ -33,7 +31,7 @@ public class DefinitionServiceTest {
definitionList.add(testDefinition); definitionList.add(testDefinition);
definitionList.add(testDefinition); definitionList.add(testDefinition);
when(restService.get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString())) when(restService.get(anyString(), any(), anyInt(), anyInt(), anyString()))
.thenReturn(Optional.of(definitionList)); .thenReturn(Optional.of(definitionList));
final Optional<List<Definition>> listOfDefinitions = definitionService.getAllDefinitions(); final Optional<List<Definition>> listOfDefinitions = definitionService.getAllDefinitions();
@@ -41,26 +39,73 @@ public class DefinitionServiceTest {
assertTrue(listOfDefinitions.isPresent()); assertTrue(listOfDefinitions.isPresent());
assertThat(listOfDefinitions.get().size(), is(2)); assertThat(listOfDefinitions.get().size(), is(2));
listOfDefinitions.get().forEach(definition -> assertThat(definition, equalTo(testDefinition))); listOfDefinitions.get().forEach(definition -> assertThat(definition, equalTo(testDefinition)));
verify(restService).get(anyString(), any(), anyInt(), anyInt(), anyString());
} }
@Test @Test
public void testGetAllDefinitions_ReturnsEmptyOptional() { public void testGetAllDefinitions_ReturnsEmptyOptional() {
when(restService.get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString())) when(restService.get(anyString(), any(), anyInt(), anyInt(), anyString()))
.thenReturn(Optional.empty()); .thenReturn(Optional.empty());
final Optional<List<Definition>> listOfDefinitions = definitionService.getAllDefinitions(); final Optional<List<Definition>> listOfDefinitions = definitionService.getAllDefinitions();
assertFalse(listOfDefinitions.isPresent()); assertFalse(listOfDefinitions.isPresent());
verify(restService).get(anyString(), any(), anyInt(), anyInt(), anyString());
} }
@Test @Test
public void testGetAllDefinitions_ExceptionThrown() { public void testGetAllDefinitions_ExceptionThrownWhenSendingRequest() {
when(restService.get(anyString(), any(TypeToken.class), anyInt(), anyInt(), anyString())) when(restService.get(anyString(), any(), anyInt(), anyInt(), anyString()))
.thenThrow(new UnsupportedOperationException("Test exception")); .thenThrow(new UnsupportedOperationException("Test exception"));
final Optional<List<Definition>> listOfDefinitions = definitionService.getAllDefinitions(); final Optional<List<Definition>> listOfDefinitions = definitionService.getAllDefinitions();
assertFalse(listOfDefinitions.isPresent()); assertFalse(listOfDefinitions.isPresent());
verify(restService).get(anyString(), any(), anyInt(), anyInt(), anyString());
}
@Test
public void testFindById() {
final Definition testDefinition = createDefinition();
when(restService.get(anyString(), any(), anyInt(), anyInt(), anyString()))
.thenReturn(Optional.of(testDefinition));
final Optional<Definition> foundDefinition = definitionService.findById(testDefinition.getId());
assertTrue(foundDefinition.isPresent());
assertThat(foundDefinition.get().getId(), is(1));
assertThat(foundDefinition.get(), is(equalTo(testDefinition)));
verify(restService).get(anyString(), any(), anyInt(), anyInt(), anyString());
}
@Test
public void testFindById_ReturnsEmptyOptional() {
when(restService.get(anyString(), any(), anyInt(), anyInt(), anyString()))
.thenReturn(Optional.empty());
final Optional<Definition> nonExistentDefinition = definitionService.findById(1);
assertFalse(nonExistentDefinition.isPresent());
verify(restService).get(anyString(), any(), anyInt(), anyInt(), anyString());
}
@Test
public void testFindById_ExceptionThrownWhenSendingRequest() {
when(restService.get(anyString(), any(), anyInt(), anyInt(), anyString()))
.thenThrow(new UnsupportedOperationException("test exception"));
final Optional<Definition> exceptionThrowingDefinition = definitionService.findById(1);
assertFalse(exceptionThrowingDefinition.isPresent());
verify(restService).get(anyString(), any(), anyInt(), anyInt(), anyString());
}
@Test
public void testFindById_IdIsZero() {
final Optional<Definition> impossibleDefinition = definitionService.findById(0);
assertFalse(impossibleDefinition.isPresent());
verifyZeroInteractions(restService);
} }
@Test @Test
@@ -120,6 +165,7 @@ public class DefinitionServiceTest {
definition.setName("Test Name"); definition.setName("Test Name");
definition.setDefinition(definitionList); definition.setDefinition(definitionList);
definition.setNotation(notationList); definition.setNotation(notationList);
definition.setId(1);
return definition; return definition;
} }