junit5

What's the purpose of the JUnit 5 @Nested annotation

落爺英雄遲暮 提交于 2020-01-02 00:47:08
问题 In JUnit 5, there is a new annotation: @Nested . I understand how it work, I understand why we use nested class, I just don't understand why we need to have nested test class in our test. 回答1: The @Nested annotation allows you to have an inner class that's essentially a test class, allowing you to group several test classes under the same parent (with the same initialization). 回答2: All my tests need a database server running. Most of my tests also need a Users table in the database, to be

Production code + Test module-info = Unpossible?

萝らか妹 提交于 2020-01-01 16:54:53
问题 I have a mock class with a trivial implementation of a service I provide from a module. I'm using OpenJDK 11.03, gradle 5.2.1 and IntelliJ 2019.2. In /main/code/myPackage/myService.java I have: package myPackage; class myService { public abstract void someFunction(); } And in my test/code/somePackage/myMockService I have package myPackage; // no import, they're in the same package. class myMockService extends myService { @Override public void someFunction() { System.out.prinln("Hello World");

How to run particular (out of build cycle) Junit5 tests using Gradle

送分小仙女□ 提交于 2019-12-31 06:53:09
问题 I am moving tests to junit5 that are going to be run with Gradle. In a project I work with there are unit tests and some specific tests that must be run on demand (from particular Gradle tasks I suppose). It is clear about unit tests. Gradle plugin adds a support for that. But I could not find a way to define another test task for my needs I searched in Junit5 plugin source and found out that there are no any particular class for that purpose. The Gradle plugin simply sets up a JavaExec task

Tomakehurst Wiremock. Tries to connect to 8080. Connection refused

痴心易碎 提交于 2019-12-25 01:25:55
问题 @Test void resourceTo_Wired_ClientCall() { wireMockServer = new WireMockServer(options().port(10021)); wireMockServer.start(); final MappingBuilder stubBuilder = get(urlMatching("http://demo1096495.mockable.io/client1")); stubFor(stubBuilder .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", "application/json") .withBody("{\"something\":\"test-wired\"}"))); ; Got error: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1] failed:

@BeforeAll Method as non-static

隐身守侯 提交于 2019-12-24 20:16:14
问题 I was able to implement a non-static setup method with @BeforeAll annotation. It seems to be working correctly as only gets call once. I am bit confuse as the documentation for @BeforeAll says the method has to be static. Please explain. @TestMethodOrder(OrderAnnotation.class) @SpringJUnitWebConfig(locations = { "classpath:service.xml" }) @TestInstance(Lifecycle.PER_CLASS) @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Inherited public class MyTest { @BeforeAll

Maven surefire plugin does not detect Junit5 tests

元气小坏坏 提交于 2019-12-24 09:09:14
问题 I have already looked at Surefire is not picking up Junit 5 tests and a few related questions however things are rapidly evolving with Junit5 that those suggestions don't seem to work anymore. Maven : 3.3.9 I have the following in my pom.xml <dependencies> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> <scope

Is it possible to nest JUnit 5 parameterized tests?

大城市里の小女人 提交于 2019-12-24 08:25:23
问题 I am attempting to write a parameterized test for an interface Foo, which declares a method getFooEventInt(int, int). I have written a paramterized test that works for a single instance of Foo (a FooImpl object). public class FooTest { @ParameterizedTest @MethodSource("getFooEvenIntProvider") public void getFooEvenIntTest(int seed, int expectedResult) { Foo foo = new FooImpl(); Assertions.assertEquals(expectedResult, foo.getFooEvenInt(seed)); } private static Stream getFooEvenIntProvider() {

How to set custom test execution order for a test suite in JUnit5?

孤街醉人 提交于 2019-12-23 13:58:39
问题 I have a large set of tests on JUnit5, which I run in parallel in several threads. There is also information about the time of each test. I want to run at the beginning of the longest tests, and leave the fastest at the end to optimize common execution time. I have not found a way to do this in JUnit5. In version 5.4 there is an org.junit.jupiter.api.MethodOrderer interface which allows you to write a test sorter within a test class. And connect to the test class via the annotation org.junit

IntelliJ JUnit 5 test run fine as Gradle task. When run as individual test sometimes get exception: NoClassDefFoundError …/TestExecutionListener

帅比萌擦擦* 提交于 2019-12-23 09:20:07
问题 Using IntelliJ IDEA version 2018.2.5 (Community Edition) on Windows 10 and learning how to use JUnit 5 parameterized tests with Gradle 4.8 based on JUnit 5 samples from the JUnit team. The test run as expected as gradle task test but sometimes when running individual test get Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/platform/launcher/TestExecutionListener How can this exception be fixed while still using IntelliJ 2018.2.5 (Community Edition), JUnit 5 with parameter

How can I run cleanup method only after tagged tests?

有些话、适合烂在心里 提交于 2019-12-22 18:10:26
问题 I'm writing JUnit 5 tests for my Java project. I have some test methods that require time consuming clean up (after each of them). Ideally, I would like to mark them with some annotation and run cleanup method only for them. This is what I tried: class MyTest { @AfterEach @Tag("needs-cleanup") void cleanup() { //do some complex stuff } @Test void test1() { //do test1 } @Test @Tag("needs-cleanup") void test2() { //do test2 } } I want cleanup method to be run only after test2 . But it actually