#37 added some mockMvcTest to TriggerController

This commit is contained in:
Fabio Formosa
2021-11-06 01:17:50 +01:00
parent 1cd7f605e3
commit 233b56f282
3 changed files with 22 additions and 10 deletions

View File

@@ -1,9 +1,11 @@
package it.fabioformosa.quartzmanager;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.ComponentScan;
@SpringBootTest
@ComponentScan("it.fabioformosa.quartzmanager")
@SpringBootConfiguration
public class QuartManagerApplicationTests {
@Test

View File

@@ -1,25 +1,27 @@
package it.fabioformosa.quartzmanager.controllers;
import it.fabioformosa.quartzmanager.QuartManagerApplicationTests;
import it.fabioformosa.quartzmanager.controllers.utils.TestUtils;
import it.fabioformosa.quartzmanager.controllers.utils.TriggerUtils;
import it.fabioformosa.quartzmanager.dto.SchedulerConfigParam;
import it.fabioformosa.quartzmanager.dto.TriggerDTO;
import it.fabioformosa.quartzmanager.services.SchedulerService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
@Disabled
@ContextConfiguration(classes = {QuartManagerApplicationTests.class})
@WebMvcTest(controllers = TriggerController.class, properties = {
"quartz-manager.jobClass=it.fabioformosa.quartzmanager.jobs.myjobs.SampleJob"
})
@@ -43,7 +45,7 @@ class TriggerControllerTest {
SchedulerConfigParam configParamToPost = SchedulerConfigParam.builder().maxCount(20).triggerPerDay(20000L).build();
mockMvc.perform(
post(TriggerController.TRIGGER_CONTROLLER_BASE_URL + "mytrigger")
post(TriggerController.TRIGGER_CONTROLLER_BASE_URL + "/mytrigger")
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtils.toJson(configParamToPost))
)
@@ -51,4 +53,15 @@ class TriggerControllerTest {
.andExpect(MockMvcResultMatchers.content().json(TestUtils.toJson(expectedTriggerDTO)))
;
}
@Test
void whenGetIsCalled_thenATriggerIsReturned() throws Exception {
TriggerDTO expectedTriggerDTO = TriggerUtils.getTriggerInstance();
Mockito.when(schedulerService.getTriggerByName("mytrigger")).thenReturn(expectedTriggerDTO);
mockMvc.perform(get(TriggerController.TRIGGER_CONTROLLER_BASE_URL + "/mytrigger")
.contentType(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().json(TestUtils.toJson(expectedTriggerDTO)));
}
}

View File

@@ -1,12 +1,9 @@
package it.fabioformosa;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = QuartManagerApplication.class)
@WebAppConfiguration
public class QuartManagerApplicationTests {