Tag: junit5

DataJPATest Junit方法命名和NullPointerExceptions

我正在使用SpringBoot / Kotlin / JPA / Hibernate / Junit,并且拥有JpaServiceTest类,可以执行与单个实体有关的存储库方法。 JpaService类的方法名遵循约定findByXXXXId , findAll , addXXXX , addXXXX和deleteXXXX 。 为了保持一致性,我使用相同的约定为JpaTest类中的方法命名。 我的JpaTest类有两个findById场景,其中’Null’预期是另一个返回映射实体的地方。 我的应用程序按预期方式工作,但是我的测试类在findById方案上失败,这个方案有望返回一个有效的实体。 The service class @Service(“MyService”) @Transactional internal class JpaMyService(val MyRepo: MyRepository) : MyService { val log = LoggerFactory.getLogger(“MyService”) override fun findByMyId(MyId: Long): MyDto? { log.debug(“Retrieving My: {}”, MyId) return MyRepo.findOne(MyId)?.toDto() } override fun findAllMys(): List { […]

未解决的参考:使用@ExtendWith注释的SpringExtension

我正在使用SpringBoot中的Junit 5来利用它提供的一些新function。 基本的设置工作,我可以使用新的注释,如@BeforeAll和@AfterAll但是,我相信,因为我不能解决@ExtendWith(SpringExtension::class)我不能初始化我的测试类,以使用注入资源。 我已经尝试将所需的依赖项添加到我的build.gradle文件,但我假设我缺少一个键依赖项: buildscript { ext.dokka_version = ‘0.9.15’ ext.dockerVersion = ‘1.2’ ext.junit4Version = ‘4.12’ ext.junitVintageVersion = ‘4.12.2’ ext.junitPlatformVersion = ‘1.0.2’ ext.junitJupiterVersion = ‘5.0.2’ ext.kotlin_version = ‘1.1.60’ ext.log4jVersion = ‘2.9.0’ ext.springVersion = ‘1.5.8.RELEASE’ repositories { mavenCentral() jcenter() } dependencies { // Needed for the ‘kotlin’ plugin classpath(“org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}”) // Needed for the ‘org.springframework.boot’ plugin classpath(“org.springframework.boot:spring-boot-gradle-plugin:${springVersion}”) // […]

如何更改TestEngine ID以运行JUnit 5?

我有我的Kotlin Android项目的以下依赖项{ dependencies { … testImplementation “org.junit.platform:junit-platform-engine:1.0.2” testImplementation “org.junit.platform:junit-platform-gradle-plugin:1.0.2” testImplementation “org.junit.platform:junit-platform-launcher:1.0.2” testImplementation “org.junit.platform:junit-platform-suite-api:1.0.2” testImplementation “org.junit.jupiter:junit-jupiter-api:5.0.2” testImplementation “org.junit.jupiter:junit-jupiter-engine:5.0.2” testImplementation “org.junit.jupiter:junit-jupiter-params:5.0.2” } 运行我的测试时得到的错误是: org.junit.platform.launcher.core.DefaultLauncher handleThrowable WARNING: TestEngine with ID ‘junit-vintage’ failed to discover tests java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter … Caused by: java.lang.ClassNotFoundException: org.junit.runner.manipulation.Filter Exception in thread “main” java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest; 如果我的测试引擎id是junit-vintage ,那么是否可以改变它? 它似乎仍然使用junit 4引擎。 我怎样才能改变这个,所以它使用junit-jupiter? 我从我的依赖中删除了junit-4.12,所以我想知道为什么它使用junit-vintage引擎而不是junit-jupiter。 目前使用: Android Studio 3.0 […]

测试环境配置:Android + JUnit 5 + Mockito + Spek + Kotlin

我很难配置基于JUnit Jupiter(5)的测试环境。 我有两个不同的错误: WARNING: TestEngine with ID ‘spek’ failed to discover tests org.junit.platform.commons.util.PreconditionViolationException: Could not load class with name… Exception in thread “main” java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:61)… 配置如下。 主build.gradle : apply plugin: ‘org.junit.platform.gradle.plugin’ buildscript { ext.kotlin_version = ‘1.1.4-3’ repositories { google() jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:3.0.0-beta5’ classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” classpath “org.junit.platform:junit-platform-gradle-plugin:1.0.0” classpath “de.mannodermaus.gradle.plugins:android-junit5:1.0.0” } } […]

注入Platform Launcher创建的测试类

我以编程方式运行JUnit 5测试(Kotlin): LauncherFactory.create().execute( LauncherDiscoveryRequestBuilder .request() .selectors( DiscoverySelectors.selectClass(Sometest::class.java) ) .build(), SummaryGeneratingListener() ) 有没有办法挂钩到启动器的生命周期,以便在测试运行之前用测试类注入测试类? 我试图使用@ExtendWith ,但是然后我的TestInstancePostProcessor仍然由JUnit创建,而我需要一个链接到我原来的范围,从我执行Launcher 。

三个测试容器从哪里来?

我把一个演示JUnit5项目放在一起来试用这个框架。 该项目包括Gradle(4.4),Java(8)和Kotlin(1.2.0)四个测试用例。 我有下面的Gradle构建脚本(我已经删除了大部分的样板,只保留重要的东西): apply plugin: ‘java’ apply plugin: ‘kotlin’ apply plugin: ‘org.junit.platform.gradle.plugin’ repositories { mavenCentral() } buildscript { ext.kotlin_version = ‘1.2.0’ repositories { mavenCentral() } dependencies { classpath ‘org.junit.platform:junit-platform-gradle-plugin:1.0.2’ classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” } } configurations { all { exclude group: ‘junit’, module: ‘junit’ } } project.ext { junitPlatformVersion = ‘1.0.2’ junitJupiterVersion = ‘5.0.2’ } dependencies { […]

JUnit5在课堂上进行参数化测试

是否有可能使用JUnit5的参数化新功能来运行测试类来接收测试参数,而不是在方法级别执行? 使用JUnit 4,像@RunWith(Parameterized::class)加继承这样的runner可以用来传递一个参数数组到子类,但是我不确定是否有可能实现一些相同的东西,但是使用新的JUnit 5 api。

Kotlin中@BeforeAll的正确解决方法是什么?

目前JUNIT5 API只允许@BeforeAll在静态的方法上 所以如果这样做,这将不会编译: @BeforeAll fun setup() { MockitoAnnotations.initMocks(this) mvc = MockMvcBuilders.standaloneSetup(controller).build() } 所以为了在Kotlin中有静态的方法,我必须把这样的伴侣对象放进去: companion object { @JvmStatic @BeforeAll fun setup() { MockitoAnnotations.initMocks(this) mvc = MockMvcBuilders.standaloneSetup(smsController).build() } } 这将编译,但我没有访问父类的变量。 那么用Kotlin调用JUnit5的@BeforeAll会是怎样的习惯用法呢?

具有多个源集的Gradle下的JUnit 5

我有一个项目,我正在把我想要使用JUnit 5。 我已经在单元测试中正常工作了。 然而,我确实有多个测试源集 – 我还有一个用于验收测试。 我正在努力研究如何让JUnit 5在一个任务中运行src / test中定义的单元测试,并在另一个任务中运行“acceptanceTest”sourceSet中定义的“验收测试”,并位于“src / acceptance”中任务。 我以前有过使用JUnit 4和Cucumber的工作,但JUnit 5插件似乎并不想这样工作。 的build.gradle: buildscript { ext { jackson_version = "2.9.0.pr4" // IntelliJ needs M4 junitJupiter_version = "5.0.0-M4" junitPlatform_version = "1.0.0-M4" kotlin_version = "1.1.3-2" slf4j_version = "1.7.25" spring_version = "4.3.10.RELEASE" springBoot_version = "1.5.4.RELEASE" springBootAdmin_version = "1.5.2" runAcceptance = System.properties['noAcceptance'] == null } repositories […]

我如何得到一个黄瓜功能的结果

我正在尝试在JUnit 5 Jupiter中运行Cucumber功能。 我已经从Cucumber-jvm源代码中提取了一些代码,并将其用于JUnit 5的TestFactory 。 它正在工作:当我运行所有JUnit测试(这是Kotlin代码,但同样适用于Java)时,我看到我的功能正在运行: @CucumberOptions( plugin = arrayOf("pretty"), features = arrayOf("classpath:features") ) class Behaviours { @TestFactory fun loadCucumberTests() : Collection<DynamicTest> { val options = RuntimeOptionsFactory(Behaviours::class.java).create() val classLoader = Behaviours::class.java.classLoader val resourceLoader = MultiLoader(classLoader) val classFinder = ResourceLoaderClassFinder(resourceLoader, classLoader) val runtime = Runtime(resourceLoader, classFinder, classLoader, options) val cucumberFeatures = options.cucumberFeatures(resourceLoader) return cucumberFeatures.map<CucumberFeature, DynamicTest> […]