AndroidStudio3.5中进行单例测试,一直No tests found in

AS中提供了一个专门用来做单例测试的包
在这里插入图片描述
正常情况下我们模拟ExampleInstrumentedTest去写单例情况都是可以单例测试成功的。
MyTest.java

package com.example.myapplication;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class MyTest {

    @Test
    public void testPrint(){
        System.out.println("单例测试");
    }
}

点击debug运行后都会成功的运行单例
在这里插入图片描述
但是我之前两天做项目时它不知道怎么了,debug单例时一直报错:
在这里插入图片描述
百度了一波有的说要在Edit Configuration里面做如下配置:在这里插入图片描述
有的叫我在gradle里面加入依赖:

 androidTestImplementation 'androidx.test:rules:1.2.0'
 androidTestImplementation 'androidx.test:runner:1.2.0'

都没有得到解决。
后来自己手抽给单例测试类继承了TestCase解决了问题

package com.example.xmpp;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import junit.framework.TestCase;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest extends TestCase {
    @Test
    public void useAppContext() {
        System.out.println("111122");
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        assertEquals("com.example.xmpp", appContext.getPackageName());
    }
}

注意是导入==import junit.framework.TestCase;==这个包。

在这里插入图片描述
虽然说依然有报错,但是可以看到目前的的单例时执行了的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值