I'm revisiting this book (completed Spring start here a few days ago) and I ran into this code (author's code):
package com.thanos.freshtacos;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@WebMvcTest
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 all I'm wondering is "what am I supposed to learn here?". There's so many things and I have so many questions, things like "how does he know what to import and from where", "why use status().isOk()", "why does he care about the view name being home if he checks the page for string", and "what even is MockMvc?!"
If I had to work through my own side project and wrote my own tests, and somehow ended up like that I'd feel uncomfortable thinking that I'm doing something terribly wrong (is this even right?).
As a self-taught, what am I focusing to understand here? The motivations behind testing the endpoint, the status, the view name (isn't that an implementation detail?), or what is MockMvc? Or all of the above? 😭