title: Testing Spring controllers using Powermock
body: <p>I have a class which tests specific controller and it works fine</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = "classpath:config/test-applicationContext-config.xml")
  @TestExecutionListeners({ 
      WebContextTestExecutionListener.class, 
      DependencyInjectionTestExecutionListener.class,
      DirtiesContextTestExecutionListener.class })
  public class TestAdminController {  
       //.....
  }
  </code></pre>
  
  <p>I have used there Google's Mockito library from <a href="https://code.google.com/p/mockito/" rel="noreferrer">mockito.org</a> to mock my underlying beans.</p>
  
  <p><strong>Now my question is the following:</strong> I have some classes which need to be mocked also, but they have <code>final</code> methods and Google's mockito seems not resolving this issue. One of my collegues suggested to use Powermock from <a href="https://code.google.com/p/powermock/" rel="noreferrer">powermock.org</a>. But it requires to annotate the tester class with <code>@RunWith(PowerMockRunner.class)</code> annotation. If I use this I have to remove the annotation <code>@RunWith(SpringJUnit4ClassRunner.class)</code> and this will give me problems cause the Spring test context will not be created. How I can avoid this situation?</p>
  
  <p>After suggestion I have configured to use <code>PowerMockRule</code> instead of <code>@RunWith</code> annotation</p>
  
  <p>My project Maven dependencies are as follows</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.powermock&lt;/groupId&gt;
      &lt;artifactId&gt;powermock-mockito-release-full&lt;/artifactId&gt;
      &lt;version&gt;1.5&lt;/version&gt;
      &lt;type&gt;pom&lt;/type&gt;
  &lt;/dependency&gt; 
  &lt;dependency&gt;
          &lt;groupId&gt;org.powermock&lt;/groupId&gt;
          &lt;artifactId&gt;powermock-module-junit4-rule&lt;/artifactId&gt;
          &lt;version&gt;1.5&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
          &lt;groupId&gt;org.powermock&lt;/groupId&gt;
          &lt;artifactId&gt;powermock-classloading-xstream&lt;/artifactId&gt;
          &lt;version&gt;1.5&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Now my class looks like this (another controller test)</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = "classpath:config/test-applicationContext-config.xml")
  @TestExecutionListeners({ 
      WebContextTestExecutionListener.class, 
      DependencyInjectionTestExecutionListener.class,
      DirtiesContextTestExecutionListener.class })
  @PrepareForTest(ADSynchronizationImpl.class)
  public class ThirdPartyLoginControllerTest {
      @Rule
      public PowerMockRule rule = new PowerMockRule();
  
      @Autowired
      private ThirdPartyLoginController thirtPartyLoginController;
  
      @Autowired
      private MockFactoryForControllers mockFactory;
  
      @Test
      public void testLoginActiveDirectory(){
  
      }
  }
  </code></pre>
  
  <p>But after running my test I'm getting the following exception trace</p>
  
  <blockquote>
    <p>com.thoughtworks.xstream.converters.ConversionException:
    javassist.NotFoundException:
    com.somecompany.analytics.manager.db.api.MlDao$$EnhancerByMockitoWithCGLIB$$b86d7415
    : javassist.NotFoundException:
    com.somecompany.analytics.manager.db.api.MlDao$$EnhancerByMockitoWithCGLIB$$b86d7415
    ---- Debugging information ---- message             : javassist.NotFoundException:
    com.somecompany.analytics.manager.db.api.MlDao$$EnhancerByMockitoWithCGLIB$$b86d7415
    cause-exception     : java.lang.RuntimeException cause-message       :
    javassist.NotFoundException:
    com.somecompany.analytics.manager.db.api.MlDao$$EnhancerByMockitoWithCGLIB$$b86d7415
    class               :
    com.somecompany.ui.controllers.ThirdPartyLoginController required-type
    : com.somecompany.ui.controllers.ThirdPartyLoginController
    converter-type      :
    com.thoughtworks.xstream.converters.reflection.ReflectionConverter
    path                :
    /org.powermock.modules.junit4.rule.PowerMockStatement$1/outer-class/fNext/fTarget/thirtPartyLoginController/mlDao
    line number         : 14 class<a href="https://code.google.com/p/mockito/" rel="noreferrer">1</a>            :
    com.somecompany.ui.controllers.ThirdPartyLoginControllerTest class<a href="https://code.google.com/p/powermock/" rel="noreferrer">2</a> 
    : org.junit.internal.runners.statements.InvokeMethod class[3]<br>
    : org.powermock.modules.junit4.rule.PowerMockStatement class[4]<br>
    : org.powermock.modules.junit4.rule.PowerMockStatement$1 version<br>
    : null
    -------------------------------   at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:79)
      at
    com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
      at
    com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
      at
    com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
      at
    com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
      at
    com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
      at
    com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
      at
    com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
      at
    com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
      at
    com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
      at
    com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
      at
    com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:355)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:306)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
      at
    com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
      at
    com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
      at
    com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
      at
    com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
      at
    com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
      at
    com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
      at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1052)    at
    com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1036)     at
    com.thoughtworks.xstream.XStream.fromXML(XStream.java:912)    at
    com.thoughtworks.xstream.XStream.fromXML(XStream.java:903)    at
    org.powermock.classloading.DeepCloner.clone(DeepCloner.java:54)   at
    org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89)
      at
    org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
      at
    org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
      at
    org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
      at
    org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
      at
    org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
      at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
      at
    org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at
    org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)    at
    org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
      at
    org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at
    org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at
    org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: java.lang.RuntimeException: javassist.NotFoundException:
    com.somecompany.idm.analytics.manager.db.api.MlDao$$EnhancerByMockitoWithCGLIB$$b86d7415
      at
    org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:199)
      at
    org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:149)
      at
    org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:356)    at
    com.thoughtworks.xstream.core.util.ClassLoaderReference.loadClass(ClassLoaderReference.java:31)
      at
    com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:49)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.DynamicProxyMapper.realClass(DynamicProxyMapper.java:55)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.PackageAliasingMapper.realClass(PackageAliasingMapper.java:88)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.ClassAliasingMapper.realClass(ClassAliasingMapper.java:79)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.ArrayMapper.realClass(ArrayMapper.java:74)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.MapperWrapper.realClass(MapperWrapper.java:30)
      at
    com.thoughtworks.xstream.mapper.CachingMapper.realClass(CachingMapper.java:45)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.determineType(AbstractReflectionConverter.java:458)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:294)
      at
    com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:234)
      at
    com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
      ... 57 more Caused by: javassist.NotFoundException:
    com.somecompany.analytics.manager.db.api.MlDao$$EnhancerByMockitoWithCGLIB$$b86d7415
      at javassist.ClassPool.get(ClassPool.java:450)  at
    org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:192)
      ... 87 more</p>
  </blockquote>