【问题标题】:how can i do unit testing for APIs?如何对 API 进行单元测试?
【发布时间】:2020-02-03 14:24:34
【问题描述】:

我是单元测试的新手,我有这个任务来为 Post API 编写单元测试我只有端点和 api 正文我在谷歌读了很多,但我不知道如何开始,有什么帮助吗?

【问题讨论】:

    标签: java spring-boot api unit-testing junit


    【解决方案1】:

    你必须创建一个intent,如果需要的话用数据填充它,比如从二维码中提取的数据,然后调用startActivity。

    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    Bundle b = new Bundle();
    b.putInt("key", 1); //Your id
    intent.putExtras(b); //Put your id to your next Intent
    startActivity(intent);
    finish();
    

    我在包中放了一个 int,但它可以是任何实现 Serializable 的东西。

    【讨论】:

    • 是否可以使用appium创建Intent
    【解决方案2】:

    您可以使用该页面的活动直接访问所需的屏幕(如下例中的Splash screen 所示)。您可以使用以下方法启动活动。

    public static void launchActivity(Activity activityName) 
    {
    
            ((AndroidDriver<MobileElement>) driver).startActivity(activityName);
    }
    

    如何调用此函数

    假设您有以下应用程序包和活动(例如,但您必须将其用于您的应用程序)

    String appPackage ="my.app.helloworld";
    
    String appActivity = "my.app.helloworld".common.activity.SplashScreen";
    
    launchActivity(new Activity(appPackage, appActivity));
    

    您需要在AndroidManifest.xml 文件中设置android:exported="true" 以解决错误java.lang.SecurityException

    <activity
        android:name="com.dsquares.lucky/.screens.mainscreens.Wallet.WalletPayment.AddFundsActivity"
        android:label="wallet" 
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" >
            </action>
        </intent-filter>
    </activity>
    

    【讨论】:

    • 我们在 android 应用程序项目中有 AndroidManifest.xml 文件,其中包含应用程序中的所有活动集及其权限。你必须为这个活动制作 android:exported="true"。您已要求您的 android 开发团队执行此操作。更新的答案也显示一个例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-29
    • 2012-01-08
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 2019-01-05
    相关资源
    最近更新 更多