junit5

Migrating from JUnit4 to JUnit5 throws NullPointerException on @Autowired repositories

北城余情 提交于 2019-12-08 01:12:25
问题 I have a very simple repository test, it runs just fine when I'm using JUnit's 4 "@RunWith(SpringRunner.Class)". When I tried to use "@ExtendWith" like in the provided example I get a NullPointerException when trying to work with the repository. It seems like "@Autowire" doesn't inject the repository when using the latter annotation. Here's the pom.xml file and stack trace: https://pastebin.com/4KSsgLfb Entity Class: package org.tim.entities; import lombok.AccessLevel; import lombok.Data;

java.lang.NoSuchMethodError when run test with Junit 5

情到浓时终转凉″ 提交于 2019-12-07 04:35:59
问题 I created a simple test to try Junit 5: import org.junit.jupiter.api.Test; public class MyTest { @Test public void testJupiter() { System.out.println("test"); } } This is the dependency I use: <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.0.0</version> <scope>test</scope> </dependency> Stack trace is the next: Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader(

JUnit 5, Java 9 and Gradle: How to pass --add-modules?

戏子无情 提交于 2019-12-07 04:02:48
问题 I want to migrate from Java 8 to Java 9. When running my tests I get a CNFE regarding javax.xml.bind.JAXBContext. Therefore, "--add-modules java.xml.bind" seems to be required. I tried to extend my GRADLE_OPTS env variable, but the error remains. Any hint is appreciated. 回答1: You can follow the five basic steps while migrating as stated in the gradle-building java9 modules which are:- When converting a java-library project to produce a Java 9 module, there are five changes you should to make

How to choose which JUnit5 Tags to execute with Maven

[亡魂溺海] 提交于 2019-12-07 02:33:52
问题 I have just upgraded my solution to use JUnit5. Now trying to create tags for my tests that have two tags: @Fast and @Slow . To start off I have used the below maven entry to configure which test to run with my default build. This means that when I execute mvn test only my fast tests will execute. I assume I can override this using the command line. But I can not figure out what I would enter to run my slow tests.... I assumed something like.... mvn test -Dmaven.IncludeTags=fast,slow which

What is the equivalent of TestName rule in JUnit 5?

会有一股神秘感。 提交于 2019-12-06 18:31:12
问题 How can I get name of the test method in JUnit 5? 回答1: Declare a parameter of type TestInfo in your test method and JUnit will automatically supply an instance of that for the method: @Test void getTestInfo(TestInfo testInfo) { // Automatically injected System.out.println(testInfo.getDisplayName()); System.out.println(testInfo.getTestMethod()); System.out.println(testInfo.getTestClass()); System.out.println(testInfo.getTags()); } You can get test method name (and more) from the TestInfo

What use is @TestInstance annotation in JUnit 5?

北城以北 提交于 2019-12-06 17:41:43
问题 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 . 回答1: 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

Migrating from JUnit4 to JUnit5 throws NullPointerException on @Autowired repositories

那年仲夏 提交于 2019-12-06 14:33:03
I have a very simple repository test, it runs just fine when I'm using JUnit's 4 "@RunWith(SpringRunner.Class)". When I tried to use "@ExtendWith" like in the provided example I get a NullPointerException when trying to work with the repository. It seems like "@Autowire" doesn't inject the repository when using the latter annotation. Here's the pom.xml file and stack trace: https://pastebin.com/4KSsgLfb Entity Class: package org.tim.entities; import lombok.AccessLevel; import lombok.Data; import lombok.NonNull; import lombok.Setter; import javax.persistence.Entity; import javax.persistence

How to test extension implementations

拜拜、爱过 提交于 2019-12-06 11:15:31
There are several extension points available in the JUnit 5 API. For example: AfterEachCallback AfterAllCallback AfterTestExecutionCallback BeforeAllCallback BeforeEachCallback BeforeTestExecutionCallback ExecutionCondition ParameterResolver TestExecutionExceptionHandler TestInstancePostProcessor TestTemplateInvocationContextProvider Nearly all of these extensions take some form of ExtensionContext which provides access to the test class , test method , test instance , ability to publish report entries , store values , and other capabilities. A lot of these operate directly on class instances

Does JUnit 5 support test method execution in alphabetical order or any similar functionality? [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-06 10:56:22
This question already has answers here : How to change tests execution order in JUnit5? (2 answers) Closed 7 months ago . JUnit 4 has @FixMethodOrder(MethodSorters.NAME_ASCENDING) to support test execution in alphabetical order. Is there any similar functionality introduced in latest JUnit 5 or any other way to achieve this? I went through some of the similar issue but could not find any solution. So posting this question again to check for a solution. Thanks JUnit issue is still open https://github.com/junit-team/junit5/issues/13 So, right now there is no such possibility. Finally, this is

JUnit5: Test multiple classes without repeating code

╄→尐↘猪︶ㄣ 提交于 2019-12-06 10:10:41
I have built my own implementation of a stack in Java, which looks something like this: There is the interface "Stack" which provides the basic functions (pop, push, peek etc.). And then I have 2 concrete classes, one with the help of arrays and the one with a linked list (how is not important in this case). Now my question: I want to test this with JUnit5 and because you can't instantiate an interface, I have to test each function once for the class with the arrays and once for the class with the linked list, so the code is unnecessarily long. Is there a way that I can test all functions for