作者:干杯随风一刀_893 | 来源:互联网 | 2023-05-19 14:50
IhaveanAndroidprojectthatshowsHelloWorld.ItwascreatedfromtheBlankActivitytemplat
I have an Android project that shows "Hello World". It was created from the "Blank Activity" template from Android Studio.
我有一个显示“Hello World”的Android项目。它是从Android Studio的“空白活动”模板创建的。
I then add/create a new java class in my application package (the same package that has my activity). I call it Shape and add a simple constructor
然后我在我的应用程序包中添加/创建一个新的java类(与我的活动相同的包)。我称之为Shape并添加一个简单的构造函数
public class Shape {
public Shape(int i){
if (i==0){
throw new IllegalArgumentException("Cant have 0");
}
}
}
Great. Now I have a class that isn't touching Android at all, and I want to unit test it. What should I do next?
大。现在我有一个完全没有触及Android的课程,我想对它进行单元测试。接下来我该怎么办?
This is where my question stops. Below I'll go through what I tried.
这是我的问题停止的地方。下面我将介绍我尝试过的内容。
Please note that I really have never tested before in Android or Java. Excuse me for "rookie" mistakes.
- While in the Shape.java I go to "Navigate" > "Test"
- 在Shape.java中我转到“导航”>“测试”
- Hit enter to select "Create new Test"
- 按Enter键选择“创建新测试”
- Get this popup, and select JUNIT4.
- 获取此弹出窗口,然后选择JUNIT4。
- I then hit the fix button to fix the library not being found
- 然后我点击修复按钮修复未找到的库
- I get this popup
- 我得到这个弹出窗口
- I'm not really sure what to select, so I select the default/highlighted.
- 我不确定要选择什么,所以我选择默认/突出显示。
I write my test
我写了我的考试
package com.eghdk.getjunit4towork;
import org.junit.Test;
import static org.junit.Assert.*;
public class ShapeTest {
@Test(expected = IllegalArgumentException.class)
public void testShapeWithInvalidArg() {
new Shape(0);
}
}
At this point, I'm not really sure how to run my tests, but try to do this:
此时,我不确定如何运行我的测试,但尝试这样做:
I get these errors when running
我跑步时遇到这些错误
Error:(3, 17) Gradle: error: package org.junit does not exist
Error:(5, 24) Gradle: error: package org.junit does not exist
Error:(8, 6) Gradle: error: cannot find symbol class Test
错误:(3,17)Gradle:错误:包org.junit不存在错误:(5,24)Gradle:错误:包org.junit不存在错误:(8,6)Gradle:错误:找不到符号课堂考试
4 个解决方案