解决 Android 单元测试 No tests found for given includes:

问题

报错:

Execution failed for task ':testDebugUnitTest'.

> No tests found for given includes:

解决方案

1、一开始以为是没有给测试类加public修饰

2、然后替换 @Test 注解的包可以解决,将 org.junit.jupiter.api.Test 修改为 org.junit.Test

java 复制代码
import org.junit.Test; //OK

import org.junit.jupiter.api.Test;//No use

org.junit.jupiter.api.Test是IDE 插件默认生成Test文件的引用。

两个类的实现是不一样的,介绍如下:

org.junit.jupiter.api

java 复制代码
package org.junit.jupiter.api;

@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@API(status = STABLE, since = "5.0")
@Testable
public @interface Test {
}

org.junit

java 复制代码
package org.junit;


@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Test {

    /**
     * Default empty exception.
     */
    static class None extends Throwable {
        private static final long serialVersionUID = 1L;

        private None() {
        }
    }

    /**
     * Optionally specify <code>expected</code>, a Throwable, to cause a test method to succeed if
     * and only if an exception of the specified class is thrown by the method. If the Throwable's
     * message or one of its properties should be verified, the
     * {@link org.junit.rules.ExpectedException ExpectedException} rule can be used instead.
     */
    Class<? extends Throwable> expected() default None.class;

    long timeout() default 0L;
}

Note:使用 org.junit.Test 的测试方法test要被public修饰。

后续报错

相关推荐
儿歌八万首3 分钟前
Compose 自定义组件:封装一个通用标题栏
android·compose·标题栏
ZHOUPUYU13 分钟前
PHP性能优化实战:提升你的应用速度
android·性能优化·php
Railshiqian36 分钟前
安卓源码编译ko文件到设备img,并在开机阶段自动加载
android·kernel
NoSi EFUL2 小时前
学生成绩管理系统(MySQL)
android·数据库·mysql
molong9312 小时前
SIM 卡监听(电话监听)
android·学习·kotlin
帅次2 小时前
Android 高级工程师面试参考答案:Framework、生命周期、View 与 Binder
android·面试·binder
程序员陆业聪2 小时前
AI提效Android开发全景图:从需求到上线的AI工具链
android
李白的天不白2 小时前
滚动条样式大全
android
程序员陆业聪3 小时前
你调的每一个接口背后,到底发生了什么?
android
Railshiqian3 小时前
common-android15-6.6 kernel环境下,编写并编译一个helloworld驱动模块
android·kernel