在 Android 仪器测试 - 错误 - 重复文件 - org.mockito.plugins.MockMaker中使



我正在尝试使用PowerMock来模拟具有静态方法的类,但具体而言,我希望在Android仪器测试中执行此操作。需要明确的是,我希望在真正的Android设备或模拟器上运行测试。我正在使用Android Studio(1.5.1(和Gradle(1.5.0(。为了避免任何红鲱鱼,我创建了一个非常基本且相当粗糙的"hello world"应用程序。此应用程序仅显示 2 段文本,一条从静态方法检索,另一段从非静态方法检索。我已经为这两个"文本提供程序"类编写了检测测试。你可以在这里看到这个应用程序:

https://github.com/Kai2k/PowerMockAndroidTest.git

尝试运行检测测试时,我收到错误,似乎许多人试图实现此目的:

com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK mockito-extensions/org.mockito.plugins.MockMaker
File1: /Users/kaiarmer/.gradle/caches/modules-2/files-2.1/com.crittercism.dexmaker/dexmaker-mockito/1.4/70892a94894462c1b35df3c8a77d21b7e843550b/dexmaker-mockito-1.4.jar
File2: /Users/kaiarmer/.gradle/caches/modules-2/files-2.1/org.powermock/powermock-api-mockito/1.6.4/fe12509b7e9e49d25131f4155145748a31e42e40/powermock-api-mockito-1.6.4.jar

我对应用程序的 build.gradle 文件的依赖关系如下所示:

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock-api-mockito:1.6.4'
testCompile 'org.powermock:powermock-module-junit4-rule-agent:1.6.4'
testCompile 'org.powermock:powermock-module-junit4-rule:1.6.4'
testCompile 'org.powermock:powermock-module-junit4:1.6.4'
androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'org.powermock:powermock-api-mockito:1.6.4'
androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'com.crittercism.dexmaker:dexmaker:1.4'
androidTestCompile 'com.crittercism.dexmaker:dexmaker-mockito:1.4'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
}

你会注意到一些对 power mock 和相关库(除了 androidTestCompile(的"testCompile"依赖。这是因为对于腰带和牙套,我想看看我是否可以让 junit(非仪器(测试(使用功率模拟(也起作用。我能够做到这一点,但不能通过仪器测试。这是我的(可怕的(代码:

主要活动:

package com.example.android.powermocktest;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    TextView textView1 = (TextView)findViewById(R.id.textView1);
    textView1.setText(GetStaticValue.getTheStaticValue());
    TextView textView2 = (TextView)findViewById(R.id.textView2);
    textView2.setText(new GetNonStaticValue().getNonStaticString());
}
@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);
}
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout         xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.android.powermocktest.MainActivity">
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.android.powermocktest.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView1"
    android:text="Hello World!" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:id="@+id/textView2"
    android:text="Hello World again!" />
</RelativeLayout>

以及获取静态和非静态值的类:

package com.example.android.powermocktest;
public class GetStaticValue {
private final static String THE_VALUE = "Hi, I'm static";
public static String getTheStaticValue(){
    return THE_VALUE;
}
}
package com.example.android.powermocktest;
public class GetNonStaticValue {
public String getNonStaticString(){
    return "Hi, I'm non-static";
}
}

最后是测试类。首先是仪器测试:

package com.example.android.powermocktest;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(AndroidJUnit4.class)
@PrepareForTest(GetStaticValue.class)
public class GetStaticValueTest {
    @Test
    public void getStaticValueReturnsValue(){
        PowerMockito.mockStatic(GetStaticValue.class);
        when(GetStaticValue.getTheStaticValue()).thenReturn("Hi, I'm static");
        assertThat(GetStaticValue.getTheStaticValue(), equalTo("Hi, I'm static"));
    }
}
package com.example.android.powermocktest;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;
@RunWith(AndroidJUnit4.class)
public class GetNonStaticValueTest {
    @Test
    public void getNonStaticValueReturnsNonStaticValue(){
        GetNonStaticValue getNonStaticValue = Mockito.mock(GetNonStaticValue.class);
        when(getNonStaticValue.getNonStaticString()).thenReturn("Hi, I'm non-static");
        assertThat(getNonStaticValue.getNonStaticString(), equalTo("Hi, I'm non-static"));
    }
}

现在jUnit测试(工作(:

package com.example.android.powermocktest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest(GetStaticValue.class)
public class GetStaticValueTest {
    @Test
    public void getStaticValueReturnsValue() {
        PowerMockito.mockStatic(GetStaticValue.class);
        when(GetStaticValue.getTheStaticValue()).thenReturn("Hi, I'm static");
        assertThat(GetStaticValue.getTheStaticValue(), equalTo("Hi, I'm static"));
    }
}

到目前为止我尝试过:

我已经围绕这个问题做了一些阅读,我发现的一个解决方案,我没有尝试过,是手动打开电源模拟库罐并删除有问题的重复类。由于(在工作中(我们使用 gradle 并且不使用本地罐子,我宁愿不这样做。

如何让Powermock与Dexmaker一起工作

我听说有人建议在gradle构建文件的Android部分中使用"排除"。这不适合运行检测测试,它是(由Android Studio/Gradle(构建为apk并在设备上启动的。因此,需要电源模拟罐来运行测试。

Android Studio: Gradle 依赖项错误

我尝试删除您在上面的构建文件中列出的一些依赖项。例如,我尝试删除"org.powermock:powermock-api-mockito"依赖项,但是例如需要使用"mockStatic"。

似乎有人在使用 Maven 时成功了,通过告诉 Maven 排除重复项:

https://groups.google.com/forum/#!searchin/powermock/android/powermock/ndZ2ZliYGCY/Eh226605u2cJ

PowerMock + Mockito + Maven on Android 应用程序显示 Dex 加载器错误

我试图看看是否有办法告诉 Gradle 忽略依赖项 jar 中的重复类,但到目前为止我没有成功。

我觉得这是值得追求的,因为首先,权力模拟在谨慎使用时非常有用,其次,我不敢相信这个问题还没有得到解决(以前肯定遇到过!

最后一点,我确实注意到谷歌自己似乎天生就暗示模拟只适用于单元测试,而不是仪器测试:

http://developer.android.com/training/testing/start/index.html

任何人可以提供的任何帮助将不胜感激。谢谢。

唯一正确的答案是PowerMockito不支持Android使用的Davik VM,它适用于标准的JVM。 因此,不能将其用于插桩测试,只能用于单元测试。

我能够通过以下方式解决此问题。如果您看到任何错误消息,指出在 APK [文件名] 中复制了重复的文件,请在打包选项中添加要排除的 [文件名]。

android {
    packagingOptions {
        exclude 'mockito-extensions/org.mockito.plugins.MockMaker'
    }
}

你在你的项目/模块gradle中有这个吗?

packagingOptions {
    exclude 'fileNameYouWantToExclude'
}

这样,如果发现重复项,Androd 将只放置一个文件

相关内容

最新更新