标签: junit-jupiter

JUnit 5:抛出如何断言异常?

有没有更好的方法断言方法在JUnit 5中抛出异常?

目前,我必须使用@Rule来验证我的测试是否会引发异常,但这对于我希望多个方法在我的测试中抛出异常的情况不起作用.

java junit junit5 junit-jupiter

162
推荐指数
8
解决办法
13万
查看次数

org.junit.platform.commons.JUnitException:ID为“junit-jupiter”的TestEngine未能发现测试

我想在 Gradle 项目中实施 Junit 5 测试。我试过这个:

梯度配置:

plugins {
    id 'org.springframework.boot' version '2.5.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'test'
version = '0.0.1'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "2020.0.4")
}

dependencies {
    ...............
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)

联合测试:

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import java.util.concurrent.TimeUnit;

public class GeneratePdf {

    @DisplayName("Test MessageService.get()")
    @Test
    @Timeout(value = 60, unit …
Run Code Online (Sandbox Code Playgroud)

java junit spring-test junit5 junit-jupiter

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

NoSuchMethodError:org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass

我有测试导致错误.我试图在IntelliJ Idea中执行它2018.3.2.所有jupiter和junit依赖项都有版本RELEASE

错误全文:

Dec 26, 2018 1:17:17 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to execute tests
java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(Ljava/lang/String;)Lorg/junit/platform/commons/function/Try;
at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.createAbortedExecutionPredicate(OpenTest4JAndJUnit4AwareThrowableCollector.java:40)
at org.junit.jupiter.engine.support.OpenTest4JAndJUnit4AwareThrowableCollector.<clinit>(OpenTest4JAndJUnit4AwareThrowableCollector.java:30)
at org.junit.jupiter.engine.support.JupiterThrowableCollectorFactory.createThrowableCollector(JupiterThrowableCollectorFactory.java:34)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:68)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Run Code Online (Sandbox Code Playgroud)

测试有以下观点

import biz.Services.msg.BookTimeMsgService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(MockitoExtension.class)
public class MsgReceiverTest {

@Mock
BookTimeMsgService bookTimeMsgService; …
Run Code Online (Sandbox Code Playgroud)

java dependencies intellij-idea junit5 junit-jupiter

21
推荐指数
2
解决办法
9141
查看次数

junit-bom 和 junit platform 的用途是什么?我应该将它们包含在 gradle 依赖项中吗?

我正在阅读Junit 5 用户指南。它引导我找到JUnit 5 Jupiter Gradle Sample,这是将 Junit 5 与 Gradle 结合使用的最简单示例。在build.gradle文件中,有 2 个依赖项,junit-jupiter并且junit-bom. 而在testtask中,它也调用了useJUnitPlatform()function。

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform('org.junit:junit-bom:5.7.1'))
    testImplementation('org.junit.jupiter:junit-jupiter')
}

test {
    useJUnitPlatform()
    testLogging {
        events "passed", "skipped", "failed"
    }
}
Run Code Online (Sandbox Code Playgroud)

据我所知,这junit-jupiter是聚合工件,它提取以下 3 个工件,

  1. junit-jupiter-api(编译依赖)
  2. junit-jupiter-engine(运行时依赖)
  3. junit-jupiter-params(用于参数化测试)

所以我想junit-jupiter在我的项目中运行 JUnit Jupiter 已经足够了(如果我错了,请纠正我)。我想知道这里是什么junit-bom以及JUnitPlatform有什么用?我可以简单地摆脱它们吗?感谢大家:)

java junit gradle junit5 junit-jupiter

19
推荐指数
1
解决办法
8394
查看次数

Mockito 为通用 @BeforeEach 设置方法中定义的存根抛出 UnnecessaryStubbingException

我有一个单元测试类,其中我的被测单元依赖于另一个类。使用 Mockito 模拟依赖关系,然后使用 JUnit 的@BeforeEach注释使用在每个单元测试之前运行的通用存根进行设置。请参阅下面的伪代码。

@ExtendWith(MockitoExtension.class)
public class FooFactoryTest {

    @Mock
    private BarDependency barDependency;

    @InjectMocks
    private FooFactory unitUnderTest;

    @BeforeEach
    public void setup() {
        when(barDependency.leftBar(any())).thenReturn(new Bar());
        when(barDependency.rightBar(any())).thenReturn(new Bar());
    }

   ... many tests ...

Run Code Online (Sandbox Code Playgroud)

此设置非常适合我的 10 个单元测试中的 9 个。不幸的是,我的一项测试失败并出现以下错误:

org.mockito.exceptions.misusing.UnnecessaryStubbingException: 
Unnecessary stubbings detected.
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
  1. -> at nl.devillers.mockito.FooFactoryTest.setup(FooFactoryTest.java:69)
Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for …
Run Code Online (Sandbox Code Playgroud)

java junit unit-testing mockito junit-jupiter

15
推荐指数
2
解决办法
1万
查看次数

通过 Maven 执行测试时 @DisplayName 不起作用

通过 Maven 执行时,JUnit5 测试结果未显示指定的 @DisplayName。

在 eclipse 下运行时,相同的测试套件显示正确分配的名称(右键单击 --> 运行方式... --> JUnit 测试)。

这是已实现测试的示例:

@DisplayName(value="prog QA suite")
public class ProgQATest {

