Skip to content

Instantly share code, notes, and snippets.

@rieckpil
Last active April 30, 2022 15:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rieckpil/dc840d894d24f50db5865691ad4864aa to your computer and use it in GitHub Desktop.
Save rieckpil/dc840d894d24f50db5865691ad4864aa to your computer and use it in GitHub Desktop.
Intellij IDEA Live Template Examples

Live Template Examples for Testing Java Applications

Create new Live Templates within your IDEA's Preferences -> Editor -> Live Templates

mockMvcGET: Peform a MockMvc HTTP GET request.

org.springframework.test.web.servlet.MvcResult result = this.mockMvc
  .perform(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get($url$)
  .header(org.springframework.http.HttpHeaders.ACCEPT, org.springframework.http.MediaType.APPLICATION_JSON))
  .andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status().is(200))
  .andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.content().contentType(org.springframework.http.MediaType.APPLICATION_JSON))
  .andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath("$.size()", org.hamcrest.Matchers.is(0)))
  .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
  .andReturn();

mockMvcPOST: Peform a MockMvc HTTP POST request.

this.mockMvc
  .perform(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post($url$)
    .contentType(org.springframework.http.MediaType.APPLICATION_JSON)
    .content("""
       {
       }
      """)
  )
  .andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status().isCreated());

test: JUnit 5 with AssertJ test skeleton.

@org.junit.jupiter.api.Test
void $testName$() throws Exception {

  org.assertj.core.api.Assertions.assertThat(result)
    .isEqualTo("");
}

testAAA: Test skeleton with arrange, act, and assert blocks.

@org.junit.jupiter.api.Test
void $testName$() throws Exception {
  // arrange
    
  // act

  // assert
  org.assertj.core.api.Assertions.assertThat(result)
    .isEqualTo("");
}

For further inspiration, take a look at Matt Raible's Live Template collection on GitHub and make sure to post your favorite templates as a comment 👇🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment