亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么Android中的java源代碼有三個目錄/文件夾?

為什么Android中的java源代碼有三個目錄/文件夾?

FFIVE 2023-03-23 13:48:30
我是 Android 開發的新手。當我創建一個新的 Android Studio 項目時,該java部分中會生成三個目錄:事實上,我們關心的是第一個目錄中的 java 文件,在我的例子中com.example.myapplication -> MainActivity。為什么有三個目錄,每個目錄都包含 java 文件,創建這些目錄的目的是什么?
查看完整描述

2 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

根據維基百科,開發測試是一個軟件開發過程,涉及廣泛的缺陷預防和檢測策略的同步應用,以降低軟件開發風險、時間和成本,請參閱。

文件夾:
第一個com.example.myapplication) 用于實際源代碼。例如,活動、服務、廣播接收器、內容提供者、模型、實用程序等的 java/kotlin 文件。

第二個com.example.myapplication(andoridTest)) 用于在 android 操作系統上運行的儀器測試。例如,假設我們有MainActivity一個按鈕。單擊按鈕時會顯示帶有消息的 Toast。所以我們可以測試按鈕是否正常工作,如下所示(為簡單起見,提供了導入):

import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;

import androidx.test.rule.ActivityTestRule;

import org.junit.Rule;

import org.junit.Test;

import org.junit.runner.RunWith;


import static androidx.test.espresso.assertion.ViewAssertions.matches;

import static androidx.test.espresso.matcher.RootMatchers.withDecorView;

import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;

import static androidx.test.espresso.matcher.ViewMatchers.withText;

import static org.hamcrest.Matchers.not;


import static androidx.test.espresso.Espresso.onView;

import static androidx.test.espresso.action.ViewActions.click;

import static androidx.test.espresso.matcher.ViewMatchers.withId;


@RunWith(AndroidJUnit4ClassRunner.class)

public class ExampleAndroidTest {


    @Rule

    public ActivityTestRule<Main2Activity> mActivityRule =

            new ActivityTestRule<>(Main2Activity.class);


    @Test

    public void buttonClickShowingToast_isCorrect() {

        onView(withId(R.id.bt_test)).perform(click());

        onView(withText(R.string.toast_test))

            .inRoot(withDecorView(not(

                    mActivityRule.getActivity().getWindow().getDecorView()

            ))).check(matches(isDisplayed()));

}

}

第三個( com.example.myapplication(test)) 用于可以在本地機器上運行的單元測試,意味著不需要 android 操作系統。例如,我們正在創建一個計時器,并且我們有一個實用方法可以將秒數轉換為 HH:MM:SS 格式。方法是:


public static String getHoursMinutesSeconds(int seconds) {

    int minutes = seconds / 60;

    seconds %= 60;

    int hours = minutes / 60;

    minutes %= 60;


    String strSec = Integer.toString(seconds);

    String strMin = Integer.toString(minutes);

    String strHour = Integer.toString(hours);

    StringBuilder sb = new StringBuilder();

    if (strHour.length() < 2) sb.append(0);

    sb.append(strHour);

    sb.append(':');

    if (strMin.length() < 2) sb.append(0);

    sb.append(strMin);

    sb.append(':');

    if (strSec.length() < 2) sb.append(0);

    sb.append(strSec);


    return sb.toString();

}

由于該方法不需要測試Android API 。它必須在本地機器上進行測試(因為它要快得多)。所以單元測試代碼:


import org.junit.Test;

import org.junit.runner.RunWith;

import org.junit.runners.BlockJUnit4ClassRunner;


import io.jachoteam.taxiapp.views.WaitingIndicatorView;


import static org.hamcrest.CoreMatchers.equalTo;

import static org.hamcrest.CoreMatchers.is;

import static org.hamcrest.MatcherAssert.assertThat;


@RunWith(BlockJUnit4ClassRunner.class)

public class ExampleUnitTest {


    @Test

    public void getHoursMinutesSeconds_isCorrect1() {

        String actualValue = WaitingIndicatorView.getHoursMinutesSeconds(1);

        assertThat(actualValue, is(equalTo("00:00:01")));

    }


    @Test

    public void getHoursMinutesSeconds_isCorrect2() {

        String actualValue = WaitingIndicatorView.getHoursMinutesSeconds(60);

        assertThat(actualValue, is(equalTo("00:01:00")));

    }

}

為每個編寫的代碼單元編寫測試是最佳實踐。因為通過在早期階段提供錯誤檢測(while fresh=))并在每次更改代碼時驗證代碼,當項目變大需要維護時,它使開發人員的生活更輕松。


查看完整回答
反對 回復 2023-03-23
?
眼眸繁星

TA貢獻1873條經驗 獲得超9個贊

根據Android 文檔

/test: 包含在主機 JVM 上運行的本地測試的代碼。

/androidtest 儀器測試,在操作系統上運行。

/module-name: 這是程序源代碼所在的地方


查看完整回答
反對 回復 2023-03-23
  • 2 回答
  • 0 關注
  • 282 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號