package de.juplo.demo;
import org.junit.jupiter.api.Test;
-import org.junit.jupiter.params.ParameterizedTest;
-import org.junit.jupiter.params.provider.ValueSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.net.URI;
import java.util.Optional;
-import static org.mockito.AdditionalMatchers.geq;
-import static org.mockito.AdditionalMatchers.lt;
-import static org.mockito.ArgumentMatchers.*;
+import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
verify(service, times(0)).checkAnswer(anyInt());
}
- @ParameterizedTest
- @ValueSource(ints = { 0, 1, 2, 3, 4, 5, 41, 42, 43, 666, Integer.MAX_VALUE })
- void test200ForPositiveAnswer(int number) throws Exception {
- when(service.checkAnswer(eq(42))).thenReturn(Optional.of(true));
- when(service.checkAnswer(geq(0))).thenReturn(Optional.of(false));
- when(service.checkAnswer(lt(0))).thenReturn(Optional.empty());
+ @Test
+ void test200ForEmptyAnswer() throws Exception {
+ mvc
+ .perform(get(URI.create("http://FOO/?answer=")))
+ .andExpect(status().isOk());
+
+ verify(service, times(0)).checkAnswer(anyInt());
+ }
+
+ @Test
+ void test200ForAnswerThatContainsOnlyWhitespace() throws Exception {
+ mvc
+ .perform(get(URI.create("http://FOO/?answer=%20")))
+ .andExpect(status().isOk());
+
+ verify(service, times(0)).checkAnswer(anyInt());
+ }
+
+ @Test
+ void test200ForWrongAnswer() throws Exception {
+ when(service.checkAnswer(anyInt())).thenReturn(Optional.of(false));
+
+ mvc
+ .perform(get(URI.create("http://FOO/?answer=1234")))
+ .andExpect(status().isOk());
+
+ verify(service, times(1)).checkAnswer(anyInt());
+ }
+
+ @Test
+ void test200ForCorrectAnswer() throws Exception {
+ when(service.checkAnswer(anyInt())).thenReturn(Optional.of(true));
mvc
- .perform(get(URI.create("http://FOO/?answer=" + number)))
+ .perform(get(URI.create("http://FOO/?answer=1234")))
.andExpect(status().isOk());
+
+ verify(service, times(1)).checkAnswer(anyInt());
}
- @ParameterizedTest
- @ValueSource(ints = { -1, -2, Integer.MIN_VALUE })
- void test400ForNegativeAnswer_NOT_WORKING(int number) throws Exception {
- when(service.checkAnswer(eq(42))).thenReturn(Optional.of(true));
- when(service.checkAnswer(geq(0))).thenReturn(Optional.of(false));
- when(service.checkAnswer(lt(0))).thenReturn(Optional.empty());
+ @Test
+ void test400ForNegativeAnswer_NOT_WORKING() throws Exception {
+ when(service.checkAnswer(anyInt())).thenReturn(Optional.empty());
// The expected behaviour of the following test is, that the NoSuchElementException
// with the message "No value present", that is raised, when the view calls .get()
// Instead, the exception bubbles up, becomes wrapped in a NestedServletException
// and is thrown in the call to perform()!
mvc
- .perform(get(URI.create("http://FOO/?answer=" + number)))
- .andExpect(status().isInternalServerError());
+ .perform(get(URI.create("http://FOO/?answer=1234")))
+ .andExpect(status().isBadRequest());
+
+ verify(service, times(1)).checkAnswer(anyInt());
}
@Test
void test400ForStringInput() throws Exception {
- when(service.checkAnswer(eq(42))).thenReturn(Optional.of(true));
- when(service.checkAnswer(geq(0))).thenReturn(Optional.of(false));
- when(service.checkAnswer(lt(0))).thenReturn(Optional.empty());
+ when(service.checkAnswer(anyInt())).thenReturn(Optional.empty());
mvc
.perform(get(URI.create("http://FOO/?answer=bar")))
.andExpect(status().isBadRequest());
+
+ verify(service, times(0)).checkAnswer(anyInt());
}
}