标签: junit4

你如何断言在JUnit 4测试中抛出某个异常?

如何以惯用方式使用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的精神.

java junit assert exception junit4

1915
推荐指数
24
解决办法
120万
查看次数

@ Before,@ BeforeClass,@ BeforeEach和@BeforeAll之间的区别

两者之间的主要区别是什么

  • @Before@BeforeClass
    • 在JUnit 5 @BeforeEach@BeforeAll
  • @After@AfterClass

根据JUnit Api @Before用于以下情况:

编写测试时,通常会发现多个测试需要在运行之前创建类似的对象.

@BeforeClass可用于建立数据库连接.但不能@Before做同样的事情?

java junit annotations junit4 junit5

404
推荐指数
4
解决办法
25万
查看次数

如何在JUnit4中按特定顺序运行测试方法?

我想执行按@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的作者不想要订单功能,为什么?

java junit unit-testing junit4

398
推荐指数
11
解决办法
36万
查看次数

Maven没有找到要运行的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)

java junit junit4 maven-3 maven

387
推荐指数
18
解决办法
22万
查看次数

如何使用JUnit Test注释断言我的异常消息?

我已经用@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)

java testing annotations junit4 assertion

286
推荐指数
8
解决办法
18万
查看次数

2个JUnit Assert类之间的差异

JUnit框架包含2个Assert类(显然在不同的包中),并且每个类的方法看起来非常相似.任何人都可以解释为什么会这样吗?

我所指的课程是:junit.framework.Assertorg.junit.Assert.

java junit unit-testing assert junit4

253
推荐指数
4
解决办法
4万
查看次数

在IntelliJ 10.5中运行测试时获取"NoSuchMethodError:org.hamcrest.Matcher.describeMismatch"

我正在使用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?

java junit intellij-idea hamcrest junit4

222
推荐指数
6
解决办法
10万
查看次数

如何测试没有抛出异常?

我知道一种方法是:

@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?)

java junit unit-testing exception-handling junit4

206
推荐指数
10
解决办法
18万
查看次数

更改参数化测试的名称

在JUnit4中使用参数化测试时,有没有办法设置我自己的自定义测试用例名称?

我想将默认值 - 更改为[Test class].runTest[n]有意义的内容.

java junit parameterized junit4

194
推荐指数
6
解决办法
7万
查看次数

Java:assertEquals(String,String)可靠吗?

我知道==比较两个时有一些问题Strings.这似乎String.equals()是一种更好的方法.好吧,我正在进行JUnit测试,我倾向于使用assertEquals(str1, str2).这是断言两个字符串包含相同内容的可靠方法吗?我会使用assertTrue(str1.equals(str2)),但是你没有得到看到失败的预期和实际值的好处.

在相关的说明中,是否有人有一个页面或线程的链接,明确地解释了问题str1 == str2

java string junit junit4

194
推荐指数
5
解决办法
28万
查看次数