标签: android-junit

什么是触摸模式?为什么它对*ActivityTestRule*类很重要?

android.support.test.rule.ActivityTestRule类(见这里)发生在一个initialTouchMode在其构造函数的参数.除了以下内容之外,在类引用(或任何在线)中没有解释这一点:

initialTouchMode - 如果在启动时应将Activity置于"触摸模式",则为true

"触摸模式"究竟是什么意思?什么是设置的影响initialTouchModeActivityTestRuletruefalse?(我看到这个参数的默认值是false).

android android-testing android-espresso android-instrumentation android-junit

22
推荐指数
1
解决办法
3673
查看次数

Mockito lenient() 何时使用

据我了解,沉默StrictStubbinglenient引发的异常。基于此,不应该使用,也许只是在执行 TDD 时暂时使用,因为严格的存根异常通常意味着您的代码要么是错误的,测试设计得很糟糕,要么您添加了不必要的行。lenient

lenient是否存在实际需要或对测试有用的实际场景?

android unit-testing mockito android-junit

20
推荐指数
5
解决办法
8万
查看次数

如何进行单元测试(使用JUnit或mockito)recyclelerview项目点击

我目前正在尝试单独测试recyclerview addonitemclick listner,使用junit或mockito.这是我的代码:

private void mypicadapter(TreeMap<Integer, List<Photos>> photosMap) {
    List<PhotoListItem> mItems = new ArrayList<>();

    for (Integer albumId : photosMap.keySet()) {
        ListHeader header = new ListHeader();
        header.setAlbumId(albumId);
        mItems.add(header);
        for (Photos photo : photosMap.get(albumId)) {
            mItems.add(photo);
        }


        pAdapter = new PhotoViewerListAdapter(MainActivity.this, mItems);
        mRecyclerView.setAdapter(pAdapter);
        //  set 5 photos per row if List item type --> header , else fill row with header.
        GridLayoutManager layoutManager = new GridLayoutManager(this, 5);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (mRecyclerView.getAdapter().getItemViewType(position) == PhotoListItem.HEADER_TYPE)
                    // return …
Run Code Online (Sandbox Code Playgroud)

android mocking android-testing android-recyclerview android-junit

14
推荐指数
1
解决办法
5139
查看次数

在Robolectric中找不到@Config常量参数

我正在尝试编写Robolectric测试。我正在关注一些似乎正在使用的教程

@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class)
Run Code Online (Sandbox Code Playgroud)

设置测试,但是在我的情况下,参数常量似乎无法解析。

在此处输入图片说明

我的Robolectric依赖性如下所示:

testImplementation "org.robolectric:robolectric:4.0.2"
Run Code Online (Sandbox Code Playgroud)

android robolectric android-testing android-junit

14
推荐指数
2
解决办法
1689
查看次数

在Android Studio项目上找不到参数的方法test()

在我的Android应用程序中,我想排除软件包中的一些测试用例,以便使用文件中的test任务build.gradle。例如:

apply plugin: 'com.android.library'

test{
     exclude '**/calltest/Summary.class'
}
Run Code Online (Sandbox Code Playgroud)

如果同步项目,则会出现以下异常:

* What went wrong:
A problem occurred evaluating project ':SdkModule'.
> Could not find method test() for arguments [build_4g3vf7b615x3x1p7i9ty0pt1l$_run_closure1@73d026ca] on project ':SdkModule' of type org.gradle.api.Project.
Run Code Online (Sandbox Code Playgroud)

如果我加 apply plugin : 'java'

CONFIGURE FAILED in 1s
The 'java' plugin has been applied, but it is not compatible with the Android plugins.
Run Code Online (Sandbox Code Playgroud)

请帮我。

android build.gradle android-junit android-studio-3.0

14
推荐指数
2
解决办法
705
查看次数

使用检测和JUnit4测试重新创建Android Activity

我想为重新创建活动编写测试.执行旋转是可选的.

我希望测试能够被谷歌"祝福"的测试框架的最新版本编写.我是编写测试的新手,所以我想学习基本的,主流的,支持良好的工具.当我掌握基础知识时,任何第三方测试框架都会没问题.而且由于我想测试非常基本的,经常出现的场景,基本工具应该足够了,对吧?

最小测试代码:

public class MainActivity extends AppCompatActivity {

    static int creationCounter = 0;
    Integer instanceId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ++creationCounter;
        instanceId = new Integer(creationCounter);
        Log.d("TEST", "creating activity " + this + ", has id " + instanceId);
    }
}
Run Code Online (Sandbox Code Playgroud)

