作者:The_Fuck_566 | 来源:互联网 | 2023-09-14 11:13
I have class in the project:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.mycompany.mypackage;
public class SimpleObject
{
int i, j;
public SimpleObject(int p1, int p2) {
i=p1;
j=p2;
}
public void setI(int i) { this.i = i; }
public void setJ(int j) { this.j = j; }
public int getI() { return i; }
public int getJ() { return j; }
} |
Created a shadow class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package com.mycompany.shadows;
(SimpleObject.class)
public class SimpleObjectShadow
{
SimpleObject sm;
int i, j;
int oldI;
int oldJ;
public SimpleObjectShadow() {
}
public void __constructor__(int i, int j) {
this.i = i;
this.j = j;
oldI = i;
oldJ = j;
}
public void setI(int i) {
oldI = i;
this.i = i;
}
public void setJ(int j) {
oldJ = j;
this.j = j;
}
public int getI() { return i; }
public int getJ() { return j; }
} |
Added shadow object to the test:
1 2 3 4 5 6
| (shadows= {SimpleObjectShadow.class})
public void testSimpleObjectShadow() {
SimpleObject sm = new SimpleObject(100,200);
SimpleObjectShadow shadowOf_ = (SimpleObjectShadow) Robolectric.getShadowWrangler().createShadowFor(sm);
shadowOf_ = Robolectric.shadowOf_(sm);
} |
The last line throws exception:
org.fest.reflect.exception.ReflectionError: Unable to find method '$$robo$getData' in oracle.mobile.shadows.SimpleObject with parameter type(s) []
at org.fest.reflect.method.Invoker.lookupInClassHierarchy(Invoker.java:78)
at org.fest.reflect.method.Invoker.createInvoker(Invoker.java:60)
at org.fest.reflect.method.Invoker.newInvoker(Invoker.java:55)
at org.fest.reflect.method.MethodReturnType.in(MethodReturnType.java:66)
at org.robolectric.bytecode.ShadowWrangler.shadowOf(ShadowWrangler.java:393)
at org.robolectric.Robolectric.shadowOf_(Robolectric.java:1087)
at opc.documents.test.LoginActivityUnitTest.testCreateActivityWithProfile(LoginActivityUnitTest.java:204)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:246)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:181)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
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)
It is expected that shadowed classes are instrumented also. If seems that usefulness of the shadows is not that good as when shadowed class is not instrumented because in this case calls are not intercepted.
If we need to do something else (in addition to the specified here http://pivotal.github.io/robolectric/extending.html), it would be nice to be explained.
thanks.
该提问来源于开源项目:robolectric/robolectric
It would be indeed useful to Shadow your own project classes. First I thought (when reading about it) that it's possible but had hard time figuring why it's not working, then I landed here. Robolectric is shadowing http calls via "ShadowDefaultRequestDirector". We've another http component in our project & we were trying to do the same thing for that component.
What is the reason that it's not allowed?