After migrating my JAVA EE app. (Spring Web model-view-controller (MVC) framework) from Ant to Maven I have some errors executing the Junit test that before the migration were working fine
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)
and here my class:
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);
}
}
Try @RunWith(PowerMockRunner.class)
instead of @RunWith(MockitoJUnitRunner.class)