StackAsk is a community where developers ask questions, share answers and learn from each others real-world experience, so every solved problem helps the next person who runs into it.
package com.example.spring_test.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.example.spring_test.service.GoodByeService; import com.example.spring_test.service.HelloService; import lombok.AllArgsConstructor; @RestController @AllArgsConstructor public class GoodbyeController { private final HelloService helloService; private final GoodByeService goodByeService; @GetMapping("/hello-and-goodbye") public String hello(@RequestParam String who) { return helloService.hello(who) + " and " + goodByeService.goodBye(who); } }
GoodByeService
package com.example.spring_test.service; import org.springframework.stereotype.Service; @Service public class GoodByeService { public String goodBye(String who) { return "Goodbye " + who + '!'; } }
GoodbyeService
package com.example.spring_test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringJUnitTestStartup { public static void main(String[] args) { SpringApplication.run(SpringJUnitTestStartup.class); } }
package com.example.spring_test.test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; import com.example.spring_test.SpringJUnitTestStartup; import com.example.spring_test.service.GoodByeService; import com.example.spring_test.test.BaseIntegrationTest.Initializer; import com.example.spring_test.test.BaseIntegrationTest.SomeTestConfiguration; @ExtendWith(SpringExtension.class) @SpringBootTest( classes = SpringJUnitTestStartup.class, webEnvironment = WebEnvironment.DEFINED_PORT ) @ContextConfiguration( initializers = Initializer.class, classes = SomeTestConfiguration.class ) public class BaseIntegrationTest { @MockBean protected GoodByeService goodByeService; public static class Initializer implements ApplicationContextInitializer { @Override public void initialize(ConfigurableApplicationContext applicationContext) { // init something } } public static class SomeTestConfiguration { @Bean public Object someBean() { return "someBean"; } } }
package com.example.spring_test.test; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.test.web.servlet.MockMvc; @AutoConfigureMockMvc public class GoodbyeControllerTest extends BaseIntegrationTest { @Autowired private MockMvc mockMvc; @Test public void helloAndGoodByeTest() throws Exception { // given final String who = "World"; final String goodBye = "I will never say good bye"; when(goodByeService.goodBye(who)).thenReturn(goodBye); // when // then final String expected = "Hello World! and " + goodBye; mockMvc.perform(get("/hello-and-goodbye?who=World")) .andExpect(status().isOk()) .andExpect(content().string(expected)); verify(goodByeService, times(1)).goodBye(who); } }
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext>
2.2K Point(s)
1.2K Point(s)
313 Point(s)
178 Point(s)
138 Point(s)
Input your email to receive reset password link, or if you remember your password, you can click