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

后续报错

相关推荐
yuren_xia1 小时前
使用 JUnit 4在 Spring 中进行单元测试的完整步骤
spring·junit·单元测试
zhishishe2 小时前
2025 年免费 Word 转 PDF 转换器有哪些?
android·windows·pdf·电脑·word
Ya-Jun8 小时前
常用第三方库精讲:cached_network_image图片加载优化
android·flutter
_一条咸鱼_8 小时前
Android嵌套滑动详解
android·面试·android jetpack
zhishishe10 小时前
工具指南:免费将 PDF 转换为 Word 的 10 个工具
android·windows·pdf·word
孙同学_10 小时前
【MySQL】004.MySQL数据类型
android·数据库·mysql
流浪汉kylin13 小时前
Android 图片选择器改系统
android
前行的小黑炭13 小时前
Android 上下位机开发:串口是什么,为什么android版本都比较低?粘包半包的原因以及处理思路,缓冲区处理,以及超时清空缓冲区....
android
移动开发者1号13 小时前
你知道Android中配置resourcePrefix的作用吗?
android
tangweiguo0305198714 小时前
Android Compose 系统 Scope 的优化实践
android