我需要为面试解决一个java问题,他们给我发了测试课.它始于
import org.junit.Before;
Run Code Online (Sandbox Code Playgroud)
并且在地方也有以下语法:
@RunWith(JUnit4.class)
...
@Before
...
@Test
Run Code Online (Sandbox Code Playgroud)
我有一段时间没有使用过Java,所以这让我有些困惑.我下载了eclipse,当我尝试编译这个测试文件时,导入和@符号出现错误.导入错误抛出:
The import org.junit cannot be resolved.
Run Code Online (Sandbox Code Playgroud)
@RunWith甚至无法识别,因为它试图将其解析为一种类型.
我目前正在与Jenkins建立一个持续集成工具.我希望每次构建时都运行JUnit测试.我的问题是,没有一个将要测试的项目使用maven或ant.所以我想知道是否可以在没有maven或ant的情况下运行这些测试,如果是,我该怎么做?
提前感谢您的回答
我一直在查看StackOverflow上的一些帖子,看看如何使用MYSQL数据库设置测试.
我只是想测试一些控制器方法,这些方法需要一个带有一些数据的测试数据库才能返回有意义的结果.我想使用一个真正的MYSQL数据库,因为我将在生产中使用它,我已经读过MYSQL和Play提供的InMemory数据库之间存在很多差异.
在这个和这个博客关于测试Play的帖子中!应用程序,帖子显示了使用数据库参数初始化FakeApplication对象然后调用Helper.start(fakeApp)的示例.
所述文档为Helper.start(FakeApplication应用程序)提供了以下描述:
启动一个新的应用程序.
太好了.但实际上是通过调用start来触发哪些进程以及在测试中会给我带来什么?
Map<String, String> settings = new HashMap<String, String>();
settings.put("db.default.url", "jdbc:mysql://localhost/testdb");
settings.put("db.default.user", "root");
settings.put("db.default.password", "");
app = Helpers.fakeApplication(settings);
Helpers.start(app);
Run Code Online (Sandbox Code Playgroud)
我希望上面的代码将Ebean配置为使用我的测试数据库,但是当我尝试执行诸如Ebean.save()之类的方法时,我得到一个错误,表示没有向Ebean注册默认数据库.为了注册我需要填充SERVERCONFIG对象,并创建一个EbeanServer从EbeanServerFactory.在那种情况下,将设置映射传递给FakeApplication对象有什么意义吗?再一次,启动FakeApplication实际上做了什么?它怎么用?
提前致谢.
可能重复:
如何从命令行运行Junit测试用例?
我以前使用maven运行我的JUnit测试.现在我将所有源代码打包到JAR文件中,并希望使用java命令运行它.我怎样才能做到这一点?请注意,我的代码中没有主类.
如何在没有项目源代码的情况下运行 junit 测试并从它们那里获得一些不错的(例如类似 gradle 的 html)报告(例如测试 + 打包在 uber jar 中的依赖项)?
我可以使用 maven 程序集插件将测试和 maven 打包到可运行的 jar 中,在How to run JUnit test cases from the command line 的帮助下运行这个 jar但是如何创建一些不错的测试结果报告呢?
我想从命令行运行一个包含JUnit 5测试的类.不幸的是,我有一些外部依赖项阻止我使用Maven,Gradle或其他构建系统.
在JUnit 4中,我可以完成这个
java .:"lib/*" org.junit.runner.JUnitCore TestClass
Run Code Online (Sandbox Code Playgroud)
JUnit 5有相同的功能吗?我只想知道测试是否成功与IntelliJ中的运行时间相似.
TestClass.java
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Collections;
import static org.junit.jupiter.api.Assertions.*;
public class TestClass {
private static ArrayList<Student> students;
private static ArrayList<Student> inAgeOrderStudents;
private static ArrayList<Student> inNameOrderStudents;
@BeforeAll
static void setUp(){
initializeStudents();
initSortedAgeStudents();
initSortedNameStudents();
}
@BeforeEach
void reloadStudents() {
Collections.shuffle(students);
}
static void initializeStudents(){
students = new ArrayList<Student>();
students.add(new Student(18, "Tim"));
students.add(new Student(18, "Tim"));
students.add(new Student(16, "Jean"));
students.add(new Student(14, "Lin"));
students.add(new Student(19, "Sam"));
}
static …Run Code Online (Sandbox Code Playgroud) 我希望能够从命令行运行Junit测试,但是当我运行此命令时
java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name]
Run Code Online (Sandbox Code Playgroud)
我得到的只是
OK (0 tests)
Run Code Online (Sandbox Code Playgroud)
它是否与Android项目有关?我之前运行过这个命令并没有太多问题.
如果您不介意有额外的抵押品来记住/设置CLASSPATH:如何从命令行运行 JUnit 测试用例,则此答案有效。但是,如果项目是由 Netbeans 创建的,则类路径已经在您的项目中,并且该项目“知道”如何运行单个单元测试和单个单元测试方法。
$ ant -p
. . .
test-single Run single unit test.
test-single-method Run single unit test.
. . .
Run Code Online (Sandbox Code Playgroud)
但是,如果您尝试构建这些目标,您将看到此错误消息(以及其他错误消息),而没有关于如何设置相关属性的指导:
BUILD FAILED
/home/me/myproject/nbproject/build-impl.xml:1300: Must select some files in the IDE or set javac.includes
Run Code Online (Sandbox Code Playgroud)
属性设置不正确,ant 只会告诉您构建成功,但不会编译或运行您的测试。这比它应该的更难。
那么,我们如何设置这些目标所需的属性呢?
我有一个文件'Junit.class',我从命令行运行它像普通(java Junit).在eclipse中我运行它并且导入的语句有效,从我拥有的.jar文件中获取它们.当我从命令行运行它时它不会运行.Eclipse制作了Junit.class文件
:~java Junit
Run Code Online (Sandbox Code Playgroud)
错误来自它无法找到导入的类.我是否需要包含jar文件,如果是这样,例如,有没有办法?
:~ java (jar_File_1) (Jar_file_2) Junit.class
Run Code Online (Sandbox Code Playgroud)
基本上我只需要在我的Junit类中导入类的jar文件.
我想在终端中使用junit.我有一个班Abc.java
class Abc{
public int add(int a, int b){
return a+b
}
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个类AbcTest.java
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
class AbcTest {
public void testAdd() {
System.out.println("add");
int a = 0;
int b = 0;
Abc instance = new Abc();
int expResult = 0;
int result = instance.add(a, b);
assertEquals(expResult, result);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行命令
javac -cp /usr/share/java/junit4.jar AbcTest.java
Run Code Online (Sandbox Code Playgroud)
我收到以下错误输出
AbcTest.java:16: error: cannot find symbol
Abc instance = new Abc();
^
symbol: class …Run Code Online (Sandbox Code Playgroud)