您好我有以下代码要测试:
@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");
}
}
请在下面找到我的单元测试:
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")
);
}
}
我正在尝试模拟静态方法方法ESAPI.validator()。isValidInput,这就是我使用powermock的原因。
执行上述代码时,会显示以下错误:
java.lang.NoClassDefFoundError: org/powermock/tests/utils/RunnerTestSuiteChunker
Caused by: java.lang.ClassNotFoundException: org.powermock.tests.utils.RunnerTestSuiteChunker
请在下面找到我的maven依赖:
<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>
我知道为什么我将org.powermock.tests.utils.RunnerTestSuiteChunker作为jar powermock已经在类路径中了?
请注意,当我自己使用mockito时它工作正常,但由于静态方法,我必须添加powermock。
提前感谢任何建议
答案 0 :(得分:0)
您需要导入以下依赖项
<dependency>
<groupId>org.powermock.tests</groupId>
<artifactId>powermock-tests-utils</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
这对我有用,我有同样的问题