我是Android Studio的新手。对不起,如果解决方案真的很容易;我在尝试运行JUnit测试时遇到初始化错误。
这是我的JUnit测试:
package mu.organyze;
import android.app.Activity;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.rule.ActivityTestRule;
import android.widget.TextView;
import org.junit.Rule;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import android.widget.Button;
import static org.mockito.Mockito.*;
//mock creation
@RunWith(AndroidJUnit4.class)
public class UnitTestForHomeScreen {
//
@Rule
public ActivityTestRule<HomeScreen> mActivityRule = new ActivityTestRule(HomeScreen.class);
@Test
public void checkLogOutButton() throws Exception {
Button buttonLogOut = mock(Button.class);
when(buttonLogOut.getId()).thenReturn(R.id.buttonLogOut);
HomeScreen activity = mActivityRule.getActivity();
activity.onClick(buttonLogOut);
//assert continas after LogOut button is push
}
@Test
public void checkStoreButton() throws Exception {
Button storeButton = mock(Button.class);
when(storeButton.getId()).thenReturn(R.id.buttonStoreData);
HomeScreen activity = mActivityRule.getActivity();
activity.onClick(storeButton);
//assert continas after storeButton is push
}
@Test
public void checkSearchButton() throws Exception {
Button searchButton = mock(Button.class);
when(searchButton.getId()).thenReturn(R.id.buttonSearch);
HomeScreen activity = mActivityRule.getActivity();
activity.onClick(searchButton);
//assert contains after searchButton button is push
}
@Test
public void checkRankingButton() throws Exception {
Button rankingButton = mock(Button.class);
when(rankingButton.getId()).thenReturn(R.id.buttonRankings);
HomeScreen activity = mActivityRule.getActivity();
activity.onClick(rankingButton);
//assert contains after LogOut button is push
}
@Test
public void check() throws Exception {
Button storeButton = mock(Button.class);
when(storeButton.getId()).thenReturn(R.id.buttonStoreData);
HomeScreen activity = mActivityRule.getActivity();
activity.onClick(storeButton);
//assert contains after LogOut button is push
}
// public void checkInputs() throws Exception {
// Activity activity = mActivityRule.getActivity();
// assertNotNull(activity.findViewById(R.id.textHomeMessage));
// TextView textHomeMessage = (TextView) activity.findViewById(R.id.textHomeMessage);
// assertTrue(textHomeMessage.isShown());
// assertEquals("What do you want to do?", textHomeMessage.getText());
// assertNull(activity.findViewById(R.id.buttonStoreData));
// StoreData itemAdding = new StoreData();
// itemAdding.editTextCount.setText("2");
// assertArrayEquals(itemAdding.getCountItem(),"2");
// }
}
这是我的build.gradle文件。
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "mu.organyze"
minSdkVersion 24
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.firebase:firebase-auth:10.0.1'
compile 'com.google.firebase:firebase-database:10.0.1'
testCompile 'junit:junit:4.12'
testCompile 'com.android.support:support-annotations:23.4.0'
testCompile 'com.android.support.test:runner:0.4.1'
testCompile 'com.android.support.test:rules:0.4.1'
testCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
testCompile 'com.google.dexmaker:dexmaker:1.2'
testCompile "org.mockito:mockito-core:2.12.0"
testCompile 'org.mockito:mockito-android:2.12.0'
androidTestUtil 'com.android.support.test:orchestrator:1.0.1'
androidTestCompile 'org.hamcrest:hamcrest-integration:1.3'
}
apply plugin: 'com.google.gms.google-services'
我尝试过其他人的解决方案,例如: 更改我的运行配置,更改了我的JUnit文件中的导入。
错误讯息:
java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass)
at org.junit.runners.model.InitializationError.<init>(InitializationError.java:38)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:111)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)