Junit mockito 测试Controller层方法有Pageable异常

1、问题

  在使用MockMVC+Mockito模拟Service层返回的时候,当我们在Controller层中参数方法调用有Pageable对象的时候,我们会发现,我们没办法生成一个Pageable的对象,会报一个Pageable是一个接口的错误。当我们把所有的参数从Pageable接口变成Pageable的实现类PageRequest的时候,所有的方法参数都换成PageRequest,又会出现一个新的错误,且不说PageRequest不能作为参数用于hibernate的分页查询,另一方面,它没有构造方法,也无法初始化。

2、代码

1)controller代码,主要是

@RequestMapping("/banners")
    public DataMap showBanners(BannerQuery bannerQuery, @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
        Page<Banner> page = bannerService.findPage(bannerQuery, pageable);
        return new DataMap().addAttribute(Constants.STATUS_CODE, StatusCode.SUCCESS.getStatusCode())
                .addAttribute(Constants.DATA, page);
    }

2)测试方法:

@Test
    public void showBannersWithWhenTest() throws Exception {

        Banner banner = new Banner();
        banner.setBannerUrl("www.baidu.com");
        banner.setActionUrl("hhtp");
        List<Banner> list = new ArrayList<>();
        list.add(banner);
        Page<Banner> page = new PageImpl<Banner>(list);

        when(bannerService.findPage(isA(BannerQuery.class),isA(Pageable.class))).thenReturn(page);     //mock绑定了放回
        //生成一个发送请求的类
        RequestBuilder rb = get("/banners")  //请求的url
                .param("page","0")           //请求的参数
                .param("size","5")
                .contentType(MediaType.APPLICATION_FORM_URLENCODED);

        String responseString = mockMvc.perform(rb)
                .andDo(print())                                     //添加ResultHandler结果处理器,比如调试时打印结果到控制台
               /* .andExpect(jsonPath("$.data.content").isArray())    //判断返回的数据是否是一个数组*/
                .andExpect(status().isOk())                         //判断返回的状态是否为200
                /*.andExpect(jsonPath("$.data.content",hasSize(5)))   //判断返回的json对象的data.content属性的大小是否为5
                //判断返回的data.content中的第一个对象的uid属性是否为所给值
                .andExpect(jsonPath("$.data.content[0].uid",is("81377051ddb6424fa677931ab1bc6496")))
                .andExpect(jsonPath("$.data.content[1].uid",is("8cb7b04fdea846d2ab6006fa1280a8b8")))*/
                .andReturn().getResponse().getContentAsString();

        System.out.println("--------showCategorysTest()返回的json = " + responseString);

    }

3)报错:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.domain.Pageable]: Specified class is an interface

全部内容

