Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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