1

I am experimenting MockMvc of Spring framework. To call perform method of MockMvc I would need to have an import as following

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

Partial code

 this.mockMvc.perform(get("/")).andExpect(view().name("homePage"));

I know get method is static, but it is the first time that I see import has static keyword. Is anyone able to explain this to me? why static keyword is required? why just method is imported? I am a bit confused with this.

Jack
  • 6,430
  • 27
  • 80
  • 151
  • [An almost identical question](https://stackoverflow.com/questions/14209169/static-import-in-java) got closed in the past. Though I don't really know why. (It has good answers nevertheless.) – 5gon12eder Sep 21 '15 at 23:06
  • 2
    possible duplicate of [What does the "static" modifier after "import" mean?](http://stackoverflow.com/questions/162187/what-does-the-static-modifier-after-import-mean) – zapl Sep 21 '15 at 23:08
  • it's shorter then writing `this.mockMvc.perform(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get("/"))` with no import :) – zapl Sep 21 '15 at 23:09
  • @zapl Question in Jan '13 gets closed as “not a real question”, question in Jul '13 scores +141. I see… – 5gon12eder Sep 21 '15 at 23:11
  • @5gon12eder yeah, it's rather arbitrary sometimes. Especially short questions with short answers – zapl Sep 21 '15 at 23:21

1 Answers1

3

It's importing the static get() method in the MockMvcRequestBuilders class. This means you can call it directly (without doing MockMvcRequestBuilders.get().)

This applies to any static method or field in another class - it's not specifically a spring / MockMvc thing. Likewise you don't need the import per-se, it just provides a shorthand notation.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216