如何以惯用方式使用JUnit4来测试某些代码是否会抛出异常?
虽然我当然可以这样做:
@Test
public void testFooThrowsIndexOutOfBoundsException() {
boolean thrown = false;
try {
foo.doStuff();
} catch (IndexOutOfBoundsException e) {
thrown = true;
}
assertTrue(thrown);
}
Run Code Online (Sandbox Code Playgroud)
我记得有一个注释或一个Assert.xyz或者其他东西,对于这些类型的情况来说,远不如KUndgy 和JUnit的精神.
两者之间的主要区别是什么
@Before
和 @BeforeClass
@BeforeEach
和@BeforeAll
@After
和 @AfterClass
根据JUnit Api @Before
用于以下情况:
编写测试时,通常会发现多个测试需要在运行之前创建类似的对象.
而@BeforeClass
可用于建立数据库连接.但不能@Before
做同样的事情?
我想执行按@Test
特定顺序注释的测试方法.
例如:
public class MyTest {
@Test public void test1(){}
@Test public void test2(){}
}
Run Code Online (Sandbox Code Playgroud)
我希望确保test1()
在test2()
每次运行之前运行MyTest
,但我找不到像这样的注释@Test(order=xx)
.
我认为这对JUnit来说非常重要,如果JUnit的作者不想要订单功能,为什么?
我有一个maven程序,它编译得很好.当我运行mvn test
它时不会运行任何测试(在TESTs标题下说There are no tests to run.
).
我用一个超级简单的设置重新创建了这个问题,我将在下面包含以及运行时的输出-X
.
单元测试从eclipse运行良好(两者都使用默认的junit包,当我改为包含maven下载的junit.jar时).mvn也test-compile
正确地在test-classes下创建了类.我在OSX 10.6.7上使用Maven 3.0.2和java 1.6.0_24运行它.
这是目录结构:
/my_program/pom.xml
/my_program/src/main/java/ClassUnderTest.java
/my_program/src/test/java/ClassUnderTestTests.java
Run Code Online (Sandbox Code Playgroud)
pom.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my_group</groupId>
<artifactId>my_program</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>My Program</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
ClassUnderTest.java:
public class ClassUnderTest {
public int functionUnderTest(int n) {
return n;
}
}
Run Code Online (Sandbox Code Playgroud)
ClassUnderTestTests.java:
import org.junit.Assert;
import org.junit.Before; …
Run Code Online (Sandbox Code Playgroud) 我已经用@Test
注释编写了一些JUnit测试.如果我的测试方法抛出一个已检查的异常,并且我想要将该消息与异常一起断言,那么有没有办法使用JUnit @Test
注释?AFAIK,JUnit 4.7不提供此功能,但未来的版本是否提供此功能?我知道在.NET中你可以断言消息和异常类.寻找Java世界中的类似功能.
这就是我要的:
@Test (expected = RuntimeException.class, message = "Employee ID is null")
public void shouldThrowRuntimeExceptionWhenEmployeeIDisNull() {}
Run Code Online (Sandbox Code Playgroud) JUnit框架包含2个Assert
类(显然在不同的包中),并且每个类的方法看起来非常相似.任何人都可以解释为什么会这样吗?
我所指的课程是:junit.framework.Assert
和org.junit.Assert
.
我正在使用JUnit-dep 4.10和Hamcrest 1.3.RC2.
我创建了一个自定义匹配器,如下所示:
public static class MyMatcher extends TypeSafeMatcher<String> {
@Override
protected boolean matchesSafely(String s) {
/* implementation */
}
@Override
public void describeTo(Description description) {
/* implementation */
}
@Override
protected void describeMismatchSafely(String item, Description mismatchDescription) {
/* implementation */
}
}
Run Code Online (Sandbox Code Playgroud)
使用Ant从命令行运行时,它完全正常.但是当从IntelliJ运行时,它失败了:
java.lang.NoSuchMethodError: org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:18)
at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:8)
at com.netflix.build.MyTest.testmyStuff(MyTest.java:40)
Run Code Online (Sandbox Code Playgroud)
我的猜测是它使用了错误的hamcrest.MatcherAssert.我如何找到它使用的hamcrest.MatcherAssert(即哪个jar文件用于hamcrest.MatcherAssert)?AFAICT,我班级路径中唯一的火腿罐是1.3.RC2.
IntelliJ IDEA使用它自己的JUnit或Hamcrest副本吗?
如何输出IntelliJ正在使用的运行时CLASSPATH?
我知道一种方法是:
@Test
public void foo(){
try{
//execute code that you expect not to throw Exceptions.
}
catch(Exception e){
fail("Should not have thrown any exception");
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更清洁的方法来做到这一点.(可能使用Junit的@Rule
?)
在JUnit4中使用参数化测试时,有没有办法设置我自己的自定义测试用例名称?
我想将默认值 - 更改为[Test class].runTest[n]
有意义的内容.
我知道==
比较两个时有一些问题Strings
.这似乎String.equals()
是一种更好的方法.好吧,我正在进行JUnit测试,我倾向于使用assertEquals(str1, str2)
.这是断言两个字符串包含相同内容的可靠方法吗?我会使用assertTrue(str1.equals(str2))
,但是你没有得到看到失败的预期和实际值的好处.
在相关的说明中,是否有人有一个页面或线程的链接,明确地解释了问题str1 == str2
?