junit5

JUnit5 integration tests with Gradle 4.6

我怕爱的太早我们不能终老 提交于 2019-12-06 09:18:20
Gradle 4.6 added support for JUnit5. This works for me as long as I don't have another sourceset for e.g. integration tests: I do not know how to enable useJUnitPlatform() in my integration tests. What I was able to do is to have test task working with new JUnit5 support, but my testInt task was using JUnit5 console and running tests as it would run from command line. At the end I ditch JUnit5 support in gradle and rollback to using JUnit5 console for both tests. How to enable Gradle 4.6 JUnit5 support on other tasks then test ? If your integration test task is also a Test task, you may

How can I run cleanup method only after tagged tests?

Deadly 提交于 2019-12-06 07:04:44
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 runs after both tests. Is it possible to achieve it via some combination of JUnit 5 annotations? I don

java.lang.AssertionError: Status expected:<200> but was:<404> in Junit test

北城以北 提交于 2019-12-06 06:04:16
I want to create JUnit test for Rest api and generate api doc. I want to test this code: Rest controller @RestController @RequestMapping("/transactions") public class PaymentTransactionsController { @Autowired private PaymentTransactionRepository transactionRepository; @GetMapping("{id}") public ResponseEntity<?> get(@PathVariable String id) { return transactionRepository .findById(Integer.parseInt(id)) .map(mapper::toDTO) .map(ResponseEntity::ok) .orElseGet(() -> notFound().build()); } } Repository interface public interface PaymentTransactionRepository extends CrudRepository

Don't start nested test cases when outer test case fails with JUnit 5

 ̄綄美尐妖づ 提交于 2019-12-06 02:47:10
I have the following test class: import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; import static org.junit.jupiter.api.Assertions.assertTrue; public class HierarchicalTest { @Test void checkOuter() { assertTrue(false); } @Nested @TestInstance(TestInstance.Lifecycle.PER_CLASS) class PrepBTest { @Test void checkInnerA() {} @Test void checkInnerB() {} } } I want to have the behavior that checkInnerA() and checkInnerB() won't be executed when checkOuter() fails. On the other side checkInnerB() should be executed when checkInnerA()

How to parameterize with String arrays in JUnit 5

泄露秘密 提交于 2019-12-05 22:18:47
问题 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

Migrating specific tests from JUnit 4 to JUnit 5 in IntelliJ

与世无争的帅哥 提交于 2019-12-05 21:49:59
问题 I am trying to migrate test files from Junit 4 to Junit 5 using IntelliJ refactor code -> migrate -> junit 4 to junit 5. But it only run import optimization and show all unit tests in the project and ask yes or no on refactoring and nothing happens when I chose do refactor. any one know on another migration tool / plug in or a way to make the migration work ? 回答1: Jeanne Boyarsky created a tool to convert JUnit 4 based tests to JUnit Jupiter. You can find it in her GitHub account: https:/

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

孤者浪人 提交于 2019-12-05 10:25:50
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. Naman 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 to your project. Add a module-info.java describing the module. Modify the compileJava task to produce

Check that JUnit Extension throws specific Exception

家住魔仙堡 提交于 2019-12-05 09:33:13
Suppose I develop an extension which disallows test method names to start with an uppercase character. public class DisallowUppercaseLetterAtBeginning implements BeforeEachCallback { @Override public void beforeEach(ExtensionContext context) { char c = context.getRequiredTestMethod().getName().charAt(0); if (Character.isUpperCase(c)) { throw new RuntimeException("test method names should start with lowercase."); } } } Now I want to test that my extension works as expected. @ExtendWith(DisallowUppercaseLetterAtBeginning.class) class MyTest { @Test void validTest() { } @Test void

Why were JUnit Jupiter and JUnit Vintage separated When I Running TestCase in IntelliJ?

戏子无情 提交于 2019-12-05 00:26:58
I saw that JUnit5 is released, and i applied in IntelliJ V2016.2.1 and run some testcase. so saw below screenshot. i have just two questions. Why were JUnit Jupiter and JUnit Vintage separated When I Running TestCase in IntelliJ? Can i Merge JUnit 4 and 5 TestResults? JUnit 5 is more than just a new API (called JUnit Jupiter ). It also contains an abstraction layer for tools to run JUnit tests (called JUnit Platform ). The platform has no knowledge of either JUnit 4 or Jupiter or anything else really. All it knows are test engines, which will execute the tests they were written for. (You can

What is the equivalent of TestName rule in JUnit 5?

十年热恋 提交于 2019-12-05 00:20:37
How can I get name of the test method in JUnit 5? 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 instance as shown above. Personally I prefer to use this: System.out.println(Thread.currentThread().getStackTrace