/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:59469,suspend=y,server=n -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath "/Applications/IntelliJ IDEA 15.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA 15.app/Contents/plugins/junit/lib/junit-rt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/htmlconverter.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/javafx-doclet.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/lib/tools.jar:/Users/zhengcanrui/WORK/git/seewo-product/site-server/target/test-classes:/Users/zhengcanrui/WORK/git/seewo-product/site-server/target/classes:/Users/zhengcanrui/.m2/repository/com/seewo/web-core/2.0-SNAPSHOT/web-core-2.0-20160520.065933-15.jar:/Users/zhengcanrui/.m2/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar:/Users/zhengcanrui/.m2/repository/commons-logging/commons-logging/1.1.3/commons-logging-1.1.3.jar:/Users/zhengcanrui/.m2/repository/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar:/Users/zhengcanrui/.m2/repository/commons-codec/commons-codec/1.8/commons-codec-1.8.jar:/Users/zhengcanrui/.m2/repository/commons-fileupload/commons-fileupload/1.3/commons-fileupload-1.3.jar:/Users/zhengcanrui/.m2/repository/commons-io/commons-io/2.2/commons-io-2.2.jar:/Users/zhengcanrui/.m2/repository/commons-beanutils/commons-beanutils/1.9.2/commons-beanutils-1.9.2.jar:/Users/zhengcanrui/.m2/repository/org/apache/commons/commons-pool2/2.3/commons-pool2-2.3.jar:/Users/zhengcanrui/.m2/repository/org/hibernate/hibernate-entitymanager/4.3.7.Final/hibernate-entitymanager-4.3.7.Final.jar:/Users/zhengcanrui/.m2/repository/org/jboss/logging/jboss-logging/3.1.3.GA/jboss-logging-3.1.3.GA.jar:/Users/zhengcanrui/.m2/repository/org/jboss/logging/jboss-logging-annotations/1.2.0.Beta1/jboss-logging-annotations-1.2.0.Beta1.jar:/Users/zhengcanrui/.m2/repository/org/hibernate/hibernate-core/4.3.7.Final/hibernate-core-4.3.7.Final.jar:/Users/zhengcanrui/.m2/repository/antlr/antlr/2.7.7/antlr-2.7.7.jar:/Users/zhengcanrui/.m2/repository/org/jboss/jandex/1.1.0.Final/jandex-1.1.0.Final.jar:/Users/zhengcanrui/.m2/repository/dom4j/dom4j/1.6.1/dom4j-1.6.1.jar:/Users/zhengcanrui/.m2/repository/xml-apis/xml-apis/1.0.b2/xml-apis-1.0.b2.jar:/Users/zhengcanrui/.m2/repository/org/hibernate/common/hibernate-commons-annotations/4.0.5.Final/hibernate-commons-annotations-4.0.5.Final.jar:/Users/zhengcanrui/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.1-api/1.0.0.Final/hibernate-jpa-2.1-api-1.0.0.Final.jar:/Users/zhengcanrui/.m2/repository/org/jboss/spec/javax/transaction/jboss-transaction-api_1.2_spec/1.0.0.Final/jboss-transaction-api_1.2_spec-1.0.0.Final.jar:/Users/zhengcanrui/.m2/repository/org/javassist/javassist/3.18.1-GA/javassist-3.18.1-GA.jar:/Users/zhengcanrui/.m2/repository/org/hibernate/hibernate-ehcache/4.3.7.Final/hibernate-ehcache-4.3.7.Final.jar:/Users/zhengcanrui/.m2/repository/net/sf/ehcache/ehcache-core/2.4.3/ehcache-core-2.4.3.jar:/Users/zhengcanrui/.m2/repository/org/hibernate/hibernate-validator/5.0.3.Final/hibernate-validator-5.0.3.Final.jar:/Users/zhengcanrui/.m2/repository/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar:/Users/zhengcanrui/.m2/repository/com/fasterxml/classmate/1.0.0/classmate-1.0.0.jar:/Users/zhengcanrui/.m2/repository/org/freemarker/freemarker/2.3.22/freemarker-2.3.22.jar:/Users/zhengcanrui/.m2/repository/javax/mail/mail/1.4.7/mail-1.4.7.jar:/Users/zhengcanrui/.m2/repository/javax/activation/activation/1.1/activation-1.1.jar:/Users/zhengcanrui/.m2/repository/org/aspectj/aspectjrt/1.8.4/aspectjrt-1.8.4.jar:/Users/zhengcanrui/.m2/repository/org/aspectj/aspectjweaver/1.8.4/aspectjweaver-1.8.4.jar:/Users/zhengcanrui/.m2/repository/org/slf4j/slf4j-api/1.7.6/slf4j-api-1.7.6.jar:/Users/zhengcanrui/.m2/repository/org/slf4j/slf4j-log4j12/1.7.6/slf4j-log4j12-1.7.6.jar:/Users/zhengcanrui/.m2/repository/log4j/log4j/1.2.17/log4j-1.2.17.jar:/Users/zhengcanrui/.m2/repository/org/slf4j/jul-to-slf4j/1.7.6/jul-to-slf4j-1.7.6.jar:/Users/zhengcanrui/.m2/repository/org/slf4j/jcl-over-slf4j/1.7.6/jcl-over-slf4j-1.7.6.jar:/Users/zhengcanrui/.m2/repository/com/alibaba/fastjson/1.2.3/fastjson-1.2.3.jar:/Users/zhengcanrui/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.5.0/jackson-databind-2.5.0.jar:/Users/zhengcanrui/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.5.0/jackson-core-2.5.0.jar:/Users/zhengcanrui/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.5.0/jackson-annotations-2.5.0.jar:/Users/zhengcanrui/.m2/repository/org/apache/shiro/shiro-core/1.2.2/shiro-core-1.2.2.jar:/Users/zhengcanrui/.m2/repository/org/apache/shiro/shiro-web/1.2.2/shiro-web-1.2.2.jar:/Users/zhengcanrui/.m2/repository/com/cvte/myou/module-monitor-sdk/2.0-SNAPSHOT/module-monitor-sdk-2.0-20151009.122048-4.jar:/Users/zhengcanrui/.m2/repository/org/apache/httpcomponents/httpcore/4.3.2/httpcore-4.3.2.jar:/Users/zhengcanrui/.m2/repository/org/apache/httpcomponents/httpclient/4.3.6/httpclient-4.3.6.jar:/Users/zhengcanrui/.m2/repository/mysql/mysql-connector-java/5.1.18/mysql-connector-java-5.1.18.jar:/Users/zhengcanrui/.m2/repository/org/springframework/spring-core/4.1.2.RELEASE/spring-core-4.1.2.RELEASE.jar:/Users/zhengcanrui/.m2/repository/org/springframework/spring-context-support/4.1.2.RELEASE/spring-context-support-4.1.2.RELEASE.jar:/Users/zhengcanrui/.m2/repository/org/springframework/spring-beans/4.1.2.RELEASE/spring-beans-4.1.2.RELEASE.jar:/Users/zhengcanrui/.m2/repository/org/springframework/spring-context/4.1.2.RELEASE/spring-context-4.1.2.RELEASE.jar:/Users/zhengcanrui/.m2/repository/org/springframework/spring-web/4.1.2.RELEASE/spring-web-4.1.2.RELEASE.jar:/Users/zhengcanrui/.m2/repository/org/springframework/spring-aop/4.1.2.RELEASE/spring-aop-4.1.2.RELEASE.jar:/Users/zhengcanrui/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar:/Users/zhengcanrui/.m2/repository/org/springframework/spring-tx/4.1.2.RELEASE/spring-tx-4.1.2.RELEASE.jar:/Users/zhengcanrui/.m2/repository/org/springframework/spring-webmvc/4.1.2.RELEASE/spring-webmvc-4.1.2.RELEASE.jar:/Users/zhengcanrui/.m2/repository/org/springframework/spring-expression/4.1.2.RELEASE/spring-expression-4.1.2.RELEASE.jar:/Users/zhengcanrui/.m2/repository/org/springframework/data/spring-data-jpa/1.7.1.RELEASE/spring-data-jpa-1.7.1.RELEASE.jar:/Users/zhengcanrui/.m2/repository/org/springframework/data/spring-data-commons/1.9.1.RELEASE/spring-data-commons-1.9.1.RELEASE.jar:/Users/zhengcanrui/.m2/repository/org/springframework/spring-orm/4.0.7.RELEASE/spring-orm-4.0.7.RELEASE.jar:/Users/zhengcanrui/.m2/repository/org/springframework/spring-jdbc/4.1.2.RELEASE/spring-jdbc-4.1.2.RELEASE.jar:/Users/zhengcanrui/.m2/repository/javax/servlet/jstl/1.2/jstl-1.2.jar:/Users/zhengcanrui/.m2/repository/javax/servlet/javax.servlet-api/3.0.1/javax.servlet-api-3.0.1.jar:/Users/zhengcanrui/.m2/repository/com/alibaba/druid/1.0.13/druid-1.0.13.jar:/Users/zhengcanrui/.m2/repository/axis/axis/1.4/axis-1.4.jar:/Users/zhengcanrui/.m2/repository/org/apache/axis/axis-jaxrpc/1.4/axis-jaxrpc-1.4.jar:/Users/zhengcanrui/.m2/repository/org/apache/axis/axis-saaj/1.4/axis-saaj-1.4.jar:/Users/zhengcanrui/.m2/repository/axis/axis-wsdl4j/1.5.1/axis-wsdl4j-1.5.1.jar:/Users/zhengcanrui/.m2/repository/commons-discovery/commons-discovery/0.2/commons-discovery-0.2.jar:/Users/zhengcanrui/.m2/repository/org/quartz-scheduler/quartz/2.2.1/quartz-2.2.1.jar:/Users/zhengcanrui/.m2/repository/c3p0/c3p0/0.9.1.1/c3p0-0.9.1.1.jar:/Users/zhengcanrui/.m2/repository/org/hamcrest/hamcrest-all/1.3/hamcrest-all-1.3.jar:/Users/zhengcanrui/.m2/repository/junit/junit/4.11/junit-4.11.jar:/Users/zhengcanrui/.m2/repository/org/mockito/mockito-core/1.9.5/mockito-core-1.9.5.jar:/Users/zhengcanrui/.m2/repository/org/hamcrest/hamcrest-core/1.1/hamcrest-core-1.1.jar:/Users/zhengcanrui/.m2/repository/org/objenesis/objenesis/1.0/objenesis-1.0.jar:/Users/zhengcanrui/.m2/repository/org/springframework/spring-test/3.2.3.RELEASE/spring-test-3.2.3.RELEASE.jar:/Users/zhengcanrui/.m2/repository/com/jayway/jsonpath/json-path/0.8.1/json-path-0.8.1.jar:/Users/zhengcanrui/.m2/repository/net/minidev/json-smart/1.1.1/json-smart-1.1.1.jar:/Users/zhengcanrui/.m2/repository/com/jayway/jsonpath/json-path-assert/0.8.1/json-path-assert-0.8.1.jar:/Users/zhengcanrui/.m2/repository/org/hamcrest/hamcrest-library/1.2.1/hamcrest-library-1.2.1.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.seewo.site.controller.BannerControllerUseWhenTest,showBannersWithWhenTest
Connected to the target VM, address: ‘127.0.0.1:59469‘, transport: ‘socket‘

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.domain.Pageable]: Specified class is an interface

    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:66)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:168)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:136)
    at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:134)
    at com.seewo.site.controller.BannerControllerUseWhenTest.showBannersWithWhenTest(BannerControllerUseWhenTest.java:146)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.domain.Pageable]: Specified class is an interface
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:101)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:139)
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:79)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:105)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    ... 37 more

