/*
 * Copyright 2002-2015 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sambrannen.spring.events;

import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;

import java.util.Collections;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.junit4.rules.SpringClassRule;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.sambrannen.spring.events.domain.Event;
import com.sambrannen.spring.events.repository.EventRepository;
import com.sambrannen.spring.events.service.EventService;
import com.sambrannen.spring.events.service.StandardEventService;
import com.sambrannen.spring.events.web.EventsController;

/**
 * @author Nicolas Frankel
 * @author Sam Brannen
 * @since 1.0
 */
@RunWith(MockitoJUnitRunner.class)
@SpringApplicationConfiguration
@WebAppConfiguration
public class EventsControllerTests {

    @ClassRule
    public static final SpringClassRule SPRING_RULE = new SpringClassRule();

    @Rule
    public final SpringMethodRule springRule = new SpringMethodRule();

    @Autowired
	WebApplicationContext wac;

	MockMvc mockMvc;

    @Before
    public void setUp() {
        mockMvc = webAppContextSetup(wac).build();
    }

    @Test
    public void shouldDisplayRepositoryItemsInitially() throws Exception {
        mockMvc.perform(get("/"))
            .andExpect(view().name("event/list"))
            .andExpect(model().attribute("events", hasSize(1)));
    }


	@Configuration
	@EnableWebMvc
	static class TestConfiguration {

		@Bean
		public EventRepository eventRepository() {
			EventRepository repository = Mockito.mock(EventRepository.class);
			Mockito.when(repository.findAll()).thenReturn(Collections.singletonList(new Event(1L)));
			return repository;
		}

		@Bean
		public EventService eventService() {
			return new StandardEventService(eventRepository());
		}

		@Bean
		public EventsController eventsController() {
			return new EventsController(eventService());
		}
	}

}

@@ -658,292 +658,8 @@
 s;%0A%0A
-import static org.hamcrest.Matchers.*;%0Aimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;%0Aimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;%0Aimport static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;%0A%0A
 impo
@@ -684,17 +684,16 @@
 ctions;%0A
-%0A
 import o
@@ -839,19 +839,16 @@
 ito.Mock
-ito
 ;%0Aimport
@@ -1331,32 +1331,90 @@
 pConfiguration;%0A
+import org.springframework.test.util.ReflectionTestUtils;%0A
 import org.sprin
@@ -1583,17 +1583,16 @@
 WebMvc;%0A
-%0A
 import c
@@ -1637,130 +1637,8 @@
 nt;%0A
-import com.sambrannen.spring.events.repository.EventRepository;%0Aimport com.sambrannen.spring.events.service.EventService;%0A
 impo
@@ -1758,16 +1758,456 @@
 oller;%0A%0A
+import static org.hamcrest.Matchers.hasSize;%0Aimport static org.mockito.Mockito.when;%0Aimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;%0Aimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;%0Aimport static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;%0Aimport static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;%0A%0A%0A
 /**%0A * @
@@ -2587,16 +2587,24 @@
 owired%0A%09
+private 
 WebAppli
@@ -2623,17 +2623,28 @@
 t wac;%0A%0A
-%09
+    private 
 MockMvc 
@@ -2649,24 +2649,83 @@
 c mockMvc;%0A%0A
+    @Mock%0A    private StandardEventService eventService;%0A%0A%0A
     @Before%0A
@@ -2801,16 +2801,257 @@
 uild();%0A
+        EventsController controller = wac.getBean(EventsController.class);%0A        ReflectionTestUtils.setField(controller, %22service%22, eventService);%0A        when(eventService.findAll()).thenReturn(Collections.singletonList(new Event(1L)));%0A
     %7D%0A%0A 
@@ -3290,18 +3290,20 @@
 %0A    %7D%0A%0A
-%0A%09
+    
 @Configu
@@ -3309,17 +3309,20 @@
 uration%0A
-%09
+    
 @EnableW
@@ -3327,17 +3327,20 @@
 eWebMvc%0A
-%09
+    
 static c
@@ -3369,366 +3369,30 @@
  %7B%0A%0A
-%09%09@Bean%0A%09%09public EventRepository eventRepository() %7B%0A%09%09%09EventRepository repository = Mockito.mock(EventRepository.class);%0A%09%09%09Mockito.when(repository.findAll()).thenReturn(Collections.singletonList(new Event(1L)));%0A%09%09%09return repository;%0A%09%09%7D%0A%0A%09%09@Bean%0A%09%09public EventService eventService() %7B%0A%09%09%09return new StandardEventService(eventRepository());%0A%09%09%7D%0A%0A%09%09@Bean%0A%09%09
+        @Bean%0A        
 publ
@@ -3436,11 +3436,20 @@
 ) %7B%0A
-%09%09%09
+            
 retu
@@ -3476,31 +3476,29 @@
 ler(
-eventService());%0A%09%09%7D%0A%09%7D%0A
+null);%0A        %7D%0A    %7D
 %0A%7D%0A

Fix the Mockito JUnit runner usage