【问题标题】:jUnit: No primary or default constructor found for interface com.querydsl.core.types.PredicatejUnit:没有找到接口 com.querydsl.core.types.Predicate 的主要或默认构造函数
【发布时间】:2018-11-13 17:28:38
【问题描述】:

我在 spring-application 中有一个 restcontroller 返回一个对象列表...

@GetMapping
@Override
public ResponseEntity readAll(@QuerydslPredicate(root = Entity.class) Predicate predicate, Pageable pageable){
    ...
}

如果我运行它,一切正常。我可以通过可分页和谓词过滤请求。但是如果我运行 junit 测试,它会失败......

@Test
public void readAllTest(){
MockMvcBuilders.standaloneSetup(*myController*)
            .build().perform(MockMvcRequestBuilders.get(*myUri*)
                    .accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
            )
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
}

收到以下错误消息... org.springframework.web.util.NestedServletException:请求处理失败;嵌套异常是 java.lang.IllegalStateException: No primary or default constructor found for interface com.querydsl.core.types.Predicate

有人知道如何使用 Pageable 和 Predicate 测试 restcontroller 吗?

【问题讨论】:

    标签: spring junit mockito querydsl


    【解决方案1】:

    如果有人不仅在测试中遇到了这个问题,而且在正常的应用程序运行中也遇到了这个问题,这可能是由于 Spring Boot 没有配置 Web 应用程序。例如我的项目有一个SwaggerConfig 扩展WebMvcConfigurationSupport

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig extends WebMvcConfigurationSupport {
        // Docket bean
        
        @Override
        protected void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        }
    }
    

    我删除了继承和手动资源处理程序,现在可以正常工作了。

    注意:除了WebMvcConfigurationSupport@EnableWebMvc & WebMvcConfigurer 之类的东西也可能导致 Spring Boot 的 web 自动配置不被使用。

    来源:Another SO AnswerSwagger Issue comment

    【讨论】:

      【解决方案2】:

      现在,你只需要使用 ApplicationContext 设置 mockMvc,比如

      @RunWith(SpringRunner.class)
      @SpringBootTest
      public class ControllerTest {
      
        @Autowired
        private WebApplicationContext webApplicationContext;
      
        @Test
        public void readAllTest() throws Exception {
          MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build()
              .perform(MockMvcRequestBuilders.get("YOUR_URI")
              .accept(MediaType.APPLICATION_JSON_UTF8_VALUE))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
        }
      
      }
      

      https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#spring-mvc-test-server

      【讨论】:

        【解决方案3】:
        1. 尝试添加测试类注释@Import(QuerydslWebConfiguration.class)。它将 com.querydsl.core.types.Predicate 的控制器参数解析器添加到 spring 上下文中。

        2. 但是在你遇到这样的异常之后:

          没有找到接口 org.springframework.data.domain.Pageable 的主要或默认构造函数。

        3. 有注释,为这两个接口加载参数解析器。 org.springframework.data.web.config.EnableSpringDataWebSupport

        适用于您的测试类:

        @RunWith(SpringRunner.class)
        @WebMvcTest(*myController*.class)
        @EnableSpringDataWebSupport
        public class ControllerTest{
        
          @Test
          public void readAllTest(){
            MockMvcBuilders.standaloneSetup(*myController*)
                    .build().perform(MockMvcRequestBuilders.get(*myUri*)
                            .accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
                    )
                    .andExpect(status().isOk())
                    .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
          }
        }
        

        【讨论】:

          猜你喜欢
          • 2020-11-17
          • 2019-02-20
          • 2018-08-16
          • 2019-06-23
          • 2019-12-18
          • 2019-07-06
          • 2014-07-16
          • 2022-01-18
          • 2012-03-06
          相关资源
          最近更新 更多