解决 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 美颜相机】第十二天:GPUImageFilterGroup 源码解析
android·数码相机
_李小白18 小时前
【Android GLSurfaceView源码学习】第三天:GLSurfaceView的Surface、GLES与EGLSurface的关联
android·学习
技术摆渡人18 小时前
专题三:【Android 架构】全栈性能优化与架构演进全书
android·性能优化·架构
花卷HJ18 小时前
Android 10+ 使用 WifiNetworkSpecifier 连接指定 WiFi(完整封装 + 实战)
android
前端世界18 小时前
鸿蒙系统中时间与日期的国际化实践:一次把不同文化显示问题讲清楚
android·华为·harmonyos
木卫四科技18 小时前
【Claude Agent - 入门篇】:从原生 SDK 到自主智能体
android
2501_9159184118 小时前
Mac 抓包软件有哪些?Charles、mitmproxy、Wireshark和Sniffmaster哪个更合适
android·ios·小程序·https·uni-app·iphone·webview
2501_9151063218 小时前
iOS 抓包绕过 SSL 证书认证, HTTPS 暴力抓包、数据流分析
android·ios·小程序·https·uni-app·iphone·ssl
2501_915921431 天前
iOS App 电耗管理 通过系统电池记录、Xcode Instruments 与克魔(KeyMob)组合使用
android·ios·小程序·https·uni-app·iphone·webview
June bug1 天前
【配环境】安卓项目开发环境
android