dimanche 26 mai 2019

UnfinishedStubbingException

i am newbie in unit test in android. my problem is that i have a helper class for get pixel from dp. this code work good and have a one static method. when i want write a test for this class, i give this error:

org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at com.sooran.emdad.ExampleUnitTest.setup(ExampleUnitTest.java:70)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, which is not supported
3: you are stubbing the behaviour of another mock inside before 'thenReturn' 
instruction is completed


at com.sooran.emdad.Util.DpUtil2.toPixels(DpUtil2.java:10)
at com.sooran.emdad.ExampleUnitTest.setup(ExampleUnitTest.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at 

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:74)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:80)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:53)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

this is my class:

public class DpUtil2 {

public static int toPixels(int dp, Context context) {
    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    float value=TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
    return (int) value;

}

and this is my test code:

@RunWith(MockitoJUnitRunner.class)
public class ExampleUnitTest {

@Mock
Context context;
@Mock
Resources resources;
@Mock
DisplayMetrics displayMetrics=new DisplayMetrics();
@Mock
DpUtil2 dpUtil;

@Test
public void addition_isCorrect() {
    assertEquals(4, 2 + 2);
}

@Test
public void dpTest(){

    assertEquals(dpUtil.toPixels(64,context),168);

}

@Before
public void setup(){

    displayMetrics.setToDefaults();
    displayMetrics.density=2.625f;
    displayMetrics.widthPixels=1080;
    displayMetrics.heightPixels=1794;
    displayMetrics.scaledDensity=2.625f;
    displayMetrics.xdpi=422.03f;
    displayMetrics.ydpi=424.069f;
    displayMetrics.densityDpi=420;

    int x=168;
    doReturn(resources).when(context).getResources();
    doReturn(displayMetrics).when(resources).getDisplayMetrics();

    doReturn(x).when(dpUtil).toPixels(64,context);
    }

i can pass test correctly with remove third do return but why third do return give this error

Aucun commentaire:

Enregistrer un commentaire