{"id":4743,"date":"2017-03-26T19:47:55","date_gmt":"2017-03-26T19:47:55","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4743"},"modified":"2017-03-26T19:47:55","modified_gmt":"2017-03-26T19:47:55","slug":"learning-scala-day-2","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/learning-scala-day-2\/","title":{"rendered":"Learning Scala &#8211; day 2"},"content":{"rendered":"<p>In my first post on learning Scala I mentioned using ScalaTest. I&#8217;m going to walk through the steps (using IntelliJ community edition) to get up and running with ScalaTest as it&#8217;s a great way to try things out (ofourse you could alternatively use the Scala console).<\/p>\n<ul>\n<li>Create a new project<\/li>\n<li>Select Scala and SBT<\/li>\n<li>Give the project a name, mine&#8217;s named Demo<\/li>\n<li>If not already selected, select the JDK you want to support as well as the SBT and Scala version, or stick with the defaults if they&#8217;re already setup correctly<\/li>\n<\/ul>\n<p>Upon finishing the new project wizard, you&#8217;ll get a standard layout of folders, as follows<\/p>\n<p>src\/main\/scala<br \/>\nsrc\/test\/scala<\/p>\n<p>In the root of the project you&#8217;ll get a build.sbt file. This is the <a href=\"http:\/\/www.scala-sbt.org\/index.html\" target=\"_blank\">Scala build system<\/a>. If we open it, IntelliJ has supplied the bare minimum <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nname := &quot;Demo&quot;\r\nversion := &quot;1.0&quot;\r\nscalaVersion := &quot;2.12.1&quot;\r\n<\/pre>\n<p>Before we do anything more let&#8217;s go into src\/main\/scala, right mouse click on the folder and select New Scala class. Give it the name Calculator, here&#8217;s the code<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\nclass Calculator {\r\n  def add(a: Int, b: Int) = {\r\n    a + b\r\n  }\r\n\r\n  def subtract(a: Int, b: Int ) = {\r\n    a - b\r\n  }\r\n}\r\n<\/pre>\n<p>Now, go back to the build.sbt file and add the following<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nlibraryDependencies += &quot;org.scalactic&quot; %% &quot;scalactic&quot; % &quot;3.0.1&quot;\r\n<\/pre>\n<p>When the build.sbt file was changed you&#8217;ll see it asking you to Refresh the Project, Enable auto-import or Ignore. I clicked Enable auto-import.<\/p>\n<p>This will then import more Extendal Libraries (if you want to see what was imported, expand the External Libraries in the project view alt+1, if not already displayed)<\/p>\n<p>Now, we could have done this in one step (but I wanted to demonstrate how the external libraries get added to), but now add the following<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nlibraryDependencies += &quot;org.scalatest&quot; %% &quot;scalatest&quot; % &quot;3.0.1&quot; % &quot;test&quot;\r\n<\/pre>\n<p>further external libraries will get imported if you refresh (or if auto-import is enabled, after a moment). These now include scalatest.<\/p>\n<p>We&#8217;re going to let IntelliJ create our initial unit test fixture. To ensure everything is in place, carry out the following steps<\/p>\n<ul>\n<li>Press the Project Structure button or File | Project Structure<\/li>\n<li>In the Dependency tab or your project (i.e. mine&#8217;s demo) check the scalatest project has has been imported or add your preferred test package here<\/li>\n<li>Press OK to close the dialog (or if no changes were made then Cancel is ofcourse fine)<\/em>\n<\/ul>\n<p>Or if you&#8217;re happy all is ready then&#8230;<\/p>\n<ul>\n<li>Load Calculator.scala into the editor<\/li>\n<li>On the line of the class either right mouse click and select Goto | Test or the hot keys ctrl+shift+t<\/li>\n<li>You should see a Create New Test popup<\/li>\n<li>Select this to display a dialog<\/li>\n<li>Now select the functions you want to create tests for, then press OK.<\/li>\n<\/ul>\n<p>You&#8217;ll now be placed into the newly added CalculatorTest class and it&#8217;ll look like this<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\nimport org.scalatest.FunSuite\r\n\r\nclass CalculatorTest extends FunSuite {\r\n\r\n  test(&quot;testAdd&quot;) {\r\n  }\r\n\r\n  test(&quot;testSubtract&quot;) {\r\n  }\r\n}\r\n<\/pre>\n<p>From here we can write our test code and further tests.<\/p>\n<p>Like other test frameworks we now have access to assert functions, so for example we can have the testAdd look like this<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\ntest(&quot;testAdd&quot;) {\r\n   val calc = new Calculator\r\n    \r\n    assert(calc.add(1, 2) == 3)\r\n  }\r\n<\/pre>\n<p>org.scalatest contains a whole bunch of assert functions that we can use in our tests. <\/p>\n<p>ScalaTest is not the only scala based test library<\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/www.jetbrains.com\/help\/idea\/2016.3\/working-with-scala-tests.html\" target=\"_blank\">Working with Scala Tests<\/a><br \/>\n<a href=\"http:\/\/www.scalatest.org\/\" target=\"_blank\">ScalaTest<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my first post on learning Scala I mentioned using ScalaTest. I&#8217;m going to walk through the steps (using IntelliJ community edition) to get up and running with ScalaTest as it&#8217;s a great way to try things out (ofourse you could alternatively use the Scala console). Create a new project Select Scala and SBT Give [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[165,166],"tags":[],"class_list":["post-4743","post","type-post","status-publish","format-standard","hentry","category-scala","category-scalatest"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4743","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4743"}],"version-history":[{"count":8,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4743\/revisions"}],"predecessor-version":[{"id":4782,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4743\/revisions\/4782"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}