#Spring in Action: Testing with WebMvcTest

1 messages · Page 1 of 1 (latest)

patent stump
#

very early in the book you create this Controller:

package tacos;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
 @GetMapping("/")
 public String home() {
 return "home";
 }
}

which then gets this test:

@WebMvcTest(HomeController.class)
public class HomeControllerTest {
 @Autowired
 private MockMvc mockMvc;
 @Test
 public void testHomePage() throws Exception {
 mockMvc.perform(get("/"))
 .andExpect(status().isOk())
 .andExpect(view().name("home"))
 .andExpect(content().string(
 containsString("Welcome to...")));
// ...

and later this gets introduced:

package tacos.web;
import org.springframework.context.annotation.Configuration;
import
org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
 @Override
 public void addViewControllers(ViewControllerRegistry registry) {
 registry.addViewController("/").setViewName("home");
 }
}

and I can delete the old Controller.
but what do I know need to put in the @WebMvcTest argument?
before it was HomeController.class and now it should be empty?
or what?

mystic steepleBOT
#

<@&1004656351647117403> please have a look, thanks.

mystic steepleBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

mystic steepleBOT
#

Closed the thread due to inactivity.

If your question was not resolved yet, feel free to just post a message to reopen it, or create a new thread. But try to improve the quality of your question to make it easier to help you 👍

patent stump
#

..

patent stump
#

ok so i found out that WebMvcTest without argument loads all controller, is there a way to load a specific „controller“ which is initialized with WebMvcConfigurer