{"id":83805,"date":"2020-09-28T07:10:31","date_gmt":"2020-09-28T07:10:31","guid":{"rendered":"https:\/\/www.baeldung.com\/?p=87768"},"modified":"2020-09-28T07:10:31","modified_gmt":"2020-09-28T07:10:31","slug":"conditionally-run-or-ignore-tests-in-junit-4","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2020\/09\/28\/conditionally-run-or-ignore-tests-in-junit-4\/","title":{"rendered":"Conditionally Run or Ignore Tests in JUnit 4"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<h2 data-id=\"overview-1\">1. Overview<\/h2>\n<div class=\"bd-anchor\" id=\"overview-1\"><\/div>\n<p>Let&#8217;s imagine we have a test for some code that depends on the Operating System and should run only if our test machine is running on Linux. If it&#8217;s running on any other OS, we want the test not to fail, but to be ignored at runtime.<\/p>\n<p>A first approach could be using a couple of <em>if<\/em> statements to check for this condition using <em>System<\/em> class properties. This works, of course, but JUnit has a cleaner, more elegant method.<\/p>\n<p>In this short tutorial, we&#8217;re going to look at <strong>how we can conditionally run or ignore tests in <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/junit\">JUnit<\/a> 4 using the <em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/junit.org\/junit4\/javadoc\/4.12\/org\/junit\/Assume.html\">Assume<\/a>\u00a0<\/em>class<\/strong>.<\/p>\n<h2 data-id=\"the-assume-class\">2. The <em>Assume<\/em> Class<\/h2>\n<div class=\"bd-anchor\" id=\"the-assume-class\"><\/div>\n<p><strong>This\u00a0class provides a set of methods to support conditional test execution based on certain conditions<\/strong>. Our test will only run if all these conditions are met. If not, <strong>JUnit will just skip its execution and mark it as passed in the test report<\/strong>. The latter is the main difference with the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/junit-assertions#assertions-junit4\"><em>Assert<\/em><\/a> class, in which a failing condition leads the test to end as <em>failing<\/em>.<\/p>\n<p>An important thing to note is that <strong>the behavior we described for the <em>Assume<\/em> class is exclusive to the default JUnit runner<\/strong>. With <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/junit-4-custom-runners\">custom runners<\/a>, things may be different.<\/p>\n<p>Finally, in the same way as with <em>Assert<\/em>, we can call the <em>Assume<\/em> methods either in the <em>@Before<\/em> or\u00a0<em>@BeforeClass<\/em> annotated methods or within the <em>@Test\u00a0<\/em>method itself.<\/p>\n<p>Let&#8217;s now go through the most useful methods of the <em>Assume<\/em> class by showing some examples. For all the following examples, let&#8217;s assume <em>getOsName()<\/em> returns <em>Linux.<\/em><\/p>\n<h3 data-id=\"1-using-assumethat\">2.1. Using <em>assumeThat<\/em><\/h3>\n<div class=\"bd-anchor\" id=\"1-using-assumethat\"><\/div>\n<p>The\u00a0<em>assumeThat() <\/em>method checks that the state \u2013 in this case, <em>getOsName()<\/em> \u2013 satisfies the conditions of the matcher passed in:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void whenAssumeThatAndOSIsLinux_thenRunTest() {\r\n    assumeThat(getOsName(), is(\"Linux\"));\r\n    assertEquals(\"run\", \"RUN\".toLowerCase());\r\n}<\/code><\/pre>\n<p>In this example, <strong>we checked whether <em>getOsName()<\/em> equals to <em>Linux<\/em>. As <em>getOsName()<\/em> returns <em>Linux<\/em>, the test will be run<\/strong>. Note, we&#8217;re using the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~baeldung.com\/hamcrest-core-matchers\">Hamcrest matcher<\/a> method <em>is(T) <\/em>as the matcher here.<\/p>\n<h3 data-id=\"2-using-assumetrue\"><strong>2.2. Using <em>assumeTrue<\/em> <\/strong><\/h3>\n<div class=\"bd-anchor\" id=\"2-using-assumetrue\"><\/div>\n<p>Similarly, we can use the\u00a0<em>assumeTrue()<\/em>\u00a0method to specify a boolean expression that must evaluate to\u00a0<em>true<\/em>\u00a0in order for the test to run. If it evaluates to\u00a0<em>false<\/em>, the test will be ignored:<\/p>\n<pre><code class=\"language-java\">private boolean isExpectedOS(String osName) {\r\n    return \"Linux\".equals(osName);\r\n}\r\n@Test \r\npublic void whenAssumeTrueAndOSIsLinux_thenRunTest() {\r\n    assumeTrue(isExpectedOS(getOsName()));\r\n \r\n    assertEquals(\"run\", \"RUN\".toLowerCase());\r\n}\r\n<\/code><\/pre>\n<p>In this case, <strong><em>isExpectedOs()<\/em> returns <em>true<\/em><\/strong>. Therefore, <strong>the<\/strong> <strong>conditions for the test to run have been met, and the test will be run<\/strong>.<\/p>\n<h3 data-id=\"3-using-assumefalse\">2.3. Using <em>assumeFalse<\/em><\/h3>\n<div class=\"bd-anchor\" id=\"3-using-assumefalse\"><\/div>\n<p>Finally, we can use the opposite <em>assumeFalse()<\/em>\u00a0method to specify a boolean expression that must evaluate to <i>false <\/i>in order for the test to run. If it evaluates to\u00a0<em>true<\/em>, the test will be ignored:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void whenAssumeFalseAndOSIsLinux_thenIgnore() {\r\n    assumeFalse(isExpectedOS(getOsName()));\r\n    assertEquals(\"run\", \"RUN\".toLowerCase());\r\n}<\/code><\/pre>\n<p>In this case, as <em style=\"font-weight: bold\">isExpectedOs() <\/em><strong>also<\/strong><b> returns <\/b><em><b>true,<\/b>\u00a0<\/em><strong>the<\/strong> <strong>conditions for the test to run have not been met, and the test will be ignored<\/strong>.<\/p>\n<h3 data-id=\"4-using-assumenotnull\">2.4. Using <em>assumeNotNull<\/em><\/h3>\n<div class=\"bd-anchor\" id=\"4-using-assumenotnull\"><\/div>\n<p>When we want to ignore a test if some expression is <em>null, <\/em>we can use the<em> assumeNotNull()<\/em> method:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void whenAssumeNotNullAndNotNullOSVersion_thenRun() {\r\n    assumeNotNull(getOsName());\r\n    assertEquals(\"run\", \"RUN\".toLowerCase());\r\n}<\/code><\/pre>\n<p>As <em>getOsName()<\/em> is returning a non-null value, the condition for the test to run has been satisfied and the test will run.<\/p>\n<h3 data-id=\"5-using-assumenoexception\">2.5. Using <em>assumeNoException<\/em><\/h3>\n<div class=\"bd-anchor\" id=\"5-using-assumenoexception\"><\/div>\n<p>Finally, we could want to ignore a test if an exception is thrown. We can use <em>assumeNoException()<\/em> for this purpose:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void whenAssumeNoExceptionAndExceptionThrown_thenIgnore() {\r\n    assertEquals(\"everything ok\", \"EVERYTHING OK\".toLowerCase());\r\n    String t=null;\r\n    try {\r\n        t.charAt(0);\r\n    } catch(NullPointerException npe){\r\n        assumeNoException(npe);\r\n    }\r\n    assertEquals(\"run\", \"RUN\".toLowerCase());\r\n}<\/code><\/pre>\n<p>In this example, as <em>t<\/em> is <em>null,<\/em> <strong>a <em>NullPointerException<\/em> exception is thrown, therefore <\/strong><strong>the <\/strong><strong>conditions for the test to run have not been met, and the test will be ignored<\/strong>.<\/p>\n<h2 data-id=\"where-should-we-put-the-assumexxx-call\">3. Where Should We Put the <em>assumeXXX<\/em> Call?<\/h2>\n<div class=\"bd-anchor\" id=\"where-should-we-put-the-assumexxx-call\"><\/div>\n<p>It&#8217;s important to note that <strong>the behavior of the <em>assumeXXX<\/em> methods depends on where we put them in our tests<\/strong>.<\/p>\n<p>Let&#8217;s slightly modify our <em>assumeThat<\/em> example so the <em>assertEquals()<\/em> call goes first. Also, let&#8217;s make the<em> assertEquals()<\/em> fail:<\/p>\n<pre><code class=\"language-java\">@Test\r\npublic void whenAssumeFalseAndOSIsLinux_thenIgnore() {\r\n    assertEquals(\"run\", \"RUN\");\r\n    assumeFalse(isExpectedOS(getOsName()));\r\n}\r\n<\/code><\/pre>\n<p>When we run this example, we&#8217;ll have:<\/p>\n<pre><code class=\"language-java\">org.junit.ComparisonFailure: \r\nExpected :run\r\nActual   :RUN<\/code><\/pre>\n<p>In this case, <strong>our test is not ignored because it has failed before we reached the<em> assumeThat()<\/em> call.<\/strong> The same happens with all of the <em>assumeXXX<\/em> methods. So, we need to <strong>make sure we put them in the right place inside our test method<\/strong>.<\/p>\n<h2 data-id=\"conclusion-1\">4. Conclusion<\/h2>\n<div class=\"bd-anchor\" id=\"conclusion-1\"><\/div>\n<p>In this short tutorial, we&#8217;ve seen how we can conditionally decide whether or not a test should run, using the <em>Assume<\/em> class in JUnit 4. <strong>In case we are using JUnit 5, it&#8217;s also available in version 5.4 or later<\/strong>.<\/p>\n<p>As always, the source code of the examples we&#8217;ve been through can be found <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/testing-modules\/junit-4\"  rel=\"noopener noreferrer\">over on GitHub<\/a>.<\/p>\n<p>The post <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/junit-conditional-assume\/\" >Conditionally Run or Ignore Tests in JUnit 4<\/a> first appeared on <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/\" >Baeldung<\/a>.<\/p>\n<p><Img align=\"left\" border=\"0\" height=\"1\" width=\"1\" alt=\"\" style=\"border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;\" hspace=\"0\" src=\"https:\/\/feeds.feedblitz.com\/~\/i\/636049844\/0\/baeldung\"><\/p>\n<div style=\"clear:both;padding-top:0.2em;\"><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/636049844\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/636049844\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/636049844\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/636049844\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/636049844\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/636049844\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/junit-conditional-assume#respond\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&#160;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/junit-conditional-assume\/feed\/\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&#160;<\/div>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Let&#8217;s look at how, based on certain conditions, we can run or ignore tests using JUnit.<\/p>\n<p>The post <a rel=\"NOFOLLOW noopener noreferrer\" href=\"https:\/\/feeds.feedblitz.com\/~\/636049844\/0\/baeldung~Conditionally-Run-or-Ignore-Tests-in-JUnit\/\" target=\"_blank\">Conditionally Run or Ignore Tests in JUnit 4<\/a> first appeared on <a rel=\"NOFOLLOW noopener noreferrer\" href=\"https:\/\/www.baeldung.com\/\" target=\"_blank\">Baeldung<\/a>.<\/p>\n<div><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/636049844\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\"><\/a>&nbsp;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/636049844\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\"><\/a>&nbsp;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/636049844\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\"><\/a>&nbsp;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/636049844\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\"><\/a>&nbsp;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/636049844\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\"><\/a>&nbsp;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/636049844\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\"><\/a>&nbsp;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/junit-conditional-assume#respond\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&nbsp;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/junit-conditional-assume\/feed\/\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&nbsp;<\/div>\n<\/div>","protected":false},"author":919,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Conditionally Run or Ignore Tests in JUnit 4 - ITTeacherITFreelance.hk","description":"Let's look at how, based on certain conditions, we can run or ignore tests using JUnit. The post Conditionally Run or Ignore Tests in JUnit 4 first appeared on"},"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/83805"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/919"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=83805"}],"version-history":[{"count":4,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/83805\/revisions"}],"predecessor-version":[{"id":85411,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/83805\/revisions\/85411"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=83805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=83805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=83805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}