解决 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修饰。

后续报错

相关推荐
_祝你今天愉快18 小时前
Android 高级绘制:[四] Canvas-实际案例操作
android
三少爷的鞋19 小时前
Android 工程师的技术信仰崩塌史
android
AirDroid_cn21 小时前
Android 15 权限回溯:已授予的麦克风权限如何批量回收?
android
Mr_万能胶1 天前
官方发布,不烧心的 AOSP 编译提速技巧
android·ci/cd·架构
booboosui1 天前
安卓 8.1 也能装了:代价是一条黄字和两个静默 bug
android
Ehtan_Zheng1 天前
Jetpack AppState Transform:让 Compose 的响应式状态走出 UI 层
android
yyuuuzz1 天前
nginx 502 排查,先看上游超时那一行
android·java·运维·nginx·502
恋猫de小郭1 天前
Flutter 3.50.0 Release 的第一个崩溃,修好了,还没 hotfix
android·前端·flutter
alexhilton2 天前
MVI 让屏幕一次只有一个状态
android·kotlin·android jetpack
二流小码农2 天前
鸿蒙开发:证书只能创建3个,第4个应用怎么办
android·ios·harmonyos