首页 > 解决方案 > 为什么 Android 中的 java 源代码有三个目录/文件夹?

问题描述

我是 Android 开发的新手。当我创建一个新的 Android Studio 项目时,该部分内生成了三个目录java

在此处输入图像描述

事实上,我们关心的是第一个目录中的 java 文件,在我的例子com.example.myapplication -> MainActivity中。

为什么有三个目录,每个目录都包含 java 文件,创建这些目录的目的是什么?

标签: javaandroidandroid-studio

解决方案


根据维基百科,开发测试是一个软件开发过程,涉及同步应用广泛的缺陷预防和检测策略,以降低软件开发风险、时间和成本,请参阅.

文件夹:
第一个( com.example.myapplication) 用于实际源代码。例如,活动、服务、广播接收器、内容提供者、模型、实用程序等的 java/kotlin 文件。

第二个( com.example.myapplication(andoridTest)) 用于在 android OS 上运行的仪器测试。作为一个例子,假设我们有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")));
    }
}

为每个编写的代码单元编写测试是最佳实践。因为它通过在早期阶段提供错误检测(而新鲜=))并在每次更改代码时验证代码,从而使开发人员的生活变得更容易维护。


推荐阅读