    @Test
    @Tag(value="TestProg")
    @DisplayName(value="prog QA present")
    public void testProg2QaPresent(String searchstring)
    {
        /* test code... */
    }
}
Run Code Online (Sandbox Code Playgroud)

当使用 Eclipse 运行时,显示“prog QA suite”和“prog QA present”作为名称。

但是,当使用“maven test”运行时,将“ProgQATest”和“testProg2QaPresent”显示为名称。

使用的 pom.xml 是:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <junit.jupiter.version>5.3.2</junit.jupiter.version>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>com.github.pageobject</groupId>
        <artifactId>pageobject-core</artifactId>
        <version>1.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-params</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency> …
Run Code Online (Sandbox Code Playgroud)

maven junit5 junit-jupiter

13
推荐指数
1
解决办法
3190
查看次数

我的 PITEST 无法运行。覆盖生成minion异常退出。我需要帮助来正确配置我的 pom.xml

运行时mvn org.pitest:pitest-maven:mutationCoverage出现如下错误( Environment: Windows 10, Maven 3.6.1, Java 11, junit-jupiter 5.4.1, pitest 1.4.7

[ERROR] Failed to execute goal org.pitest:pitest-maven:1.4.7:mutationCoverage (default-cli) on project hello-strange-world: Execution default-cli of goal org.pitest:pitest-maven:1.4.7:mutationCoverage failed: Coverage generation minion exited abnormally. Please check the classpath.
[ERROR]
[ERROR] Please copy and paste the information and the complete stacktrace below when reporting an issue
[ERROR] VM : Java HotSpot(TM) 64-Bit Server VM
[ERROR] Vendor : Oracle Corporation
[ERROR] Version : 11.0.2+9-LTS
[ERROR] Uptime : 4936
[ERROR] …
Run Code Online (Sandbox Code Playgroud)

java mutation-testing maven-3 pitest junit-jupiter

13
推荐指数
2
解决办法
1万
查看次数

如何使用 JUnit5 模拟 System.getenv()

我想模拟 System.getenv() 方法。我只找到了 JUnit4 和 PowerMockito 的解决方案。我使用以下依赖项:

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>2.23.0</version>
        <scope>test</scope>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

这是我的测试示例:

@ExtendWith(MockitoExtension.class) 
public class TestEnvVariable {

  @Mock
  System system;

  @Test
  public void shouldExpandPropertyContentToMatchingSysEnv() throws Exception {
      when(system.getenv("KEY")).thenReturn("VALUE");
      assertEquals("VALUE", "KEY");
  }
}
Run Code Online (Sandbox Code Playgroud)

如何使用 JUnit5 模拟 System.getenv()?

java mockito junit5 junit-jupiter

12
推荐指数
1
解决办法
1万
查看次数

线程“main”中的异常 java.lang.NoClassDefFoundError: org/junit/platform/commons/util/ClassNamePatternFilterUtils

我想配置 Maven 以使用这些依赖项运行 Junit 5 测试:

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.7.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.7.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.7.0-M1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>3.3.3</version>
            <scope>test</scope>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

但我得到了例外:

"C:\Program Files\Java\jdk-14\bin\java.exe"
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/commons/util/ClassNamePatternFilterUtils
    at org.junit.platform.launcher.core.LauncherFactory.loadAndFilterTestExecutionListeners(LauncherFactory.java:113)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:99)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:72)
    at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:46)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:31)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.commons.util.ClassNamePatternFilterUtils
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
Run Code Online (Sandbox Code Playgroud)

你知道我该如何解决这个问题吗?

java junit noclassdeffounderror junit5 junit-jupiter

10
推荐指数
3
解决办法
1万
查看次数

测试在JUnit 4下运行,但不在JUnit 5下运行 - 编译干净,但执行0测试

任何人都可以在几分钟内轻松重现此问题.

基本的Maven quickstart项目

使用IntelliJ 2018.3和Maven 3.6.0,我使用Maven原型maven-archetype-quickstart1.4版创建了一个全新的项目.

在此输入图像描述

Java 11

在新项目的POM文件,更改属性maven.compiler.sourcemaven.compiler.target从1.7至11,为Java 11.0.2我目前使用的,祖鲁来自Azul系统.

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>11</maven.compiler.source>
  <maven.compiler.target>11</maven.compiler.target>
</properties>
Run Code Online (Sandbox Code Playgroud)

在的IntelliJ的Maven的面板,我运行cleaninstall生命周期事件.

在此输入图像描述

测试在JUnit 4中运行

作为一部分install,测试运行.这个quickstart原型带有一个断言的单一测试true.

在此输入图像描述

结果显示在RunIntelliJ面板中.

[INFO]正在运行work.basil.example.AppTest

[INFO]测试运行:1,失败:0,错误:0,跳过:0,经过的时间:0.026秒 - 在work.basil.example.AppTest中

所以我知道测试已执行.

JUnit 5,而不是4

这一切都很好.现在让我们升级到JUnit 5,看看问题.

在POM中,我从这里更改了JUnit依赖项:

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

......对此:

<dependencies>
  <!--JUnit 5-->
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.3.2</version>
    <scope>test</scope>
  </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

木星进口(没有老式测试)

编译器抱怨我的AppTest.java文件.所以我改变import那里的语句来使用jupiter包.我只想在我的新greedfield项目中运行JUnit …

java junit intellij-idea junit5 junit-jupiter

8
推荐指数
1
解决办法
931
查看次数