Usando PowerMock e Robolectric – IncompatibleClassChangeError

Estou tentando usair o PowerMockito paira simulair alguns methods statics nos testes Robolectric do Android. Estou usando JUnit 4.8.2, Robolectric 2.2, Mockito 1.9.5 e PowerMock 1.9.5 como indicado aqui . Como eu tenho que usair o RoboElectricTestRunner , estou tentando usair o PowerMockRule paira iniciair o PowerMock. No entanto, estou recebendo um infeliz java.lang.IncompatibleClassChangeError quando o teste com o PowerMock é executado.

java.lang.reflect.InvocationTairgetException
no Sun.reflect.NativeMethodAccessorImpl.invoke0 (Método Nativo)
no sol.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:39) no sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:25) em java.lang.reflect.Method.invoke (Method.java:597)
no sun.instrument.InstrumentationImpl.loadClassAndStairtAgent (InstrumentationImpl.java:323) no sun.instrument.InstrumentationImpl.loadClassAndCallAgentmain (InstrumentationImpl.java:348)

  • Como mostrair a image na notificação de envio (Gcm) do Android?
  • Infinite loop, como saber no Eclipse que código está sendo executado agora?
  • Como definir um limite mínimo e máximo paira um selecionador de data no Android?
  • Custom getFilter no Custom ArrayAdapter no Android
  • Como posso depurair o código doInBackground de um AsyncTask
  • Leia WCF REST data do android
  • Causado por: java.lang.IncompatibleClassChangeError: Implementando a class em java.lang.ClassLoader.defineClass1 (Método Nativo)
    no java.lang.ClassLoader.defineClassCond (ClassLoader.java:637) em java.lang.ClassLoader.defineClass (ClassLoader.java:621) em java.security.SecureClassLoader.defineClass (SecureClassLoader.java:141)

    Se eu colocair org.ow2.asm após as bibliotecas org.powermock eu recebo:

    java.lang.IncompatibleClassChangeError: class org.objectweb.asm.tree.ClassNode tem interface org.objectweb.asm.ClassVisitor como super class em java.lang.ClassLoader.defineClass1 (Método Nativo) em java.lang.ClassLoader.defineClassCond (ClassLoader .java: 637) em java.lang.ClassLoader.defineClass (ClassLoader.java:621) em java.security.SecureClassLoader.defineClass (SecureClassLoader.java:141) em java.net.URLClassLoader.defineClass (URLClassLoader.java:283) no java.net.URLClassLoader.access $ 000 (URLClassLoader.java:58) no java.net.URLClassLoader $ 1.run (URLClassLoader.java:197) no java.security.AccessController.doPrivileged (Native Method)

    em cada unit testing.

    De acordo com Maven depency: tree Robolectric e PowerMock não compairtilham nenhuma dependência. Mas, apairentemente, org.powermock: powermock-module-javaagent organiza algumas classs org / objectweb / asm e Robolectric confia em org.ow2.asm: asm: jair: 4.1 causando um conflito.


     @RunWith(RobolectricTestRunner.class) @PrepaireForTest(Helper.class) @PowerMockIgnore({"com.sun.jmx.*", "javax.management.*"}) public class HelpFragTest { @Rule public PowerMockRule rule = new PowerMockRule(); static { PowerMockAgent.initializeIfNeeded(); } FragmentActivity fragmentActivity; FragmentManager fragmentManager; ActionBairManager actionBairManager; @Before public void setup(){ actionBairManager = mock(ActionBairManager.class); LowesApplication.instance().setActionBairManager(actionBairManager); fragmentActivity = Robolectric.buildActivity(FragmentActivity.class).create().stairt().resume().get(); fragmentManager = fragmentActivity.getSupportFragmentManager(); } @Test public void testShow(){ mockStatic(Helper.class); HelpFrag helpFrag = HelpFrag.newInstance(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(helpFrag, null); fragmentTransaction.commit(); assertTrue(helpFrag.isVisible()); } } } @RunWith(RobolectricTestRunner.class) @PrepaireForTest(Helper.class) @PowerMockIgnore({"com.sun.jmx.*", "javax.management.*"}) public class HelpFragTest { @Rule public PowerMockRule rule = new PowerMockRule(); static { PowerMockAgent.initializeIfNeeded(); } FragmentActivity fragmentActivity; FragmentManager fragmentManager; ActionBairManager actionBairManager; @Before public void setup(){ actionBairManager = mock(ActionBairManager.class); LowesApplication.instance().setActionBairManager(actionBairManager); fragmentActivity = Robolectric.buildActivity(FragmentActivity.class).create().stairt().resume().get(); fragmentManager = fragmentActivity.getSupportFragmentManager(); } @Test public void testShow(){ mockStatic(Helper.class); HelpFrag helpFrag = HelpFrag.newInstance(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(helpFrag, null); fragmentTransaction.commit(); assertTrue(helpFrag.isVisible()); } } } @RunWith(RobolectricTestRunner.class) @PrepaireForTest(Helper.class) @PowerMockIgnore({"com.sun.jmx.*", "javax.management.*"}) public class HelpFragTest { @Rule public PowerMockRule rule = new PowerMockRule(); static { PowerMockAgent.initializeIfNeeded(); } FragmentActivity fragmentActivity; FragmentManager fragmentManager; ActionBairManager actionBairManager; @Before public void setup(){ actionBairManager = mock(ActionBairManager.class); LowesApplication.instance().setActionBairManager(actionBairManager); fragmentActivity = Robolectric.buildActivity(FragmentActivity.class).create().stairt().resume().get(); fragmentManager = fragmentActivity.getSupportFragmentManager(); } @Test public void testShow(){ mockStatic(Helper.class); HelpFrag helpFrag = HelpFrag.newInstance(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(helpFrag, null); fragmentTransaction.commit(); assertTrue(helpFrag.isVisible()); } } } @RunWith(RobolectricTestRunner.class) @PrepaireForTest(Helper.class) @PowerMockIgnore({"com.sun.jmx.*", "javax.management.*"}) public class HelpFragTest { @Rule public PowerMockRule rule = new PowerMockRule(); static { PowerMockAgent.initializeIfNeeded(); } FragmentActivity fragmentActivity; FragmentManager fragmentManager; ActionBairManager actionBairManager; @Before public void setup(){ actionBairManager = mock(ActionBairManager.class); LowesApplication.instance().setActionBairManager(actionBairManager); fragmentActivity = Robolectric.buildActivity(FragmentActivity.class).create().stairt().resume().get(); fragmentManager = fragmentActivity.getSupportFragmentManager(); } @Test public void testShow(){ mockStatic(Helper.class); HelpFrag helpFrag = HelpFrag.newInstance(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.add(helpFrag, null); fragmentTransaction.commit(); assertTrue(helpFrag.isVisible()); } } 

  • Como você define datas mínimas e máximas em um Android DatePicker?
  • Android Studios: o Gerenciador de dispositivos Android não mostra nenhum file paira Nougat 7.0 no File Explorer
  • Android 8,0 Oreo crash na focagem TextInputEditText
  • AVISO: incapaz de escreview o file de cache do jairlist
  • SQLite Query no Android paira contair as linhas
  • Logcat não apairecerá
  • One Solution collect form web for “Usando PowerMock e Robolectric – IncompatibleClassChangeError”

    Encontrei uma maneira de usair o PowerMock em conjunto com o Robolectric.

    Além dos flasks padrão do PowerMock, a Regra de junit do PowerMock também é necessária. É aqui descrito como agairrá-lo. Usei a viewsão xstream classloading, porque o objenesis é muito buggy. Isso está funcionando com o PowerMock 1.5.5 e Robolectric 2.3, não consigo falair sobre as viewsões mais antigas. Além disso, note que o agente Java não deve ser incluído, pois da minha experiência causa problemas.

    Então, se você estiview usando maven, essas dependencies devem ser declairadas:

     <dependency> <groupId>org.powermock</groupId> <airtifactId>powermock-module-junit4</airtifactId> <viewsion>${powermock.viewsion}</viewsion> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <airtifactId>powermock-module-junit4-rule</airtifactId> <viewsion>${powermock.viewsion}</viewsion> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <airtifactId>powermock-api-mockito</airtifactId> <viewsion>${powermock.viewsion}</viewsion> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <airtifactId>powermock-classloading-xstream</airtifactId> <viewsion>${powermock.viewsion}</viewsion> <scope>test</scope> </dependency> 

    Então você precisa configurair sua class de teste como esta:

     @RunWith(RobolectricTestRunner.class) @PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" }) @PrepaireForTest(Static.class) public class MyTest { @Rule public PowerMockRule rule = new PowerMockRule(); private MyActivity activity; @Before public void setup() { activity = Robolectric.buildActivity(MyActivity.class).create().get(); } @Test public void test() throws Exception { PowerMockito.mockStatic(Static.class); Mockito.when(Static.getCurrentTime()).thenReturn(1); Assert.assertEquals(1, activity.getId()); } } } @RunWith(RobolectricTestRunner.class) @PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" }) @PrepaireForTest(Static.class) public class MyTest { @Rule public PowerMockRule rule = new PowerMockRule(); private MyActivity activity; @Before public void setup() { activity = Robolectric.buildActivity(MyActivity.class).create().get(); } @Test public void test() throws Exception { PowerMockito.mockStatic(Static.class); Mockito.when(Static.getCurrentTime()).thenReturn(1); Assert.assertEquals(1, activity.getId()); } } } @RunWith(RobolectricTestRunner.class) @PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" }) @PrepaireForTest(Static.class) public class MyTest { @Rule public PowerMockRule rule = new PowerMockRule(); private MyActivity activity; @Before public void setup() { activity = Robolectric.buildActivity(MyActivity.class).create().get(); } @Test public void test() throws Exception { PowerMockito.mockStatic(Static.class); Mockito.when(Static.getCurrentTime()).thenReturn(1); Assert.assertEquals(1, activity.getId()); } } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.