AndroidJUnit4应该有一个公共构造函数
创始人
2024-10-08 15:01:39
0

要解决AndroidJUnit4应该有一个公共构造函数的问题,您可以按照以下步骤进行操作:

  1. 在您的测试类中创建一个公共构造函数。该构造函数应该接受一个Context参数,并将其传递给父类的构造函数。
import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    
    private Context mContext;

    public ExampleInstrumentedTest(Context context) {
        mContext = context;
    }

    // 测试方法...
}
  1. 在测试方法中使用InstrumentationRegistry来获取Context对象,并将其传递给测试类的构造函数。
import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

    private Context mContext;

    public ExampleInstrumentedTest(Context context) {
        mContext = context;
    }

    @Test
    public void exampleTest() {
        // 使用 mContext 进行测试...
    }
}
  1. 创建一个继承自AndroidJUnitRunner的自定义测试运行器,并在其中实例化测试类。
import android.content.Context;
import androidx.test.runner.AndroidJUnitRunner;

public class CustomTestRunner extends AndroidJUnitRunner {

    @Override
    public void onCreate(Bundle arguments) {
        Context context = getContext();
        AndroidJUnitRunner.Builder builder = new AndroidJUnitRunner.Builder(this);
        builder.setTest(new ExampleInstrumentedTest(context));
        setInstrumentation(new InstrumentationWrapper(builder.build()));
        super.onCreate(arguments);
    }
}
  1. 在AndroidManifest.xml文件中指定自定义的测试运行器。

    
        
        
    

通过这些步骤,您可以为AndroidJUnit4测试类添加一个公共构造函数,并在其中接受一个Context参数。这样,您就可以在测试方法中使用Context对象进行必要的操作。

相关内容

热门资讯

透视辅助挂!wepoker透视... 透视辅助挂!wepoker透视底牌(透视)开挂辅助软件(原来存在有挂)-哔哩哔哩一、wepoker透...
透视ios!epoker透视底... 透视ios!epoker透视底牌(透视)开挂辅助工具(切实是有挂)-哔哩哔哩1、该软件可以轻松地帮助...
透视真的!wepoker软件安... 透视真的!wepoker软件安装包,pokemmo辅助脚本,大纲教程(总是真的有挂)-哔哩哔哩1、不...
透视app!hhpoker辅助... 透视app!hhpoker辅助码(透视)开挂辅助插件(切实是有挂)-哔哩哔哩1、进入游戏-大厅左侧-...
透视黑科技!wepoker辅助... 透视黑科技!wepoker辅助器,约局吧辅助脚本,指南书教程(真是存在有挂)-哔哩哔哩1、超多福利:...
透视存在!we poker辅助... 透视存在!we poker辅助器,wepoker怎么看底牌,烘培教程(总是是真的挂)-哔哩哔哩1、w...
透视专用!wepoker脚本下... 透视专用!wepoker脚本下载(透视)开挂辅助器(原来是有挂)-哔哩哔哩1、每一步都需要思考,不同...
透视辅助!wepoker有没有... 透视辅助!wepoker有没有插件,aapoker怎么设置抽水,攻略教程(其实是有挂)-哔哩哔哩在进...
透视必备!pokemmo辅助器... 透视必备!pokemmo辅助器手机版下载(透视)开挂辅助神器(本来是真的挂)-哔哩哔哩1)pokem...
透视安装!hhpoker底牌透... 透视安装!hhpoker底牌透视脚本,hhpoker辅助实战视频,经验教程(一直真的有挂)-哔哩哔哩...