如何使用 springboot 应用程序绕过或跳过 Mockito 中的 CustomFilter [英] How to bypass or skip CustomFilter in Mockito with springboot applicaiton

查看:50
本文介绍了如何使用 springboot 应用程序绕过或跳过 Mockito 中的 CustomFilter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 springboot 应用程序和 mockito 进行测试.下面是一些文件和代码示例.

I am using springboot appllication and mockito for testing. So below are some files and code samples.

public class CustomerInfoFilter extends GenericFilterBean
{

    @Override
    public void doFilter (ServletRequest request,
                          ServletResponse response,
                          FilterChain chain)
        throws IOException,
        ServletException
    {
        Customer customer = (Customer)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        // some more logic 

        // call next filter in the filter chain
        chain.doFilter(request, response);
     }
}

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{

    @Autowired
    public void configAuthentication (AuthenticationManagerBuilder auth) throws Exception
    {
        auth.jdbcAuthentication()........... some logic
    }

    protected void configure (HttpSecurity http) throws Exception
    {
        http.addFilterAfter(new CustomerInfoFilter(customerInfoDao), BasicAuthenticationFilter.class);
        // Some logic
    }
}

下面是一段用 Mockito 测试编写的代码:

Below is piece of code written in Mockito test :

@Test
public void verifyCustomerInfoUnauthorized () throws Exception
{
    mockMvc.perform(MockMvcRequestBuilders.post("/customer").contentType(
    MediaType.APPLICATION_JSON).content("{}").accept(MediaType.APPLICATION_JSON)).andExpect(
            status().isUnauthorized()).andExpect(status().is(401));
}

  • 现在,正如您在 SecurityConfig 类中看到的,CustomerInfoFilter 将在 BasicAuthenticationFilter 之后调用.
  • 因为编写测试的方式失败了,因为它没有发送任何身份验证详细信息.
  • 还有一段代码:Customer customer =(Customer)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 因 NullpointerException 而失败,因为我们没有在课程中传递身份验证详细信息测试和 getAuthenticaiton() 将返回 null.

    问题:如何在 mockito 中跳过这个自定义过滤器.?换句话说,如何仅在测试期间禁用此自定义过滤器.?
    或任何其他解决方法或技巧.?
    • Now as you can see in SecurityConfig class CustomerInfoFilter will be called after BasicAuthenticationFilter.
    • Because the way that test is written is failing since it's not sending any authentication details.
    • And piece of code : Customer customer =(Customer)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); is failing with NullpointerException because off-course we are not passing authentication details in the test and getAuthenticaiton() will return null.

      Question: How can I skip this custom filter in mockito.? In other words how can disable this custom filter only during testing.?
      Or any other workaround or tricks.?
    • 抱歉,我是 spring 和 mockito 的新手 :) 任何帮助将不胜感激.

      Sorry I am new to spring and mockito :) any help will be appreciated.

      推荐答案

      @Mock
      SecurityContext context;
      
      @Mock
      Authentication auth;
      
      
      @Mock
      Principal principal;
      
      @Test
      public void verifyCustomerInfoUnauthorized () throws Exception
      {
      
          when(context.getAuthentication()).thenReturn(auth);
          when(context.getAuthentication().getPrincipal()).thenReturn(principal);
          SecurityContextHolder.setContext(context);
          mockMvc.perform(MockMvcRequestBuilders.post("/customer").principal().contentType(
          MediaType.APPLICATION_JSON).content("{}").accept(MediaType.APPLICATION_JSON)).andExpect(
              status().isUnauthorized()).andExpect(status().is(401));
      }
      

      您可以执行上述操作或直接在测试方法中设置模拟.无论哪种方式,它都应该可以解决问题.最重要的部分是 .setContext() 部分.那就是您的空指针的来源.

      You can do something like above or set the mocks directly in the test method. Either way it should do the trick. The most important piece is the .setContext() piece. That's where your null pointer is coming from.

      我发现这是最干净的方法.

      I find this is the cleanest way to go about it.

      这篇关于如何使用 springboot 应用程序绕过或跳过 Mockito 中的 CustomFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