目前我有以下build.gradle文件:
apply plugin: 'java'
sourceSets {
main {
java {
srcDir 'src/model'
}
}
}
dependencies {
compile files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')
runtime fileTree(dir: 'libs', include: '*.jar')
}
Run Code Online (Sandbox Code Playgroud)
这的build.gradle文件是我的仓库在这里.我的所有主文件都在src/model /中,它们各自的测试都在test/model中.
如何正确添加JUnit 4 依赖项,然后在测试/模型的文件夹中运行这些测试?
有人可以建议一种简单的方法来获取对junit测试类中的String/InputStream/File/etc类型对象的文件引用吗?显然,我可以将文件(在这种情况下为xml)粘贴为一个巨大的字符串,或者将其作为文件读取,但是有一个特定于Junit的快捷方式吗?
public class MyTestClass{
@Resource(path="something.xml")
File myTestFile;
@Test
public void toSomeTest(){
...
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Maven构建一个简单的Java项目.在我的pom文件中,我声明JUnit 4.8.2是唯一的依赖项.仍然Maven坚持使用JUnit版本3.8.1.我如何解决它?
问题表现在编译失败:"package org.junit不存在".这是因为我的源代码中的import语句.JUnit 4.*中的正确包名称是org.junit.*,而在版本3中.*它是junit.framework.*
我想我已经在http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html找到了关于问题根源的文档,但是那里的建议似乎是Maven专家的意思.我不明白该怎么做.
我们有一堆JUnit测试用例(集成测试),它们在逻辑上分为不同的测试类.
我们能够为每个测试类加载一次Spring应用程序上下文,并将其重用于JUnit测试类中的所有测试用例,如http://static.springsource.org/spring/docs/current/spring-framework-reference中所述./html/testing.html
但是,我们只是想知道是否有一种方法只为一堆JUnit测试类加载一次Spring应用程序上下文.
FWIW,我们使用Spring 3.0.5,JUnit 4.5并使用Maven来构建项目.
JUnit 4.8包含一个名为"Categories"的新功能,允许您将某些类型的测试组合在一起.这非常有用,例如,为慢速和快速测试分别进行测试.我知道JUnit 4.8发行说明中提到的内容,但是想知道我如何实际运行所有使用某些类别注释的测试.
JUnit 4.8发行说明显示了一个示例套件定义,其中SuiteClasses注释选择要运行的特定类别的测试,如下所示:
@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
// Will run A.b and B.c, but not A.a
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何在SlowTests类别中运行所有测试?您似乎必须拥有SuiteClasses注释...
我正在尝试使用maven创建javadoc并且它失败了.执行验证时也会失败.
mvn verify
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
(...)
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/miquel/creaveu/createOmegaMatrix/src/main/java/edu/url/salle/gtm/hnm/dataStructures/HFrame.java:[6,23]
package org.junit does not exist
[ERROR] /home/miquel/creaveu/createOmegaMatrix/src/main/java/edu/url/salle/gtm/hnm/dataStructures/HFrame.java:[6,0]
static import only from classes and interfaces
(···)
Run Code Online (Sandbox Code Playgroud)
在我的pom.xml文件中,我有以下几行:
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我的本地存储库包含junit jar文件:
miquel@ubuntu:~/creaveu/createOmegaMatrix$ ls -l /home/miquel/.m2/repository/org/junit/junit/4.8.2/
total 248
**-rw-r--r-- 1 miquel miquel 237344 2012-09-13 11:01 junit-4.8.2.jar**
-rw-r--r-- 1 miquel miquel 236 2012-09-13 11:13 junit-4.8.2-javadoc.jar.lastUpdated
-rw-r--r-- 1 miquel miquel 0 2012-09-13 11:13 junit-4.8.2-javadoc.jar-not-available
-rw-r--r-- 1 miquel miquel 458 2012-09-12 18:35 junit-4.8.2.pom
-rw-r--r-- 1 miquel …
Run Code Online (Sandbox Code Playgroud) 突然间,这在JUnit测试期间不断发生.一切正常,我写了一些新的测试,发生了这个错误.如果我还原它,它就不会消失.这是为什么?
log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Run Code Online (Sandbox Code Playgroud) 我如何模拟正在内联初始化的字段变量?
例如
class Test {
private Person person = new Person();
...
public void testMethod() {
person.someMethod();
...
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我想在测试方法时测试person.someMethod() - Test#testMethod.
我需要模拟person变量的初始化.任何线索?
编辑:我不允许修改Person类.
我在导入一些Android UI测试框架时遇到了麻烦 - 我无法弄清楚出了什么问题!
这是我的班级:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class ExampleUnitTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);
@Test
public void listGoesOverTheFold() {
onView(withText("Hello world!")).check(matches(isDisplayed()));
}
}
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,我得到错误'找不到符号ActivityTestRule'和'找不到符号AndroidJUnit4'.我试图导入它们但是找不到它们.
build.gradle中的依赖项设置为:
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
androidTestCompile 'com.android.support:support-annotations:23.4.0'
androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
Run Code Online (Sandbox Code Playgroud)
所以我认为我已经设置了所有依赖项 - 我一直在尝试很多东西,但没有运气.
有人有主意吗?
我相信我们都知道setUp(@Before)将在任何测试方法之前执行,而tearDown(@After)将在测试方法之后执行.
我们也知道Junit将为每个测试方法创建一个Test实例.
我的问题是,我们可以将setUp方法内容移动到类Constructor并删除setUp方法吗?是否有任何特定的理由保持setUp方法?
junit4 ×10
java ×7
junit ×5
junit3 ×2
maven-3 ×2
spring ×2
unit-testing ×2
android ×1
build.gradle ×1
gradle ×1
log4j ×1
maven ×1
mockito ×1
powermockito ×1
spring-test ×1