和测试类:

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void useAppContext() throws Exception {

        MainActivity activity1 = mActivityTestRule.getActivity();
        int act1 = activity1.instanceId.intValue();
        int counter1 = MainActivity.creationCounter;
        assertEquals(1, counter1);
        assertEquals(1, act1);


        Log.d("TEST", …
Run Code Online (Sandbox Code Playgroud)

java instrumentation android automated-tests android-junit

9
推荐指数
1
解决办法
1709
查看次数

Android RecyclerView适配器项目计数在单元测试时返回0

我正在尝试使用AndroidJunit4测试RecyclerView,这是我的测试代码:

@Rule
    public ActivityTestRule<ProductListActivity> rule  = new  ActivityTestRule<>(ProductListActivity.class);

............................
..........................

@Test
    public void ensureDataIsLoadingOnSuccess() throws Exception {
        ProductListActivity activity = rule.getActivity();
        ...........................
        ............

        activity.runOnUiThread(new Runnable() {
        public void run() {
            activity.displayProducts(asList(product1, product2), 0);
        }
    });

        assertEquals(2, mAdapter.getItemCount());
        assertThat(((ProductAdapter) mAdapter).getItemAtPosition(0),sameInstance(product1));
        assertThat(((ProductAdapter) mAdapter).getItemAtPosition(1),sameInstance(product2));


    }
Run Code Online (Sandbox Code Playgroud)

这是我在Activity中的displayProducts()代码:

@Override
    public void displayProducts(List<Product> products, Integer pageNo) {
        progressBar.setVisibility(View.GONE);
        if (pageNo == 0 && products.size() == 0) {
            noProductTextView.setVisibility(View.VISIBLE);
        } else {
            mProductAdapter.addProduct(products);
            noProductTextView.setVisibility(View.GONE);
            productListView.setVisibility(View.VISIBLE);
        }
    }
Run Code Online (Sandbox Code Playgroud)

它给出的错误如下:

junit.framework.AssertionFailedError: expected:<2> but was:<0>
at junit.framework.Assert.fail(Assert.java:50)
at junit.framework.Assert.failNotEquals(Assert.java:287)
at …
Run Code Online (Sandbox Code Playgroud)

android unit-testing android-adapter android-junit

9
推荐指数
1
解决办法
722
查看次数

如何使用 Gradle 命令在 Android 中运行单个测试类?

在我的 Android 应用程序中,我有几个测试类。如果我运行以下命令,./gradlew connectedAndroidTest它将运行 android test 文件夹中的所有测试用例并为所有测试类生成测试报告,但我需要测试特定的测试类并为我尝试使用以下命令的测试类生成报告:

./gradlew test --tests com.login.user.UserLoginTest
Run Code Online (Sandbox Code Playgroud)

上面的命令抛出以下异常

FAILURE: Build failed with an exception.

* What went wrong:
Problem configuring task :app:test from command line.
> Unknown command-line option '--tests'.

* Try:
Run gradlew help --task :app:test to get task usage details. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at …
Run Code Online (Sandbox Code Playgroud)

android unit-testing android-gradle-plugin android-instrumentation android-junit

9
推荐指数
1
解决办法
5851
查看次数

自定义测试协调器或开始/结束回调

我正在使用Android Test Orchestrator来运行我的 UI 测试,并且需要在所有测试完成后生成结果报告。我需要在编排器开始运行测试以及完成运行所有测试时进行回调,以便我可以生成并保存报告。

鉴于为每个测试创建了一个新实例AndroidJUnitRunner,我无法利用onStart()finish()做到这一点。

我正在寻找一种方法来提供自定义编排器,以便我可以使用内部方法。

android android-testing android-espresso android-junit androidjunitrunner

7
推荐指数
0
解决办法
215
查看次数

Espresso 测试给出:没有配置 Koin 上下文。请使用 startKoin 或 koinApplication DSL

我正在运行一个 espresso uiautomator 测试,该测试在 android studio 上使用绿色运行 > 按钮时运行良好。(下图)

然而./gradlew connectedAndroidTest是给出一个错误:

No Koin Context configured. Please use startKoin or koinApplication DSL
Run Code Online (Sandbox Code Playgroud)

为什么它通过 android studio 而不是在 gradle 上运行?我该如何解决?

@LargeTest
@RunWith(AndroidJUnit4::class)
class MainActivityTest {
    @Rule
    @JvmField
    var mActivityTestRule = ActivityTestRule(MainActivity::class.java)

    lateinit var context: Context
    lateinit var mainActivity: MainActivity
    lateinit var idlingResource: MainActivityIdlingResource
    private lateinit var myDevice: UiDevice
    private val sleepMedium: Long = 1000

    @Before
    fun setup() {
        context = InstrumentationRegistry.getInstrumentation().targetContext
        mainActivity = mActivityTestRule.activity
        myDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
        idlingResource = MainActivityIdlingResource(
            mActivityTestRule.activity.recyclerList, …
Run Code Online (Sandbox Code Playgroud)

android android-espresso android-junit

6
推荐指数
1
解决办法
250
查看次数