UnfinishedStubbingException

时间:2019-05-26 12:30:17

标签: android testing mockito

我是Android单元测试中的新手。 我的问题是我有一个用于从dp获取像素的助手类。 该代码工作良好,并且具有一个静态方法。 当我要为此类编写测试时,出现以下错误:

string AddMatchCommand = "Insert into Games values (@Match, @Ratings1, @Ratings2, @Ratings3, @Ratings4, @Ratings5, @Ratings6, @Ratings7, @Ratings8, @Ratings9, @Ratings10, @Ratings11, @Ratings12, @Ratings13, @Ratings14, @Ratings15, @Ratings16)";
SqlConnection MyConn = new SqlConnection(CreateTableConnectionString);  
SqlCommand command = new SqlCommand(AddMatchCommand, MyConn);  

command.Parameters.AddWithValue("@Match", MatchIDToInsert.ToString(CultureInfo.InvariantCulture));  
command.Parameters.AddWithValue("@Ratings1", RatingsToInsert[0].ToString(CultureInfo.InvariantCulture));  
command.Parameters.AddWithValue("@Ratings2", RatingsToInsert[1].ToString(CultureInfo.InvariantCulture));  
//...   
command.Parameters.AddWithValue("@Ratings16", RatingsToInsert[15].ToString(CultureInfo.InvariantCulture)); 

MyConn.Open();  
try  
{  
 command.ExecuteNonQuery();  
}  
catch (SqlException S)  
{  
 if (S.Number != 2627)  //2627 is the ID for the exception I want to ignore
 {  
  MessageBox.Show(S.Message);  
 }  
}  
MyConn.Close(); 

这是我的课程:

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)

这是我的测试代码:

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;

}

我可以通过删除第三个退货来正确通过测试 但是为什么第三次返回却给出了这个错误

1 个答案:

答案 0 :(得分:1)

嗯,要测试静态方法,您需要使用PowerMock,我使用了示例并进行了修改。

   package com.sample.mass;

    import android.content.Context;
    import android.content.res.Resources;
    import android.util.DisplayMetrics;

    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mock;
    import org.mockito.Mockito;
    import org.mockito.MockitoAnnotations;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest({DpUtil2.class})
public class DpUtil2Test {


    private DpUtil2 mDpUtil2;

    @Mock
    Context mContext;

    @Mock
    Resources mResources;

    @Mock
    DisplayMetrics mDisplayMetrics;


    private int dp = 20;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        Mockito.when(mResources.getDisplayMetrics()).thenReturn(mDisplayMetrics);
        Mockito.when(mContext.getResources()).thenReturn(mResources);

    }

    @Test
    public void testToPixels() throws Exception {
        System.out.println("yes");
        PowerMockito.mockStatic(DpUtil2.class);
        PowerMockito.when(DpUtil2.toPixels(dp, mContext)).thenReturn(dp);

        int actualResult = DpUtil2.toPixels(dp, mContext);
        //expected, actual
        Assert.assertEquals(dp, actualResult);

        PowerMockito.verifyStatic(DpUtil2.class);
        DpUtil2.toPixels(dp, mContext);
        System.out.println("-----------we are done--------------------");
    }
}

并在gradle中使用它们,

 testImplementation 'org.mockito:mockito-core:2.2.0'
    testImplementation 'org.powermock:powermock-core:1.7.1'
    testImplementation 'org.powermock:powermock-module-junit4:1.7.1'
    testImplementation 'org.powermock:powermock-api-mockito2:1.7.1'