1

I followed the Robolectric documentation to create shadow classes but I'm not able to run shadow methods during test, it always uses original methods.

This is my code:

the Original class:

public class Original {
    public void print(){
        System.out.println("Hi from original class!");
    }
}

the Shadow class:

@Implements(Original.class)
public class ShadowOriginal {

    @Implementation
    public void print(){
        System.out.println("Hi from shadow class!");
    }
}

the test:

@RunWith(RobolectricGradleTestRunner.class)
@Config(manifest = "src/main/AndroidManifest.xml",
        emulateSdk = 21,
        reportSdk = 21,
        constants = BuildConfig.class,
        shadows = {ShadowOriginal.class})
public class OasisTests {
    @Test
    public void test() {
        Original t = new Original();
        t.print();
    }
}

When I run the tests, it always display "Hi from original class!"

What is wrong in my code? I uses

  • Android studio 1.2
  • robolectric 3.0-rc2
  • robolectric-gradle-plugin 1.0.1

How can I solve this issue?

Thanks in advance

rds
  • 26,253
  • 19
  • 107
  • 134
reevolt
  • 797
  • 1
  • 9
  • 24

1 Answers1

2

You need a custom robolectric runner where you can register own classes so they can be shadowed. See Robolectric shadow not working

Community
  • 1
  • 1
nenick
  • 7,340
  • 3
  • 31
  • 23
  • Isn't it duplication? – Eugen Martynov Apr 22 '15 at 08:08
  • Yes and no, duplication are necessary to give search variants a chance to find the answer. And I don't like this kind of pointing bad posts which include an useful answer because they get deleted by not so smart moderators – nenick Apr 22 '15 at 11:59
  • As for duplication is nothing about bad or good post. It is just to not repeat things. If you get update for the question during the time it is nice to update it and everyone will benefit form it. Especially this is true for Robolectric where things are broken or changed over days not even weeks – Eugen Martynov Apr 22 '15 at 14:08
  • 1
    Nice discussion about the duplication topic: http://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled – nenick Apr 22 '15 at 15:47
  • Thanks for sharing. I will be more accurate about marking question as duplicate. I think not you, not originator didn't lost any reputation. Sorry if I made other harm – Eugen Martynov Apr 22 '15 at 16:02
  • I don't give much about reputation. Just like to help other an push one of my favourite frameworks ;) – nenick Apr 22 '15 at 16:37