解决 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 小时前
Kotlin 基本数据类型(一):Numbers
android·前端·kotlin
Huntto4 小时前
最小二乘法计算触摸事件速度
android·最小二乘法·触摸事件·速度估计
一笑的小酒馆4 小时前
Android中使用Compose实现各种样式Dialog
android
红橙Darren4 小时前
手写操作系统 - 编译链接与运行
android·ios·客户端
程序员二黑7 小时前
单元测试三大神器:unittest vs JUnit vs Jest 终极对决
单元测试·测试·ab测试
鹏多多.8 小时前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架
来来走走13 小时前
Flutter开发 网络请求
android·flutter
陈天cjq16 小时前
Redis 实用型限流与延时队列:从 Lua 固定/滑动窗口到 Streams 消费组(含脚本与压测)
redis·junit·lua
Warren9816 小时前
Lua 脚本在 Redis 中的应用
java·前端·网络·vue.js·redis·junit·lua
独行soc19 小时前
2025年渗透测试面试题总结-18(题目+回答)
android·python·科技·面试·职场和发展·渗透测试