#What am I supposed to learn/memorize from examples like this one? (Spring boot in action)

1 messages · Page 1 of 1 (latest)

sharp timber
#

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? 😭

ionic bayBOT
# sharp timber I'm revisiting this book (completed Spring start here a few days ago) and I ran ...

Detected code, here are some useful tools:

Formatted 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...")));
  }
}
#

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

ionic bayBOT
#

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.