Disconnected from the target VM, address: ‘127.0.0.1:59469‘, transport: ‘socket‘

Process finished with exit code 255

3、解决方法:

  报错的原因:没有Pageable的解析器。

  解决的方法:给他设置一个解析器

1)使用匿名内部类

@Before()
    public void setup() {
        MockitoAnnotations.initMocks(this);

        mockMvc = MockMvcBuilders.standaloneSetup(bannerController).setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
                .setViewResolvers(new ViewResolver() {
                    @Override
                    public View resolveViewName(String viewName, Locale locale) throws Exception {
                        return new MappingJackson2JsonView();
                    }
                } )
                .build();
    }

2)通过注解的方式注入一个PageableHandlerMethodArgumentResolver 解析器


@InjectMocksprivate PageableHandlerMethodArgumentResolver pageableArgumentResolver;
@Before()
    public void setup() {
        MockitoAnnotations.initMocks(this);

        mockMvc = MockMvcBuilders.standaloneSetup(bannerController).setCustomArgumentResolvers(pageableArgumentResolver)
                .build();
    }

解决方法1:http://stackoverflow.com/questions/22174665/isolated-controller-test-cant-instantiate-pageable

解决方法2:http://stackoverflow.com/questions/30057262/failed-to-instantiate-pageable-bean

  致谢:感谢您的阅读!

