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

后续报错

相关推荐
Coffeeee2 小时前
如何使用Glide和Coil加载WebP动图
android·kotlin·glide
Kapaseker3 小时前
5 分钟搞懂 Kotlin DSL
android·kotlin
恋猫de小郭4 小时前
AI Agent 开发究竟是啥?如何用 AI 开发 Agent ?深入浅出给你一套概念
android·前端·ai编程
黄林晴4 小时前
Android 17 正式发布!target 37 一大批旧代码直接不能用了
android
Carson带你学Android4 小时前
Android 17 正式发布:AI 终于成了系统能力
android·前端·ai编程
三少爷的鞋5 小时前
当 UseCase 开始长期监听,它可能已经不是 UseCase 了
android
恋猫de小郭18 小时前
Android 限制侧载新进展,谷歌联合国内厂商推验证计划
android·前端·flutter
恋猫de小郭18 小时前
解读 Android 17 全新内存限制,有没有“豁免”后门?
android·前端·flutter
贾艺驰20 小时前
实战Android Framework: 新增一个系统权限
android
alexhilton1 天前
使用Android Archive进行打包
android·kotlin·android jetpack