【问题标题】:how to write a junit using mockito for this method alogorithm?如何使用 mockito 为这种方法算法编写一个 junit?
【发布时间】:2021-02-07 04:06:26
【问题描述】:

您好,我正在尝试使用 mockito 为以下算法编写一个 junit。你能帮帮我吗?

public void setOrientation(Activity activity) {
    Get the DisplayMetrics for the phone;
    calculate the screen size;
    get configurations;
    
    if (diagonalInches>=6.5 && tablet connected to hard keyboard){
        set screen orientation
    }
}

【问题讨论】:

  • 模拟此方法使用的其他类。在您的测试中调用该方法,然后验证已在模拟类中调用了正确的方法。断言活动变量中的任何值都是应有的值。如果这还不够帮助,请提供实际代码,以便我们更好地了解这里发生的情况。

标签: java android testing junit mockito


【解决方案1】:

第 1 步:创建具有所有必需值的 Activity 对象

第 2 步:使用 @MockBean 为所有 @Autowired 创建 mockBeans

第 3 步:创建@Test 方法

第四步:使用Activity对象调用类对象方法

第 5 步:使用 Mockito.when(mock.methodTOMock).then(mockedReturnValue); 模拟所有外部或 bean 调用;

第 6 步:验证方法实例被调用以及它们被调用的次数

Mockito.verify(mockObject, time(number)).methodTOtest(arguments/Matcher methods);

第 7 步:验证预期和实际返回值 使用断言()

你能在这里提供示例代码,以便我们更好地理解

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 2018-03-04
    • 1970-01-01
    • 2015-08-30
    • 2021-11-17
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多