lundi 2 mai 2016

Spring MVC Unit Test - powermock error org/powermock/tests/utils/RunnerTestSuiteChunker -

Hi I have the following code to test:

 @RequestMapping(value = "/displayPages", method = RequestMethod.GET)
    public ModelAndView errorPage(ModelMap model, HttpServletRequest request) {
        String token = (request != null) ? request.getParameter("tok") : "";
        boolean requestP = ESAPI.validator().isValidInput("Request Param", tok, "HTTPParameterValue", 1, false);
        if (requestP || token.contains(msg.getMessage("techErr.tok", new Object[]{}, Constants.LOCAL))) {
            return new ModelAndView("dispError");
        } else {
            return new ModelAndView("login");
        }

    }

Please find my unit test below:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import static org.mockito.Mockito.when;
import org.owasp.esapi.ESAPI;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;



@RunWith(PowerMockRunner.class)
@PrepareForTest({ESAPI.class})
public class StaticClass {

      private MockMvc mockMvc;

    @Test
    public void testErrorPage() throws Exception {
        System.out.println("initialize");
        PowerMockito.mockStatic(ESAPI.class);
        when(ESAPI.validator().isValidInput(Matchers.anyString(), Matchers.anyString(), Matchers.anyString(),  Matchers.anyInt(), Matchers.anyBoolean())).thenReturn(true);

         mockMvc.perform(get("/displayPages"))
                .andExpect(view().name("dispError")

                );
    }

}

I am trying to mock the static method method ESAPI.validator().isValidInput and this is why i am using powermock.

When I execute the above code the following error is displayed:

java.lang.NoClassDefFoundError: org/powermock/tests/utils/RunnerTestSuiteChunker
Caused by: java.lang.ClassNotFoundException: org.powermock.tests.utils.RunnerTestSuiteChunker

Please find below my maven dependency:

    <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12-beta-2</version>
            <scope>test</scope>
        </dependency>



          <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.6.4</version>
       </dependency>

   <dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.10.19</version>
          <scope>test</scope>
   </dependency>

  <dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.6.5</version>
         <scope>test</scope>
</dependency>



        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.5.RELEASE</version>
            <scope>test</scope>
        </dependency> 

Any idea why I am getting org.powermock.tests.utils.RunnerTestSuiteChunker as the jar powermock is already in the classpath?

Please note that when I use mockito on its own it works fine but because of the static method I have to add powermock.

Thanks in advance for any advice

Aucun commentaire:

Enregistrer un commentaire