junit5

How do I run JUnit 5 integration tests with the Maven Failsafe plugin?

走远了吗. 提交于 2019-12-04 23:48:52
The Maven Failsafe plugin won't find my JUnit 5 integration tests when I'm running the command mvn clean failsafe:integration-test , although it can find the files. I have the junit-jupiter-api and junit-jupiter-engine as test dependencies: <properties> <junit.jupiter.version>5.0.1</junit.jupiter.version> </properties> <dependencies> <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-engine<

What use is @TestInstance annotation in JUnit 5?

不羁岁月 提交于 2019-12-04 23:11:54
Can you give a simple explanation of @TestInstance annotation and how it can be useful in JUnit 5? I think we can achieve the same effect probably by making our fields static . I think the docs provide a useful summary: If you would prefer that JUnit Jupiter execute all test methods on the same test instance, simply annotate your test class with @TestInstance(Lifecycle.PER_CLASS). When using this mode, a new test instance will be created once per test class. Thus, if your test methods rely on state stored in instance variables, you may need to reset that state in @BeforeEach or @AfterEach

Best practice for looped JUnit test

耗尽温柔 提交于 2019-12-04 21:41:20
In a school assignment, I should write blackbox test for a method that returns true for a parameter < 80 and false otherwise. Currently my approach would be for (int i = 0; i < 80; i++) { assertTrue(someMethod(i)); } for (int i = 80; i <= 100; i++) { assertFalse(someMethod(i)); } However, this would require 100 seperate assertions. Is there a best/better practice method? If relevant, I'm using JUnit 5 but could switch to JUnit 4 if required (it's just a school assignment after all). Regards. For JUnit 5 consider the repeated test feature: https://junit.org/junit5/docs/current/user-guide/

JUnit5 - How to get test result in AfterTestExecutionCallback

时间秒杀一切 提交于 2019-12-04 07:06:02
I write JUnit5 Extension. But I cannot find way how to obtain test result. Extension looks like this: import org.junit.jupiter.api.extension.AfterTestExecutionCallback; import org.junit.jupiter.api.extension.TestExtensionContext; public class TestResultExtension implements AfterTestExecutionCallback { @Override public void afterTestExecution(TestExtensionContext context) throws Exception { //How to get test result? SUCCESS/FAILED } } Any hints how to obtain test result? Nicolai As other answers point out, JUnit communicates failed tests with exceptions , so an AfterTestExecutionCallback can be

How to parameterize with String arrays in JUnit 5

£可爱£侵袭症+ 提交于 2019-12-04 06:19:23
I would like to write JUnit 5 parametrized test which takes string array ( String[] ) as a parameter: @ParameterizedTest @MethodSource("stringArrayProvider") void parseFirstAndSecondInt(String[] args) { Arguments arguments = new Arguments(args); assertEquals(1, arguments.getFirst()); assertEquals(2, arguments.getSecond()); } I'm not sure, how to provide a collection/stream/iterator of string arrays. I've unsuccessfully tried following approach with @MethodSource annotation static Stream<String[]> stringArrayProvider() { return Stream.of( new String[]{"1", "2"}, new String[]{"1", "2", "3"}); }

Why do JUnit 5 tests not inherit @Test annotation from abstract classes?

烈酒焚心 提交于 2019-12-04 05:20:32
I just realized (while migrating legacy code from JUnit 4 to JUnit 5) that some of our test methods are not executed because they don't have the @Test annotation. They don't have it, because they override methods from an abstract superclass (where the annotation is present). I can easily fix this by adding the @Test to each method. But I was wondering if this is intended behavior. It changed from JUnit 4 to 5 but I can't find anything about it in the official JUnit5 User Guide or anywhere else. According to this question , Annotations are usually not inherited. But it seems that this was

JUnit 5 under Gradle with multiple source sets

怎甘沉沦 提交于 2019-12-04 04:32:47
I've got a project I'm putting together that I want to use JUnit 5 for. I've got this working fine for Unit Tests already. I do however have multiple test source sets - I've got an additional one for Acceptance Tests. I'm struggling to work out how to get JUnit 5 to run the Unit Tests - defined in src/test - in one task and the Acceptance Tests - defined in the "acceptanceTest" sourceSet and located in "src/acceptance" - in another task. I have previously got this working with JUnit 4 and Cucumber, but the JUnit 5 plugin doesn't seem to want to work like this. build.gradle: buildscript { ext {

Integrate JUnit 5 tests results with Intellij test report

痞子三分冷 提交于 2019-12-04 04:26:24
问题 My build.gradle is configure like this: apply plugin: 'java' compileTestJava { sourceCompatibility = 1.8 targetCompatibility = 1.8 } repositories { mavenCentral() } dependencies { testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M1") testRuntime("org.junit.vintage:junit-vintage-engine:5.0.0-M1") } and a simple test like this: public class JUnit5Test { @Test void test() { } } When I execute my test, I see this in the console: Test run finished after 76 ms [ 1 tests found ] [ 0 tests

How to automatically migrate from JUnit 4 to JUnit 5?

心不动则不痛 提交于 2019-12-04 04:05:51
问题 In the spirit of this question from JUnit 3 to JUnit 4, are there any list of regular expressions to efficiently migrate from the junit 4 API to the junit 5 API , regardless of the code size? 回答1: The tooling at the moment is not great, but improving: IntelliJ: Migrates most annotations to JUnit 5 versions. Hovewer, does not do anything if your test file contains @Rule s (e.g., ExpectedException) as of v2018.2. Error Prone: contains built-in refactorings to automatically migrate various JUnit

Maven not running JUnit 5 tests

。_饼干妹妹 提交于 2019-12-04 03:17:08
问题 Im trying to get a simple junit test running with maven but it is not detecting any tests. Where am I going wrong? The project directory Project -> src -> test-> java -> MyTest.java Results : Tests run: 0, Failures: 0, Errors: 0, Skipped: 0 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/xsd/maven-4.0.0.xsd">