{"id":120,"date":"2024-02-28T08:53:20","date_gmt":"2024-02-28T11:53:20","guid":{"rendered":"https:\/\/springmasteryhub.com\/?p=120"},"modified":"2024-02-28T08:53:20","modified_gmt":"2024-02-28T11:53:20","slug":"how-to-test-complex-conditions-with-the-mc-dc-criteria","status":"publish","type":"post","link":"https:\/\/springmasteryhub.com\/?p=120","title":{"rendered":"How to test complex conditions with the MC\/DC criteria"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p>Have you needed help defining what to test when you encounter a complex condition in your code? Do you feel lazy when looking at a condition that has too many logical statements?<\/p>\n\n\n\n<p>It\u2019s easy to look at that big if statement that you find in legacy code and think: \u201cMan, what should I test here? Should I cover all the possible combinations in this code? If I do every possible combination will I catch all the bugs?\u201d.<\/p>\n\n\n\n<p>The answer to all these questions is no. Testing everything in your code to cover all details as much as possible will be expensive. You can find new ways to save time, cover more cases, and spot hidden bugs in your code. How can you do this?<\/p>\n\n\n\n<p>You can do this by MC \/ DC criteria. It guides you to test combinations for well-checked code. Let\u2019s find out how.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Understanding the problem of complex conditions<\/h3>\n\n\n\n<p>If you have a logical statement it can be <code>true<\/code> or <code>false<\/code> which is 2 possibilities.<\/p>\n\n\n\n<p>That\u2019s our starting point. If a single logical operation composes the condition of our code. Then the amount of possibilities to test will be only two (true or false), so it\u2019s easy to test all combinations.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nif(age &gt;= 18)\n\n\n<\/pre><\/div>\n\n\n<p>The only possible combinations here are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>an age that is equal to or higher than 18<\/li>\n\n\n\n<li>an age that is less than 18<\/li>\n<\/ul>\n\n\n\n<p>That\u2019s an easy case.<\/p>\n\n\n\n<p>But as the complexity grows, the number of possible combinations also grows.<\/p>\n\n\n\n<p>Let\u2019s take a look at this example:<\/p>\n\n\n\n<p>Let\u2019s consider a student. They can join the field trip if their parents agree. They must also be in grade 12 or part of the science club<\/p>\n\n\n\n<p>The code will look something like this:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nif (hasParentalPermission &amp;&amp; (grade == 12 || isScienceClubMember)) {\n    System.out.println(&quot;The student can go on the field trip.&quot;);\n} else {\n    System.out.println(&quot;The student cannot go on the field trip.&quot;);\n}\n\n\n<\/pre><\/div>\n\n\n<p>So the total of combinations will be 2\u00b3. Which is 8.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Test Case<\/th><th>hasParentalPermission<\/th><th>grade == 12<\/th><th>isScienceClubMember<\/th><th>Result<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>True<\/td><td>True<\/td><td>True<\/td><td>True<\/td><\/tr><tr><td>2<\/td><td>True<\/td><td>True<\/td><td>False<\/td><td>True<\/td><\/tr><tr><td>3<\/td><td>True<\/td><td>False<\/td><td>True<\/td><td>True<\/td><\/tr><tr><td>4<\/td><td>True<\/td><td>False<\/td><td>False<\/td><td>False<\/td><\/tr><tr><td>5<\/td><td>False<\/td><td>True<\/td><td>True<\/td><td>False<\/td><\/tr><tr><td>6<\/td><td>False<\/td><td>True<\/td><td>False<\/td><td>False<\/td><\/tr><tr><td>7<\/td><td>False<\/td><td>False<\/td><td>True<\/td><td>False<\/td><\/tr><tr><td>8<\/td><td>False<\/td><td>False<\/td><td>False<\/td><td>False<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Table 1 &#8211; All possible conditions<\/figcaption><\/figure>\n\n\n\n<p>So now if you want to cover all possibilities you\u2019ll have to write 8 scenarios for a simple code. That\u2019s starting to become expensive.<\/p>\n\n\n\n<p>If we have 4 conditions, that will be even worse, meaning that we will have 2\u2074 (16) possible conditions.<\/p>\n\n\n\n<p>One issue is that many conditions are repetitive. This can complicate things when trying to account for all scenarios.<\/p>\n\n\n\n<p>So that\u2019s the point of MC\/DC. This allows you to bypass these possibilities while maintaining reliable testing. It will reduce the number of possibilities to N + 1 (condition count + 1).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to apply MC\/DC in 2 simple steps<\/h2>\n\n\n\n<p>Follow these steps:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You must test each condition as true and false once.<\/li>\n\n\n\n<li>Each condition has to affect the resulting outcome independently.<\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>Let\u2019s apply the MC\/DC for the condition that creates the above table, to reduce the amount of possibilities.<\/p>\n\n\n\n<p>By doing so we can extract a N+1 table of possibilities:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>hasParentalPermission<\/th><th>grade == 12<\/th><th>isScienceClubMember<\/th><th>Result<\/th><th>Testing<\/th><\/tr><\/thead><tbody><tr><td>T<\/td><td>T<\/td><td>T<\/td><td>True<\/td><td>Testing all conditions true<\/td><\/tr><tr><td>T<\/td><td>T<\/td><td>F<\/td><td>True<\/td><td>Test how:<\/td><\/tr><tr><td>grade ==12<\/td><td><\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td>Affects the outcome<\/td><td><\/td><td><\/td><td><\/td><td><\/td><\/tr><tr><td>T<\/td><td>F<\/td><td>T<\/td><td>True<\/td><td>Test how isScienceClubMember affects the outcome<\/td><\/tr><tr><td>F<\/td><td>T<\/td><td>T<\/td><td>False<\/td><td>Test how hasParentalPermission affects<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">Table 2 &#8211; Reduced condition scope using MC\/DC<\/figcaption><\/figure>\n\n\n\n<p>If you look closely, each condition has been evaluated as true and false once. Each of them had the chance to change the result outcome. Let\u2019s check.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2018hasParentalPermission\u2019 condition could alter the result. It was tested as true and false at least once.<\/li>\n\n\n\n<li><strong>grade == 12<\/strong> had the chance to change the outcome and was true and false at least once.<\/li>\n\n\n\n<li><strong>isScienceClubMember<\/strong> had the chance to change the outcome and was true and false at least once.<\/li>\n<\/ul>\n\n\n\n<p>It doesn&#8217;t seem very easy but is very simple. Practice for a while and it will become second nature. This will help a lot in creating efficient unit tests.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>You learned a quick way from this article. It\u2019s for testing tricky parts in your code. Now, deciding what to test is easier. This helps spot issues in your code.<\/p>\n\n\n\n<p>Now, it\u2019s your turn. Practice this. Look at your code. Find places to use this method. This will save you a ton of time and can avoid some headaches too!<\/p>\n\n\n\n<p>Don\u2019t forget to follow me on social media to be the first to read when my next article comes out!<\/p>\n\n\n\n<p><a href=\"https:\/\/twitter.com\/WillianFMoya\">Willian Moya (@WillianFMoya) \/ X (twitter.com)<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www.linkedin.com\/in\/willianmoya\/\">Willian Ferreira Moya | LinkedIn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Have you needed help defining what to test when you encounter a complex condition in your code? Do you feel lazy when looking at a condition that has too many logical statements? It\u2019s easy to look at that big if statement that you find in legacy code and think: \u201cMan, what should I test [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":122,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[13,18,20,26,27],"class_list":["post-120","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-best-pratices","tag-java","tag-programming","tag-software-development","tag-technology","tag-testing"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=120"}],"version-history":[{"count":0,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/wp\/v2\/posts\/120\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springmasteryhub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}