Java EE app。(Spring Web model-view-controller(MVC)framework)をAntからMavenに移行した後、JUnitテストの実行中にエラーが発生しました。移行前は正常に機能していました。
org.powermock.api.mockito.ClassNotPreparedException:
The class org.springframework.web.util.WebUtils not prepared for test.
To prepare this class, add class to the '@PrepareForTest' annotation.
In case if you don't use this annotation, add the annotation on class or method level.
at org.powermock.api.mockito.expectation.reporter.MockitoPowerMockReporter.classNotPrepared(MockitoPowerMockReporter.Java:31)
at org.powermock.api.mockito.internal.mockcreation.MockTypeValidatorFactory$DefaultMockTypeValidator.validate(MockTypeValidatorFactory.Java:38)
at org.powermock.api.mockito.internal.mockcreation.AbstractMockCreator.validateType(AbstractMockCreator.Java:18)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMock(MockCreator.Java:57)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.Java:47)
at org.powermock.api.mockito.PowerMockito.mockStatic(PowerMockito.Java:71)
at com.tdk.iot.controller.criteria.ProductCriteriaPeriodControllerTest.setUpTest(ProductCriteriaPeriodControllerTest.Java:83)
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:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.Java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.Java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.Java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.Java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.Java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.Java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.Java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.Java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.Java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.Java:220)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.Java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.Java:62)
at org.Eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.Java:86)
at org.Eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.Java:38)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:459)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.Java:675)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.Java:382)
at org.Eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.Java:192)
そしてここに私のクラス:
package com.tdk.iot.controller.criteria;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import Java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.powermock.api.mockito.PowerMockito;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.web.util.WebUtils;
import com.tdk.iot.domain.formBeans.ProductCriteriaPeriodForm;
import com.tdk.iot.services.client.HelpDeskService;
import com.tdk.iot.services.client.ProductCriteriaPeriodService;
/**
* @author amadeu
*
*/
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = {
"classpath:com/tdk/iot/services/testServiceContext.xml",
"classpath:com/tdk/iot/controller/apply/applyController.xml",
"classpath:com/tdk/iot/config/testApplicationContext.xml",
"classpath:com/tdk/iot/config/testDatabaseMessageSource.xml",
"classpath:com/tdk/iot/controller/propertyeditors/propertyeditorsContext.xml"})
@PrepareForTest({HttpServletRequest.class,
HttpServletResponse.class,
ProductCriteriaPeriodService.class,
HelpDeskService.class,
WebUtils.class,
HttpSession.class})
public class ProductCriteriaPeriodControllerTest {
private final static String CRITERIA_FORM_ATTRIBUTE_KEY = "productCriteriaPeriodForm";
private static final String PRODUCT_CRITERIA_PERIOD_CONFIRMATION_MSM = "productCriteriaPeriodConfirmationMessage";
private static final String PRODUCT_CRITERIA_PERIOD_CONFIRMATION_MSM_KEY = "criteria.period.periodsHaveBeenUpdated";
private static final String PRODUCT_CRITERIA_PERIOD_ERROR_MSM_KEY = "criteria.period.errorSaving";
@Mock
private HttpServletRequest request;
@Mock
private HttpServletResponse response;
@Mock
private ProductCriteriaPeriodService productCriteriaPeriodService;
@Mock
protected HelpDeskService helpDeskService ;
@Mock
private HttpSession session;
@InjectMocks
private ProductcriteriaperiodController controller;
@Before
public void setUpTest() {
PowerMockito.mockStatic (WebUtils.class);
PowerMockito.mockStatic (RequestContextUtils.class);
}
@Test
public void showFormTest() throws Exception {
when(helpDeskService.isHDRole()).thenReturn(true);
Model model = new ExtendedModelMap();
assertEquals("/criteria/productcriteriaperiod", controller.criteriaGracePeriod(request, model));
Map<String, Object> attributes = model.asMap();
assertTrue(attributes.containsKey(CRITERIA_FORM_ATTRIBUTE_KEY));
ProductCriteriaPeriodForm criteriaGracePeriodForm = (ProductCriteriaPeriodForm)attributes.get(CRITERIA_FORM_ATTRIBUTE_KEY);
assertNotNull(criteriaGracePeriodForm.getProductCriteriaPeriodsList());
}
@Test
public void saveOKTest( ) throws Exception {
Model model = new ExtendedModelMap();
BindingResult result = mock(BindingResult.class);
when(result.hasErrors()).thenReturn(false);
assertEquals("/criteria/productcriteriaperiod", controller.save(new ProductCriteriaPeriodForm(), result, model));
Map<String, Object> attributes = model.asMap();
assertTrue(attributes.containsKey(CRITERIA_FORM_ATTRIBUTE_KEY));
assertTrue(attributes.containsKey(PRODUCT_CRITERIA_PERIOD_CONFIRMATION_MSM));
assertEquals(attributes.get(PRODUCT_CRITERIA_PERIOD_CONFIRMATION_MSM), PRODUCT_CRITERIA_PERIOD_CONFIRMATION_MSM_KEY);
}
@Test
public void saveNOKTest( ) throws Exception {
Model model = new ExtendedModelMap();
BindingResult result = mock(BindingResult.class);
when(result.hasErrors()).thenReturn(true);
assertEquals("/criteria/productcriteriaperiod", controller.save(new ProductCriteriaPeriodForm(), result, model));
Map<String, Object> attributes = model.asMap();
assertTrue(attributes.containsKey(CRITERIA_FORM_ATTRIBUTE_KEY));
assertTrue(attributes.containsKey(PRODUCT_CRITERIA_PERIOD_CONFIRMATION_MSM));
assertEquals(attributes.get(PRODUCT_CRITERIA_PERIOD_CONFIRMATION_MSM), PRODUCT_CRITERIA_PERIOD_ERROR_MSM_KEY);
}
}
@RunWith(MockitoJUnitRunner.class)の代わりに@RunWith(PowerMockRunner.class)
を試してください