【问题标题】:Spring Boot 2 JUnit Test for rest api failed at 'jwtAuthenticationEntryPoint':REST API 的 Spring Boot 2 JUnit 测试在“jwtAuthenticationEntryPoint”失败:
【发布时间】:2019-05-26 07:29:37
【问题描述】:

我正在为 Spring Boot 服务编写 Junit 测试,用于测试 api 调用,但它未能运行抛出以下异常

Caused by:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'resourceServerConfig': Unsatisfied dependency expressed through field 'entryPoint'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'jwtAuthenticationEntryPoint': Requested bean is currently in creation: Is there an unresolvable circular reference?
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)

Junit 测试:

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = Application.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public class ServiceTest {

  @Autowired
    protected WebApplicationContext wac;

    protected MockMvc mockMvc;

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

        @Test
        public void validateApiCall() throws Exception {
            String token = "Bearer xxxxxxxx";   
         mockMvc.perform(MockMvcRequestBuilders.get("http://localhost::8060/api").header("Authorization", token)).andExpect(status().isOk());


        }
}

【问题讨论】:

    标签: spring spring-boot spring-security spring-boot-test


    【解决方案1】:

    我可以通过这种方式自动装配javax.servlet.Filter[] 来修复它

            @RunWith(SpringRunner.class)
            @SpringBootTest
            @ContextConfiguration(classes = Application.class)
            @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
            public class ServiceTest {
    
              @Autowired
              protected WebApplicationContext wac;
    
              @Autowired
              private javax.servlet.Filter[] springSecurityFilterChain;
    
              @MockBean private JwtAuthenticationEntryPoint jwt;
    
              protected MockMvc mockMvc;
    
              @Before
              public void setup() {
                  MockitoAnnotations.initMocks(this);
                  mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build();
                }
    
                    @Test
                    public void validateApiCall() throws Exception {
                        String token = "Bearer xxxxxxxx";   
    
     mockMvc.perform(MockMvcRequestBuilders.get("http://localhost::8060/api").header("Authorization", token)).andExpect(status().isOk());
    
    
                    }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-04
      • 2023-03-25
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2017-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多