标签: junit5

动态测试与JUnit 5中的参数化测试有何不同?

几乎所有我见过的动态测试的例子都可以使用参数化测试进行重写和编写.因此,这是一个实际场景,其中动态测试是唯一的选择,或者至少比参数化测试更合适.

JUnit 5文档中唯一的"真正"动态测试示例并不实用.

有任何想法吗?

java junit junit4 junit5

9
推荐指数
1
解决办法
773
查看次数

为什么assertEquals中的可选断言消息移动到Junit 5中的最后一个位置?

在JUnit 4中,可选的断言消息是assertEquals方法中的第一个参数.在JUnit 5中,它是最后一个.

是否有任何技术原因将其移至最后位置?如果是这样,哪个?

java junit unit-testing junit4 junit5

9
推荐指数
1
解决办法
459
查看次数

如何断言最终满足条件?

我正在尝试编写一个 JUnit5 测试,断言最终在接下来的 15 秒内满足条件。我怎样才能最好地实现这一点?

我在想这个问题:

    assertTimeout(ofSeconds(15), () -> {assertThat(condition, is(true));});
Run Code Online (Sandbox Code Playgroud)

但要反复测试条件

java junit junit5

9
推荐指数
1
解决办法
6102
查看次数

JUnit 5 @ParameterizedTest 使用类作为 @ValueSource

我想做一个ParameterizedTest@ValueSource我已经阅读了很多教程,包括JUnit 5 参数化测试指南

/**
 * The {@link Class} values to use as sources of arguments; must not be empty.
 *
 * @since 5.1
 */
Class<?>[] classes() default {};
Run Code Online (Sandbox Code Playgroud)

所以我尝试了以下方法:

@ParameterizedTest
@ValueSource(classes = {new Mandator("0052", "123456", 79)})
void testCalculateMandateI(Mandator value) {

     //code using Mandator here
}
Run Code Online (Sandbox Code Playgroud)

委托人类别:

 public class Mandator {

    private String creditorPrefix;
    private String retailerCode;
    private int mandateIdSequence;

    public Mandator(final String creditorPrefix, final String retailerCode, final int mandateIdSequence) {
        this.creditorPrefix = creditorPrefix;
        this.retailerCode …
Run Code Online (Sandbox Code Playgroud)

java parameterized junit5

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

未找到Junit 5 gradle插件

尝试使用带有gradle的junit 5:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0'
    }
}

apply plugin: 'java-library'
apply plugin: 'org.junit.platform.gradle.plugin'
...
Run Code Online (Sandbox Code Playgroud)

错误:

Plugin with id 'org.junit.platform.gradle.plugin' not found.
Run Code Online (Sandbox Code Playgroud)

Gradle版本4.0.怎么了?

plugins gradle junit5

8
推荐指数
2
解决办法
5086
查看次数

嵌套类中的junit5 MethodSource

我正在使用junit5,并且想在嵌套类中创建参数化测试。例如:

class CardTest {

    @Nested
    class Cost {
        Stream<Arguments> cards() {
            return Stream.of(
                    Arguments.of(Card.common(0, Color.RED), 0),
                    /** Other Data **/
                    Arguments.of(Card.choseColor(), 50)
            );
        }

        @MethodSource("cards")
        @ParameterizedTest
        void cardCost(Card card, int cost) {
            assertThat(card.cost())
                    .isEqualTo(cost);
        }
    }
    /** Some other nested classes or simple methods **/
}
Run Code Online (Sandbox Code Playgroud)

问题是@MethodSource要求指定的方法必须为static。但是Java不允许在非静态内部类中使用静态方法。如果我创建了Coststatic,则不会由收集junit

我应该怎么做才能解决这个问题?

java junit junit5

8
推荐指数
3
解决办法
1016
查看次数

Gradle 5 JUnit BOM和Spring Boot不正确的版本

我正在使用Gradle 5的BOM(物料清单)功能.这是我为JUnit 5依赖项描述它的方式:

testImplementation(enforcedPlatform("org.junit:junit-bom:5.4.0")) // JUnit 5 BOM
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testImplementation("org.junit.jupiter:junit-jupiter-params")
Run Code Online (Sandbox Code Playgroud)

我的假设是提供BOM将解析依赖项的版本5.4.0.但是,他们得到了解决5.1.1.我不知道为什么.(我还要求enforcedPlatform()锁定指定的版本)

在此输入图像描述

