我正在尝试为android编写我的第一个单元测试。我使用像expamle这样的计算器。我为方法(加上,减去...)创建了类计算器,并在创建的类CalculatorTest中使用它。
public class CalculatorTest {
private Calculator calculator;
@Before
public void setUp() throws Exception {
calculator = new Calculator();
}
@Test
public void addition() throws Exception {
assertEquals(3, calculator.add(1, 2));
}
}
我的朋友是:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.azhimkulov.azamat.myfirsttest"
minSdkVersion 19
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 {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
compile 'junit:junit:4.12'
}
当我尝试启动测试时,我发现了这个错误:
"/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java" -ea -javaagent:/Users/azamat/Library/Caches/AndroidStudio3.0/coverageJars/coverage-agent.jar=/private/var/folders/10/1djpvw7914jcdgcvj3mqb1fc0000gn/T/coverage410args -Didea.test.cyclic.buffer.size=1048576 "-javaagent:/Applications/Android Studio.app/Contents/lib/idea_rt.jar=63120:/Applications/Android Studio.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath "/Applications/Android Studio.app/Contents/lib/idea_rt.jar:/Applications/Android Studio.app/Contents/plugins/testng/lib/testng-plugin.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/charsets.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/localedata.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/sunec.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/jce.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/jsse.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/management-agent.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/resources.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/jre/lib/rt.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/lib/dt.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/lib/jconsole.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/lib/sa-jdi.jar:/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/lib/tools.jar:/Users/azamat/AndroidStudioProjects/Lessons_Moduls/MyFirstTest/app/build/intermediates/classes/test/debug:/Users/azamat/AndroidStudioProjects/Lessons_Moduls/MyFirstTest/app/build/intermediates/classes/debug:/Users/azamat/.gradle/caches/transforms-1/files-1.1/support-media-compat-26.1.0.aar/0c072cf83f4f466eeebb08359ff9fd8b/res:/Users/azamat/.gradle/caches/transforms-1/files-1.1/support-media-compat-26.1.0.aar/0c072cf83f4f466eeebb08359ff9fd8b/jars/classes.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/2973d150c0dc1fefe998f834810d68f278ea58ec/junit-4.12.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/android.arch.lifecycle/common/1.0.0/e414a4cb28434e25c4f6aa71426eb20cf4874ae9/common-1.0.0.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/support-fragment-26.1.0.aar/a7b5013e0e25b8e899e1d4158c630e62/jars/classes.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/espresso-core-3.0.2.aar/f12778d36371e3bcf33bb5c44c51ff27/jars/classes.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/javax.inject/javax.inject/1/6975da39a7040257bd51d21a231b76c915872d38/javax.inject-1.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/com.squareup/javawriter/2.1.1/67ff45d9ae02e583d0f9b3432a5ebbe05c30c966/javawriter-2.1.1.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/support-vector-drawable-26.1.0.aar/bf841f91dcb3ffae6d9316e799a933d4/jars/classes.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/runtime-1.0.0.aar/de65bfbb2493bc2c2b9573c3cfd8d70d/jars/classes.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/appcompat-v7-26.1.0.aar/19476b8eee8d91ed05ed7302a415fe08/jars/classes.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/appcompat-v7-26.1.0.aar/19476b8eee8d91ed05ed7302a415fe08/res:/Users/azamat/.gradle/caches/transforms-1/files-1.1/monitor-1.0.2.aar/5f2ca3d843ca303ce4370584a66aaf11/jars/classes.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/com.android.support/support-annotations/26.1.0/814258103cf26a15fcc26ecce35f5b7d24b73f8/support-annotations-26.1.0.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/constraint-layout-1.1.2.aar/b9bad247bc5001b1cc263ed2e699bb4d/res:/Users/azamat/.gradle/caches/transforms-1/files-1.1/constraint-layout-1.1.2.aar/b9bad247bc5001b1cc263ed2e699bb4d/jars/classes.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/org.mockito/mockito-core/1.10.19/e8546f5bef4e061d8dd73895b4e8f40e3fe6effe/mockito-core-1.10.19.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/support-core-utils-26.1.0.aar/db1eaf42c81aaf10582dd145b8087d30/jars/classes.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/support-core-ui-26.1.0.aar/1b7187a25f0669767da70cf0db91e8b3/jars/classes.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/runner-1.0.2.aar/e14ba332a792d4829e85e12458d734e6/jars/classes.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/org.objenesis/objenesis/2.1/87c0ea803b69252868d09308b4618f766f135a96/objenesis-2.1.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/com.google.code.findbugs/jsr305/2.0.1/516c03b21d50a644d538de0f0369c620989cd8f0/jsr305-2.0.1.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/espresso-idling-resource-3.0.2.aar/f059bdc031aed8b4270b827d3d821724/jars/classes.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/support-compat-26.1.0.aar/81e9661d3a98cf3b5aafadea2af8efee/res:/Users/azamat/.gradle/caches/transforms-1/files-1.1/support-compat-26.1.0.aar/81e9661d3a98cf3b5aafadea2af8efee/jars/classes.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/android.arch.core/common/1.0.0/a2d487452376193fc8c103dd2b9bd5f2b1b44563/common-1.0.0.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-library/1.3/4785a3c21320980282f9f33d0d1264a69040538f/hamcrest-library-1.3.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-integration/1.3/5de0c73fef18917cd85d0ab70bb23818685e4dfd/hamcrest-integration-1.3.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/support-v4-26.1.0.aar/416d0fa58e653bc8f4e625b824b1d99e/jars/classes.jar:/Applications/Android Studio.app/Contents/gradle/m2repository/net/sf/kxml/kxml2/2.3.0/kxml2-2.3.0.jar:/Users/azamat/.gradle/caches/transforms-1/files-1.1/animated-vector-drawable-26.1.0.aar/b064583e29047fbe714f1d80a017aaf6/jars/classes.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/com.android.support/support-annotations/27.1.1/39ded76b5e1ce1c5b2688e1d25cdc20ecee32007/support-annotations-27.1.1.jar:/Users/azamat/.gradle/caches/modules-2/files-2.1/com.android.support.constraint/constraint-layout-solver/1.1.2/bfc967828daffc35ba01c9ee204d98b664930a0f/constraint-layout-solver-1.1.2.jar:/Applications/Android Studio.app/Contents/plugins/testng/lib/jcommander.jar" org.testng.RemoteTestNGStarter -usedefaultlisteners false -socket63119 @w@/private/var/folders/10/1djpvw7914jcdgcvj3mqb1fc0000gn/T/idea_working_dirs_testng.tmp -temp /private/var/folders/10/1djpvw7914jcdgcvj3mqb1fc0000gn/T/idea_testng.tmp
---- IntelliJ IDEA coverage runner ----
sampling ...
include patterns:
com\.azhimkulov\.azamat\.myfirsttest\..*
exclude patterns:
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123)
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 13 more
Process finished with exit code 1
我试图在谷歌中找到解决方案。我去运行/调试配置并找到一些东西,但无法理解如何决定:
无法访问CalculatorTest:
答案 0 :(得分:0)
尝试在build.gradle中添加它
testCompile 'org.assertj:assertj-core:2.0.0'
testCompile 'org.testng:testng:6.9.10'
或下载this jar文件
并在build.gradle中添加此行
compile group: 'org.beanshell', name: 'bsh', version: '2.0b4'
为您的类创建一个默认构造函数,如
public CalculatorTest(){
super(ActivityYouWantToTest.class);
}
如果以上方法不起作用,也请尝试Build -> Rebuild Project