Junit mockito 測試Controller層方法有Pageable異常

weixin_33860553發表於2016-11-02

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
View Code

 

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 解析器


@InjectMocks
private 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

  致謝:感謝您的閱讀!

 

相關文章