检查JUnit 5的BOM我们发现所有org.junit.jupiter依赖项都以版本列出5.4.0(在项目中解析为5.1.1),所有org.junit.platform依赖项都列在版本中1.4.0,并在项目中正确解析.

我不确定我错过了什么,并希望在这里得到一些帮助.谢谢!

编辑:

我使用Sormuras响应并将所有BOM移动到dependencies {}块的顶部,但仍未获得版本5.4.0.然后我怀疑它可能来自我使用的Gradle Spring Dependency Management插件,所以当我评论它时,我得到了版本JUnit 5.4.0.如何禁用来自Gradle Spring Dependency Management插件的JUnit?

Gradle Spring依赖管理插件

最后:

我决定直接使用Spring Boot Dependencies BOM并删除Gradle插件:

implementation(platform("org.springframework.boot:spring-boot-dependencies:2.0.5.RELEASE"))

我想这个插件是在Gradle 5之前为那些版本的Gradle创建的,你不能使用BOM文件.现在有了BOM支持,我可以直接包含它.这样我的JUnit版本就像我在enforcedPlatform()块中指定的一样.

我接受了下面的Sam Brannen的答案,因为他很好地解释了问题是如何发生的以及解决了什么问题,我认为这对使用旧版本Gradle的人来说很重要.

junit gradle spring-boot junit5

8
推荐指数
2
解决办法
4768
查看次数

在junit5中使用SpringRunner有什么特别的配置吗?

我的微服务项目基于 spring-boot 框架,我所有的单元测试都在 spring runner 上运行。

@RunWith(SpringRunner.class)
Run Code Online (Sandbox Code Playgroud)

添加此注释,导入以下库:

import org.springframework.test.context.junit4.SpringRunner;
Run Code Online (Sandbox Code Playgroud)

如何将我的测试类设置为与 junit5 一起运行?

junit spring-boot junit5 spring-boot-test springrunner

8
推荐指数
3
解决办法
8756
查看次数

如何在 JUnit 5 中参数化 beforeEach()?

我使用 JUnit 5 作为我的测试运行器。

在该设置方法中,我硬编码3个PARAMS( ,platformNameplatformVersiondeviceName)。我有一个测试方法应该测试各种组合......这意味着,在运行我的testLogin()测试时,它应该在多个平台名称、版本、设备名称上运行......

所以,我尝试如下...

@BeforeEach
@CsvSource({"IOS,13.0,iPhone X Simulator", "IOS,13.2,iPhone Simulator", "IOS,13.3,iPhone XS Simulator"})
void setUp(String platformName, String platformVersion, String deviceName) throws MalformedURLException {
    ....
    capabilities.setCapability("platformName", platformName);
    capabilities.setCapability("platformVersion", platformVersion);
    capabilities.setCapability("deviceName", deviceName);
    capabilities.setCapability("methodName", testInfo.getDisplayName());
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,beforeEach()方法如何可以参数化?另外,我想获取测试方法名称...所以,如果我指定参数,那么我应该在哪里指定 TestInfo 参数。

请帮我。我也看到了下面的问题...

JUnit 5 中的 beforeEach/beforeAll 参数化

========

public class TestBase {

    @BeforeEach
    void setUp(TestInfo testInfo) throws MalformedURLException {
        MutableCapabilities capabilities = new MutableCapabilities();
        capabilities.setCapability("platformName", "iOS");
        capabilities.setCapability("platformVersion", "13.2");
        capabilities.setCapability("deviceName", "iPhone Simulator");
        capabilities.setCapability("name", …
Run Code Online (Sandbox Code Playgroud)

parameterized-unit-test selenium-webdriver junit5

8
推荐指数
2
解决办法
2559
查看次数

将 Spring Boot 升级到 2.4.2 时 org.springframework.test.context.TestContextAnnotationUtils 上的 ClassNotFoundException

在我的 POM 中,我将 Spring Boot 的版本从 2.3.5.RELEASE 升级到 2.4.2。结果,mvn clean test现在失败并出现错误

java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils
Caused by: java.lang.ClassNotFoundException: org.springframework.test.context.TestContextAnnotationUtils
Run Code Online (Sandbox Code Playgroud)

在一个简单的测试中

package ch.ge.ael.enu.mediation;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class MediationApplicationTests {

    @Test
    void contextLoads() {
    }

}
Run Code Online (Sandbox Code Playgroud)

有人有线索吗?

spring-test spring-boot junit5

8
推荐指数
2
解决办法
5333
查看次数