请原谅我,如果我的问题看起来重复,但我没有得到如何测试改造API调用.
应用程序级别的build.gradle
dependencies {
compile filetree(dir: 'libs', include: ['*.jar'])
androidTestCompile("com.android.support.test.espresso:espresso-core:$rootProject.ext.expressoVersion", {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile "com.android.support:appcompat-v7:$rootProject.ext.supportLibraryVersion"
compile "com.jakewharton:butterknife:$rootProject.ext.butterKnifeVersion"
annotationProcessor "com.jakewharton:butterknife-compiler:$rootProject.ext.butterKnifeVersion"
// Dependencies for local unit tests
testCompile "junit:junit:$rootProject.ext.junitVersion"
testCompile "org.mockito:mockito-all:$rootProject.ext.mockitoVersion"
testCompile "org.hamcrest:hamcrest-all:$rootProject.ext.hamcrestVersion"
testCompile "org.powermock:powermock-module-junit4:$rootProject.ext.powermockito"
testCompile "org.powermock:powermock-api-mockito:$rootProject.ext.powermockito"
compile "com.android.support.test.espresso:espresso-idling-resource:$rootProject.ext.espressoVersion"
// retrofit, gson
compile "com.google.code.gson:gson:$rootProject.ext.gsonVersion"
compile "com.squareup.retrofit2:retrofit:$rootProject.ext.retrofitVersion"
compile "com.squareup.retrofit2:converter-gson:$rootProject.ext.retrofitVersion"
}
项目级的build.gradle有这个额外的内容
//在一个地方定义版本
ext {
// Sdk and tools
minSdkVersion = 15
targetSdkVersion = 25
compileSdkVersion = 25
buildToolsversion = '25.0.2'
supportLibraryVersion = '23.4.0'
junitVersion = '4.12'
mockitoVersion = '1.10.19'
powermockito = '1.6.2'
hamcrestVersion = '1.3'
runnerVersion = '0.5'
rulesversion = '0.5'
espressoVersion = '2.2.2'
gsonVersion = '2.6.2'
retrofitVersion = '2.0.2'
butterKnifeVersion = '8.5.1'
expressoVersion = '2.2.2'
}
主要活动
public class MainActivity extends AppCompatActivity implements MainView {
@BindView(R.id.textViewApiData)
TextView mTextViewApiData;
@BindView(R.id.progressBarLoading)
ProgressBar mProgressBarLoading;
private MainPresenter mMainPresenter;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initializeComponents();
}
private void initializeComponents() {
mMainPresenter = new MainPresenter(this);
mMainPresenter.presentDataFromApi();
}
@Override
public void onResponseReceived(final String response) {
mTextViewApiData.setText(response);
}
@Override
public void one rrorReceived(final String message) {
mTextViewApiData.setText(message);
}
@Override
public void showProgressDialog(final boolean enableProgressDialog) {
mProgressBarLoading.setVisibility(enableProgressDialog ? View.VISIBLE : View.GONE);
}
}
的MainView
public interface MainView {
void onResponseReceived(String response);
void one rrorReceived(String message);
void showProgressDialog(boolean enableProgressDialog);
}
public class apiclient {
private static Retrofit sRetrofit;
public static Retrofit getInstance() {
if (sRetrofit == null) {
sRetrofit = new Retrofit.Builder()
.baseUrl(Constants.Urls.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return sRetrofit;
}
}
主持人
public class MainPresenter {
private final MainView mMainView;
private final Call<List<UserResponse>> mCallListUserResponse;
public MainPresenter(final MainView mainView) {
this.mMainView = mainView;
final ApiInterface apiInterface = apiclient.getInstance().create(ApiInterface.class);
mCallListUserResponse = apiInterface.getUsers();
}
public void presentDataFromApi() {
mMainView.showProgressDialog(true);
mCallListUserResponse.enqueue(new Callback<List<UserResponse>>() {
@Override
public void onResponse(final Call<List<UserResponse>> call,
final Response<List<UserResponse>> response) {
mMainView.onResponseReceived(Constants.DummyData.SUCCESS);
mMainView.showProgressDialog(false);
}
@Override
public void onFailure(final Call<List<UserResponse>> call, final Throwable t) {
mMainView.onErrorReceived(Constants.DummyData.ERROR);
mMainView.showProgressDialog(false);
}
});
}
}
ApiInterface
public interface ApiInterface {
@GET(Constants.Urls.USERS)
Call<List<UserResponse>> getUsers();
}
常量
public class Constants {
public class Urls {
public static final String BASE_URL = "https://jsonplaceholder.typicode.com";
public static final String USERS = "/users";
}
}
这就是我想要做的事情,但它不起作用.测试用例现在将通过,因为我评论了最后一行中的3行.取消注释这些行后,您可以查看错误.
测试用例
public class MainPresenterTest {
@InjectMocks
private MainPresenter mMainPresenter;
@Mock
private MainView mMockMainView;
@Mock
private Call<List<UserResponse>> mUserResponseCall;
@Captor
private ArgumentCaptor<Callback<List<UserResponse>>> mArgumentCaptorUserResponse;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void presentDataFromApitest() throws Exception {
mMainPresenter.presentDataFromApi();
verify(mMockMainView).showProgressDialog(true);
// verify(mUserResponseCall).enqueue(mArgumentCaptorUserResponse.capture());
// verify(mMockMainView).onResponseReceived(Constants.DummyData.SUCCESS);
// verify(mMockMainView).showProgressDialog(false);
}
}
日志
Wanted but not invoked:
mUserResponseCall.enqueue(
<Capturing argument>
);
-> at com.example.ranaranvijaysingh.testingdemo.presenters.MainPresenterTest.presentDataFromApiTest(MainPresenterTest.java:69)
Actually, there were zero interactions with this mock.
解决方法:
您的代码在语法上看起来正确.但是,我怀疑@InjectMock无法将模拟对象注入最终的实例变量.当您调用mMainPresenter.presentDataFromApi()时,下面的变量可能被用作实例:
private final Call<List<UserResponse>> mCallListUserResponse;
您应该尝试将mock变量手动注入此类并分配给mCallListUserResponse以便能够从mockito实例中获取.
可能值得尝试以下步骤:
>将MainPresenter中的变量mCallListUserResponse设为非final.
>在MainPresenter类中添加一个方法,如下所示:
void setUserResponseCall(Call> userResponse){
mCallListUserResponse = userResponse;
}
>现在在Test类中执行以下操作:
修改您的测试,如下所示
@Test
public void presentDataFromApitest() throws Exception {
//Set mock instance of the user response
mMainPresenter.setUserResponseCall(mUserResponseCall);
//real object call to presentDataFromApi();
mMainPresenter.presentDataFromApi();
verify(mMockMainView).showProgressDialog(true);
verify(mUserResponseCall).enqueue(mArgumentCaptorUserResponse.capture());
}
希望能帮助到你.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。