gpt4 book ai didi

android - 无法监视 Android 类

转载 作者:太空狗 更新时间:2023-10-29 12:40:12 27 4
gpt4 key购买 nike

我有一个像这样的简单的 android 常量类:

public class Consts extends BaseConstants {

public static final String SCHEME = "http";

// Tag used to cancel the request
public static final String TAG_JSON = "json_obj_req";
}

里面没有别的东西,所以模拟起来应该很简单。我在我的测试用例中调用它:

Mockito.spy(Consts.class); ...这是失败的。下面是测试用例文件:

public class ApplicationTest extends ActivityInstrumentationTestCase2<MainActivity> {

MainActivity mActivity;

public ApplicationTest() {
super(MainActivity.class);
}

@Override
protected void setUp() throws Exception {
super.setUp();

setActivityInitialTouchMode(false);

mActivity = getActivity();
}

public void testUrlValid() {
Mockito.spy(Consts.class);
}

}

这是测试用例的 logcat 输出:

Running tests
Test running started
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class java.lang.Class
Mockito cannot mock/spy following:
- final classes
- anonymous classes
- primitive types

--------更新:

我想监视我的 mainActivity 类,但我得到了相同的 Mockito 异常:这是我正在测试的类:

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ListPageActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new SummaryFragment(),"SummaryFragment")
.commit();
}

loadBrandSound();

if (!isNetworkAvailable())
showToast(getString(R.string.no_network));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

这是我的简单测试用例:

import android.test.ActivityInstrumentationTestCase2;
import android.widget.Button;
import android.widget.EditText;

import org.mockito.Mockito;

public class ApplicationTest extends ActivityInstrumentationTestCase2<MainActivity> {

MainActivity mActivity;
private Button goBtn;
private EditText et_query;
private RecyclerListAdapter mAdapter;

public ApplicationTest() {
super(MainActivity.class);
}

@Override
protected void setUp() throws Exception {
super.setUp();

setActivityInitialTouchMode(false);

mActivity = getActivity();

goBtn=(Button)mActivity.findViewById(
R.id.btn_go);
et_query = (EditText)mActivity.findViewById(R.id.et_query);
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
}

public void testPreconditions() {
assertTrue(mActivity.isNetworkAvailable());
isLayoutValid();
}

public void isLayoutValid(){
assertNotNull(goBtn);
}

public void testSomething(){
Mockito.spy(MainActivity.class);
}

}

为什么它在 spy 线上失败了?这是日志:

    Running tests
Test running started
org.mockito.exceptions.base.MockitoException:
Cannot mock/spy class java.lang.Class
Mockito cannot mock/spy following:
- final classes
- anonymous classes
- primitive types
at mypackage.ApplicationTest.testSomething(ApplicationTest.java:65)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1729)

Finish

最佳答案

不同于Mockito.mock(Class<T> clazz) , Mockito.spy(T instance)举个例子。但是,两者都返回一个实例。您要替换静态字段,而不是实例方法。

Mockito 不能模拟静态成员或最终类,因为它主要通过创建匿名代理实现或子类来工作。子类不能覆盖静态方法或扩展最终类,因此 Java 编译器采用了一种跳过任何 Mockito 提供的实现的快捷方式。 (对于静态最终常量尤其如此,它们是 inlined at compile time 。)

虽然PowerMockito可以重写被测系统代码以模拟最终和静态方法调用,它对字段没有帮助。额外的间接级别可能是更好的解决方案。

public class MyApplication extends Application {
public boolean isUrlValid(String url) {
return isUrlValid(url, Consts.SCHEME, Consts.TAG_JSON);
}

static boolean isUrlValid(String url, String scheme, String jsonTag) {
// ...
}
}

public class MyApplicationTest {
@Test public void urlValidShouldWork() {
assertTrue(MyApplication.isUrlValid(VALID_URL, "fakescheme", "faketag");
}
}

作为替代方案,让 Consts 根据访问器方法 (getScheme) 而不是字段 (scheme) 进行操作。像 ProGuard 这样的工具通常可以内联简单的方法调用,因此它在生产中不应该变得更慢或更冗长,而且您将有更多机会在测试中替换实现。有关在测试中插入/注入(inject)模拟实现的更多技术,see this SO question .

关于android - 无法监视 Android 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27596329/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com