title: How do I unit test Spring Controller using annotations?
body: <p>I am new to the concept of unit testing with Spring controllers. I'm following some examples I found online and trying to implement their testing strategy. This is my basic controller:</p>
  
  <pre><code>@Controller
  public class GreetingController {
  
      @RequestMapping("/greeting")
      public String greeting(@RequestParam(value = "name2", required = false, defaultValue = "World2") String name2,
                             Model model) {
  
          model.addAttribute("name", name2);
          return "greeting";
      }
  
  }
  </code></pre>
  
  <p>This is my unit test:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration
  @WebAppConfiguration
  public class ControllerGreetingTest {
  
      private MockMvc mockMvc;
  
      @Autowired
      GreetingController greetingController;
      @Test
      public void shouldReturnSomething() throws Exception {
          mockMvc.perform(get("/greeting"))
                  .andExpect(status().isOk())
                  .andExpect(view().name("greeting"));
      }
  
  }
  </code></pre>
  
  <p>Seems pretty straight forward but I get the following error:</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Neither GenericXmlWebContextLoader
    nor AnnotationConfigWebContextLoader was able to detect defaults, and
    no ApplicationContextInitializers were declared for context
    configuration [ContextConfigurationAttributes@1698539 declaringClass =
    'com.practice.demo.ControllerGreetingTest', locations = '{}', classes
    = '{}', inheritLocations = true, initializers = '{}', inheritInitializers = true, name = [null], contextLoaderClass =
    'org.springframework.test.context.ContextLoader']</p>
  </blockquote>
  
  <p>I'm assuming I have to add a parameter to the <code>@ContextConfiguration</code> annotation but not sure what to include in there.</p>
  
  <p>EDIT = This is what I have so far:</p>
  
  <pre><code>public class ControllerGreetingTest {
  
      private MockMvc mockMvc;
  
     @Before
      public void setup(){
          this.mockMvc = standaloneSetup(new GreetingController()).build();
      }
  
      @Test
      public void shouldReturnDefaultString() throws Exception {
          mockMvc.perform(get("/greeting"))
                  .andExpect(status().isOk())
                  .andExpect(view().name("greetings"))
                  .andExpect(model().attribute("name","World2"));
      }
  
  }
  </code></pre>
  
  <p>It does the job but it doesn't use any of the Spring annotations like I tried to do before.. this approach is not good so trying to figure out why I keep gettings errors whenever I include the annotations in my test file.</p>
  
  <p>My POM:</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;version&gt;1.5.7.RELEASE&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
          &lt;optional&gt;true&lt;/optional&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
          &lt;artifactId&gt;hamcrest-all&lt;/artifactId&gt;
          &lt;version&gt;1.3&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
          &lt;exclusions&gt;
              &lt;exclusion&gt;
                  &lt;artifactId&gt;hamcrest-core&lt;/artifactId&gt;
                  &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
              &lt;/exclusion&gt;
          &lt;/exclusions&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.mockito&lt;/groupId&gt;
          &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
          &lt;version&gt;1.9.5&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
          &lt;version&gt;3.2.3.RELEASE&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.mongodb&lt;/groupId&gt;
              &lt;artifactId&gt;mongo-java-driver&lt;/artifactId&gt;
              &lt;version&gt;3.4.2&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-dependencies&lt;/artifactId&gt;
          &lt;version&gt;1.5.7.RELEASE&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  
  &lt;/dependencies&gt;
  </code></pre>