应用程序未能启动(Spring MockMvc @WebMvcTest)

时间:2017-05-30 22:59:19

标签: java spring-mvc spring-boot junit mockmvc

大家。我是WebMvcTest的新手,正在学习编写PostControllerTest。虽然项目运行良好,但测试不起作用。

2017-05-31 10:08:09.490  INFO 5768 --- [           main] nz.p2.controller.PostControllerTest      : Starting PostControllerTest on My-PC with PID 5768 (started by Me in C:\...)
2017-05-31 10:08:09.491  INFO 5768 --- [           main] nz.p2.controller.PostControllerTest      : No active profile set, falling back to default profiles: default
2017-05-31 10:08:10.989  INFO 5768 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Refreshing org.springframework.web.context.support.GenericWebApplicationContext@2a8d39c4: startup date [Wed May 31 10:08:10 NZST 2017]; root of context hierarchy
2017-05-31 10:08:12.788  INFO 5768 --- [           main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-05-31 10:08:13.311  WARN 5768 --- [           main] o.s.w.c.s.GenericWebApplicationContext   : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'reCaptchaAuthenticationFilter': Unsatisfied dependency expressed through field 'reCaptchaService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'nz.p2.captcha.ReCaptchaService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-05-31 10:08:13.326  INFO 5768 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-05-31 10:08:13.534 ERROR 5768 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field reCaptchaService in nz.p2.captcha.ReCaptchaAuthenticationFilter required a bean of type 'nz.p2.captcha.ReCaptchaService' that could not be found.


Action:

Consider defining a bean of type 'nz.p2.captcha.ReCaptchaService' in your configuration.

它告诉我用ReCaptchaService做一些事情;它不与PostController一起使用。鉴于简化的Controller类:

@Controller
public class PostController {
  @Autowired
  private PostService postService;

  @RequestMapping(value = URI_POST_POST, method = RequestMethod.GET)
  public ModelAndView get(@Valid Long mkid) throws IOException {

    // do somthing here using postService;

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName(URI_POST_POST);
    modelAndView.addObject("abc", abc);
    return modelAndView;
  }
}

PostControllerTest类在这里:

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = PostController.class)
public class PostControllerTest {  
  private MockMvc mockMvc;

  @Autowired
  private WebApplicationContext webApplicationContext;

  @MockBean
  private PostController postControllereMock;  

  @Before
  public void setUp() throws Exception {
    mockMvc = MockMvcBuilders.standaloneSetup(webApplicationContext).build();    
  }

  @Test
  public void testList() throws Exception {
    assertThat(this.postControllereMock).isNotNull();
    mockMvc.perform(MockMvcRequestBuilders.get(URI_POST_POST + "?mkid=8044022272730561994"))
            .andExpect(status().isOk())
            .andExpect(content().contentType("text/html;charset=UTF-8"))
            .andExpect(view().name(URI_POST_POST))
            .andExpect(MockMvcResultMatchers.view().name(URI_POST_POST))
            .andExpect(content().string(Matchers.containsString("I have put together some image galleries")))
            .andDo(print());
    }
}

我必须提到reCaptchaService在ReCaptchaAuthenticationFilter中是@Autowired,而后者是@Autowired并在这里使用:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.
        // tl; nw
        .and().csrf().disable()
        .addFilterBefore(reCaptchaAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
        // tl; nw
  }
}

那么,在这种情况下,我该如何测试PostController?

0 个答案:

没有答案