时间: 2024-10-15 13:34:31

Junit mockito 测试Controller层方法有Pageable异常的相关文章

SpringBoot测试Controller层

一.准备工作 1.导入测试依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> 2.Controller层: @RestController("/") public class UserController { @Autow

代理模式——用AOP测试业务层方法的执行时间

代理模式 对代理模式的理解,通过http://www.runoob.com/design-pattern/proxy-pattern.html 对AOP的代理模式,参考https://www.cnblogs.com/xujiming/p/5737531.html 目标:测试service层每一个方法的执行时间: 代理模式的本质:将被代理对象注入给代理对象,当调用代理对象时,实际上,是在调用被注入的被代理对象 理解: 代理对象 被代理对象 代理类 被代理对象 商家 厂商 房屋中介 房东 静态代理:

junit参数化测试的使用方法

JUnit参数化测试的五个步骤:(1)为准备使用参数化测试的测试类指定特殊的运行器 org.junit.runners.Parameterized.(2)为测试类声明几个变量,分别用于存放期望值和测试所用数据.(3)为测试类声明一个带有参数的公共构造函数,并在其中为第二个环节中声明的几个变量赋值.(4)为测试类声明一个使用注解 org.junit.runners.Parameterized.Parameters 修饰的,返回值为 java.util.Collection 的公共静态方法,并在此方

junit基础学习之-测试controller层(2)

准备工作: eclipse本身带有junit4,可以直接build path,加入junit. 连接数据库的配置文件需要修改,之前的文件是采用properties+xml文件的形式,但是在测试的时候因为不能连接服务器,所以需要需要修改配置文件,最好是重新写一个配置文件,把需要的参数直接写出来就好了. 下面是我配置文件,重点是前面,后面就不需要了. <?xml version="1.0" encoding="UTF-8"?> <beans xmlns

Spring MVC如何测试Controller(使用springmvc mock测试)

在springmvc中一般的测试用例都是测试service层,今天我来演示下如何使用springmvc mock直接测试controller层代码. 1.什么是mock测试? mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法. 2.为什么要使用mock测试? 使用Mock Object进行测试,主要是用来模拟那些在应用中不容易构造(如HttpServletRequest必须在Servlet容器中才能构造出来)或者比较复杂的对象(如JD

【统一异常处理】@ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常

1.利用springmvc注解对Controller层异常全局处理 对于与数据库相关的 Spring MVC 项目,我们通常会把 事务 配置在 Service层,当数据库操作失败时让 Service 层抛出运行时异常,Spring 事物管理器就会进行回滚. 如此一来,我们的 Controller 层就不得不进行 try-catch Service 层的异常,否则会返回一些不友好的错误信息到客户端.但是,Controller 层每个方法体都写一些模板化的 try-catch 的代码,很难看也难维护

@ControllerAdvice + @ExceptionHandler全局处理Controller层异常(转)

0.前言 对于与数据库相关的Spring MVC项目,我们通常会把事务配置在 Service层,当数据库操作失败时让Service层抛出运行时异常,Spring事物管理器就会进行回滚. 如此一来,我们的Controller层就不得不进行try-catch Service层的异常,否则会返回一些不友好的错误信息到客户端.但是,Controller层每个方法体都写一些模板化的try-catch的代码,很难看也难维护,特别是还需要对Service层的不同异常进行不同处理的时候.例如以下 Control

Junit测试Controller(MockMVC使用),传输@RequestBody数据解决办法

一.单元测试的目的 简单来说就是在我们增加或者改动一些代码以后对所有逻辑的一个检测,尤其是在我们后期修改后(不论是增加新功能,修改bug),都可以做到重新测试的工作.以减少我们在发布的时候出现更过甚至是出现之前解决了的问题再次重现. 这里主要是使用MockMvc对我们的系统的Controller进行单元测试. 对数据库的操作使用事务实现回滚,及对数据库的增删改方法结束后将会还远数据库. 二.MockMvc的使用 1.首先我们上一个例子, import org.apache.commons.log

用Mockito测试SpringMVC+Hibernate

用Mockito测试SpringMVC+Hibernate 译自:Spring 4 MVC+Hibernate 4+MySQL+Maven integration + Testing example using annotations 2017-01-19 目录: 1 目录结构 2 pom.xml3 Testing Controller Layer  3.1 com.websystique.springmvc.controller.AppControllerTest4 Testing Servi