我正在尝试为Android应用程序进行单元测试,我需要从res.string资源获取一个字符串.我想测试的类是POJO类.我正在用两种语言做应用程序,因此,我需要从资源中获取一个字符串.问题是我无法获得上下文或活动,是否可能?我知道使用Instrumentation测试我可以做到,但我需要在进行仪器测试(黑盒测试)之前测试一些功能(白盒测试).这是我必须测试的功能:
public void setDiaByText(String textView) {
getll_diaSeleccionado().clear();
if (textView.contains(context.getResources().getString(R.string.sInicialLunes))) {
getll_diaSeleccionado().add(0);
getIsSelectedArray()[0] = true;
getI_idiaSeleccionado()[0] =1;
} else
{
getIsSelectedArray()[0] = false;
getI_idiaSeleccionado()[0] =0;
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试:
@Test
public void setDiaByTextView() {
String texto = "L,M,X,J,V,S,D";
alertaPOJO.setDiaByText(texto);
assertEquals(alertaPOJO.getIsSelectedArray()[0], true);
assertEquals(alertaPOJO.getI_idiaSeleccionado()[0], 1);
}
Run Code Online (Sandbox Code Playgroud)
尝试时会崩溃 context.getResources().getString(R.string.sInicialLunes))
如果我把'Mon'代替context.getResources().getString(R.string.sInicialLunes))或'L'它完全正常工作,是否可以获取上下文或活动以访问资源文件夹?
我正在使用Mockito进行测试,setUp函数是:
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mContext = Mockito.mock(Alerta.class);
Mockito.when(mContext.getApplicationContext()).thenReturn(mContext);
alertaPOJO = new AlertaPOJO();
}
Run Code Online (Sandbox Code Playgroud)
谢谢
我正在尝试用Espresso测试NestedScrollView,但出现错误:
“将不会执行操作,因为目标视图与以下一个或多个约束不匹配:至少90%的视图区域显示给用户。”
我知道此错误是因为Android未检测到要单击的按钮,也就是说,我需要滚动到底部才能看到该按钮。我也已经阅读了scrollTo()对NestedScrollView不可用,所以我不能使用它。我想我必须滚动到NestedScrollView的底部才能看到该按钮,我既不确定这一点,也不知道该怎么做。
我想单击红色按钮,但它没有可见性。我已经看到了几个堆栈问题和一些教程,但是我不知道如何滚动到嵌套的底部。
应用程序工具栏的代码为:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.findandgo.activity.MenuPrincipal"
>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll"
app:popupTheme="@style/AppTheme.AppBarOverlay"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />`
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
NestedScrollView的代码为:
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_evento_crear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill_vertical"
android:background="@drawable/fondo"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".Evento"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:nestedScrollingEnabled="false"
android:padding="@dimen/size_20"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<com.findandgo.custom.CustomFontEditText
android:id="@+id/idEtEventoNuevoNombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/idSpEventoNuevoCategoria"
android:layout_alignStart="@+id/idSpEventoNuevoCategoria"
android:layout_below="@id/tool_bar"
android:hint="@string/sEventoNombre"
android:textSize="@dimen/size_12"
app:font="@string/font_name_source_amatic_regular" …Run Code Online (Sandbox Code Playgroud) android android-layout android-espresso android-nestedscrollview