{"id":25871,"date":"2017-07-26T07:57:22","date_gmt":"2017-07-26T07:57:22","guid":{"rendered":"https:\/\/blog.jetbrains.com\/idea\/?p=15576"},"modified":"2017-08-01T13:12:47","modified_gmt":"2017-08-01T13:12:47","slug":"support-for-junit-5-in-intellij-idea-2017-2","status":"publish","type":"idea","link":"https:\/\/blog.jetbrains.com\/pt-br\/idea\/2017\/07\/support-for-junit-5-in-intellij-idea-2017-2","title":{"rendered":"Support for JUnit 5 M4 in IntelliJ IDEA 2017.2"},"content":{"rendered":"<p>IntelliJ IDEA provides support for the upcoming <a href=\"http:\/\/junit.org\/junit5\/\" target=\"_blank\" rel=\"noopener\">JUnit 5<\/a> release. JUnit has pretty much been the standard for Java unit testing for years, and the latest version brings the library right up to date, making use of Java 8 features and bringing in ways of testing that are familiar if you&#8217;ve been using other frameworks like Spock.\u00a0 IntelliJ IDEA 2017.2 adds support for some of these features, to make the experience of testing code as painless and quick as possible.<\/p>\n<p>JUnit 5 supports <a href=\"http:\/\/junit.org\/junit5\/docs\/current\/user-guide\/#writing-tests-parameterized-tests\" target=\"_blank\" rel=\"noopener\">parameterized tests<\/a>. These tests let you use the same basic structure of a test with a number of different inputs. This reduces duplication in your test code and also lets you easily add new cases without having to create entire new tests.<\/p>\n<p>IntelliJ IDEA helps you with parameterized tests in a number of different ways,\u00a0 Firstly, it reports &#8220;suspicious&#8221; use of both <code>@Test<\/code> and <code>@ParameterizedTest<\/code> annotations &#8211; you only need one of these in order to declare the method is a test.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15584\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-1-suspicious-annotations.png\" alt=\"Suspicious Annotations\" width=\"1284\" height=\"326\" \/><\/p>\n<p>If you press Alt + Enter on the highlighted <code>@Test<\/code> annotation, IntelliJ IDEA suggests removing this unnecessary annotation.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-2-delete-test-annotation.png\" rel=\"attachment wp-att-15577\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15577\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-2-delete-test-annotation.png\" alt=\"Remove Test Annotation suggestion\" width=\"1284\" height=\"326\" \/><\/a><\/p>\n<p>If you&#8217;re using the <code>@ParameterizedTest<\/code> annotation, you need to supply a source for the parameters, otherwise there are no tests to run.\u00a0 IntelliJ IDEA warns you if you use the <code>@ParameterizedTest<\/code> annotation without an associated source (annotations like <code>@MethodSource<\/code>, <code>@ValueSource<\/code> etc.)<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-3-no-sources.png\" rel=\"attachment wp-att-15578\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15578\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-3-no-sources.png\" alt=\"No sources supplied\" width=\"1284\" height=\"264\" \/><\/a><\/p>\n<p>If you&#8217;re using a source annotation, but it doesn\u2019t match the expectations set by the\u00a0 test method, you&#8217;ll also get a warning about this.\u00a0 For example, if you use a <code>@ValueSource<\/code> that defines a series of ints as the input, but your test method takes a String parameter, IntelliJ IDEA will warn you that these types don&#8217;t match<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-4-ints-to-Strings.png\" rel=\"attachment wp-att-15579\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15579\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-4-ints-to-Strings.png\" alt=\"Incorrect parameter types\" width=\"1284\" height=\"135\" \/><\/a><\/p>\n<p>Using a <code>@MethodSource<\/code>\u00a0lets you choose a method that returns a <code>Stream<\/code> of values to use for parameters, and\u00a0IntelliJ IDEA offers suggestions of suitable methods<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-5-code-completion.png\" rel=\"attachment wp-att-15580\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15580\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-5-code-completion.png\" alt=\"Code completion for MethodSource\" width=\"1284\" height=\"626\" \/><\/a><\/p>\n<p>If you select a method that does not provide the right values for the test method&#8217;s arguments, you&#8217;ll get a warning about this as well. Here, we\u2019ve given <code>@MethodSource<\/code> a method that returns a <code>Stream<\/code> of <code>Strings<\/code>, but because our test takes more than one argument we should have chosen a method that returns a <code>Stream<\/code> of <code>Argument<\/code>s.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-6-incorrect-method-value.png\" rel=\"attachment wp-att-15581\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15581\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-6-incorrect-method-value.png\" alt=\"Incorrect method source type\" width=\"1284\" height=\"452\" \/><\/a><\/p>\n<p>You&#8217;ll notice as well that when you provide a method to <code>@MethodValue<\/code>, this method is not marked as unused because\u00a0IntelliJ IDEA recognises it&#8217;s used in the annotation.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-7-method-value-method-is-used.png\" rel=\"attachment wp-att-15582\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15582\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-7-method-value-method-is-used.png\" alt=\"No unused method\" width=\"1284\" height=\"394\" \/><\/a><\/p>\n<p>In addition to this support for parameterized tests, you also get warnings for using the <code>@Test<\/code> annotation with <code>@RepeatedTest<\/code>.<\/p>\n<p><a href=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-8-repeated-test.png\" rel=\"attachment wp-att-15583\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-15583\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2017\/07\/idea-8-repeated-test.png\" alt=\"Help for repeated tests\" width=\"1284\" height=\"212\" \/><\/a><\/p>\n<p>IntelliJ IDEA 2017.2 makes it simple to use all the features in JUnit 5, so that it&#8217;s easier and quicker to write effective tests.<\/p>\n","protected":false},"author":360,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","categories":[808],"tags":[673,3014,207],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/idea\/25871"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/idea"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/types\/idea"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/users\/360"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/comments?post=25871"}],"version-history":[{"count":0,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/idea\/25871\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/media?parent=25871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/categories?post=25871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/tags?post=25871"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/pt-br\/wp-json\/wp\/v2\/cross-post-tag?post=25871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}