<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Krzysztof Komorowski</title>
    <description>The latest articles on DEV Community by Krzysztof Komorowski (@kkomorowski).</description>
    <link>https://dev.to/kkomorowski</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F24703%2F17694504-2a6b-4e04-8d6d-f55ad0cbafbf.png</url>
      <title>DEV Community: Krzysztof Komorowski</title>
      <link>https://dev.to/kkomorowski</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kkomorowski"/>
    <language>en</language>
    <item>
      <title>Automated Acceptance Tests with ScalaTest</title>
      <dc:creator>Krzysztof Komorowski</dc:creator>
      <pubDate>Fri, 23 Feb 2024 18:52:42 +0000</pubDate>
      <link>https://dev.to/kkomorowski/automated-acceptance-tests-with-scalatest-58b</link>
      <guid>https://dev.to/kkomorowski/automated-acceptance-tests-with-scalatest-58b</guid>
      <description>&lt;p&gt;Almost seven years ago I've started a new and unexpected journey. Back then I had some experience with test automation in Python and Java and I've joined the team working on the backend application written in Scala. It was my first experience with functional programming so I was wondering if&lt;br&gt;
it would not be hard to learn and maintain the test suite written in this new for me language. It turned out that it's not and the work with Scala gave me a lot of fun.&lt;/p&gt;

&lt;p&gt;In this first episode of the blog post series I would like to show how to start testing a REST API using ScalaTest and how to use some more advanced features of this one of the most popular Scala-based test frameworks.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up ScalaTest test project.
&lt;/h2&gt;

&lt;p&gt;Scala is a JVM based language so we will need to have JDK installed (I usually use the latest LTS release - 21 at the time of writing this article).&lt;/p&gt;

&lt;p&gt;We would also use &lt;a href="https://www.scala-sbt.org/"&gt;SBT&lt;/a&gt; - the most popular build tools for Scala projects.&lt;/p&gt;

&lt;p&gt;After installing both we can start a new ScalaTest project from scratch using following in your command line:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sbt new scala/scalatest-example.g8&lt;/code&gt; &lt;sup id="fnref1"&gt;1&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;You can also clone the GitHub repository with the code presented in this article: &lt;a href="https://github.com/kkomorowski/scalatest-getting-started"&gt;https://github.com/kkomorowski/scalatest-getting-started&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Some Gherkin, please!
&lt;/h2&gt;

&lt;p&gt;The clue of the Behavior-Driven Development are the well defined test scenarios that might be used as the documentation of the system. ScalaTest is a very versatile test framework that gives the possibility of choosing the test style that best suites your needs.&lt;/p&gt;

&lt;p&gt;In the example we'll use &lt;code&gt;FeatureSpec&lt;/code&gt; and &lt;code&gt;GivenWhenThen&lt;/code&gt; from ScalaTest to describe the scenarios of our tested feature using Gherkin-like DSL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GitHubUserSpec&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AnyFeatureSpec&lt;/span&gt; &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nc"&gt;GivenWhenThen&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;

  &lt;span class="kt"&gt;Feature&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="kt"&gt;GitHub&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt; &lt;span class="kt"&gt;Profile&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="kt"&gt;:&lt;/span&gt;

    &lt;span class="kt"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="kt"&gt;As&lt;/span&gt; &lt;span class="kt"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;programmer&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kt"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="kt"&gt;I&lt;/span&gt; &lt;span class="kt"&gt;want&lt;/span&gt; &lt;span class="kt"&gt;to&lt;/span&gt; &lt;span class="kt"&gt;check&lt;/span&gt; &lt;span class="kt"&gt;the&lt;/span&gt; &lt;span class="kt"&gt;user&lt;/span&gt; &lt;span class="kt"&gt;profile&lt;/span&gt; &lt;span class="kt"&gt;presence&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kt"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="kt"&gt;So&lt;/span&gt; &lt;span class="kt"&gt;I&lt;/span&gt; &lt;span class="kt"&gt;know&lt;/span&gt; &lt;span class="kt"&gt;if&lt;/span&gt; &lt;span class="kt"&gt;I&lt;/span&gt; &lt;span class="kt"&gt;can&lt;/span&gt; &lt;span class="kt"&gt;create&lt;/span&gt; &lt;span class="kt"&gt;an&lt;/span&gt; &lt;span class="kt"&gt;account&lt;/span&gt; &lt;span class="kt"&gt;with&lt;/span&gt; &lt;span class="kt"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;given&lt;/span&gt; &lt;span class="kt"&gt;name&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

    &lt;span class="kt"&gt;Scenario&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="kt"&gt;GitHub&lt;/span&gt; &lt;span class="kt"&gt;User&lt;/span&gt; &lt;span class="kt"&gt;Exists&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="kt"&gt;:&lt;/span&gt;
      &lt;span class="kt"&gt;Given&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="kt"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;GitHub&lt;/span&gt; &lt;span class="kt"&gt;account&lt;/span&gt; &lt;span class="kt"&gt;exists&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="kt"&gt;When&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="kt"&gt;I&lt;/span&gt; &lt;span class="kt"&gt;send&lt;/span&gt; &lt;span class="kt"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;request&lt;/span&gt; &lt;span class="kt"&gt;to&lt;/span&gt; &lt;span class="kt"&gt;the&lt;/span&gt; &lt;span class="kt"&gt;/user/&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="kt"&gt;username&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="kt"&gt;API&lt;/span&gt; &lt;span class="kt"&gt;endpoint&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="kt"&gt;Then&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="kt"&gt;I&lt;/span&gt; &lt;span class="kt"&gt;get&lt;/span&gt; &lt;span class="kt"&gt;the&lt;/span&gt; &lt;span class="kt"&gt;response&lt;/span&gt; &lt;span class="kt"&gt;with&lt;/span&gt; &lt;span class="kt"&gt;HTTP&lt;/span&gt; &lt;span class="kt"&gt;status&lt;/span&gt; &lt;span class="kt"&gt;code&lt;/span&gt; &lt;span class="err"&gt;200"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;NOTE: The examples use the Scala 3 &lt;a href="https://docs.scala-lang.org/scala3/reference/other-new-features/indentation.html"&gt;significant indentation&lt;/a&gt;. This way of writing of ScalaTest scenarios even more resembles Cucumber &lt;code&gt;feature&lt;/code&gt; files to me.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's make a request!
&lt;/h2&gt;

&lt;p&gt;Unlike Cucumber or JBehave our &lt;code&gt;Given When Then&lt;/code&gt; steps are purely informational and does not bring any implementation of the test behind. We need to implement the test code ourself.&lt;/p&gt;

&lt;p&gt;Let's try to make a first request. Scala is highly compatible with Java so we could use one of the well known API testing libraries like RestAssured but here I would like to show you &lt;a href="https://sttp.softwaremill.com/en/stable/"&gt;sttp&lt;/a&gt; - a Scala-based library that will send the HTTP request for us.&lt;/p&gt;

&lt;p&gt;Simply add this line to your &lt;code&gt;build.sbt&lt;/code&gt; project file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="n"&gt;libraryDependencies&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"com.softwaremill.sttp.client3"&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt; &lt;span class="s"&gt;"core"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="s"&gt;"3.9.3"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and import to easily use Java HTTP client from Scala code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sttp.client3.quick.&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can fill up our scenario with the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nc"&gt;Scenario&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"GitHub User Exists"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;
  &lt;span class="kt"&gt;Given&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="kt"&gt;a&lt;/span&gt; &lt;span class="kt"&gt;GitHub&lt;/span&gt; &lt;span class="kt"&gt;account&lt;/span&gt; &lt;span class="kt"&gt;exists&lt;/span&gt;&lt;span class="err"&gt;"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="kt"&gt;val&lt;/span&gt; &lt;span class="kt"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"kkomorowski"&lt;/span&gt;
  &lt;span class="nc"&gt;When&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I send a request to the /user/{username} API endpoint"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;request&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;quickRequest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="s"&gt;"https://api.github.com/users/$username"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;response&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;simpleHttpClient&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;send&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="nc"&gt;Then&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I get the response with HTTP status code 200"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ... and assert on the response.
&lt;/h2&gt;

&lt;p&gt;The only thing left is to assert on the response from the API.&lt;br&gt;
We can use ScalaTest matchers to have the code more readable:&lt;/p&gt;

&lt;p&gt;Add &lt;code&gt;org.scalatest.matchers.must.Matchers&lt;/code&gt; to the imports and extend your test class with &lt;code&gt;Matchers&lt;/code&gt; trait. Now we can write the assertion the following way:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;  &lt;span class="nv"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;code&lt;/span&gt; &lt;span class="n"&gt;mustBe&lt;/span&gt; &lt;span class="nc"&gt;StatusCode&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Running your first tests
&lt;/h2&gt;

&lt;p&gt;The test is ready to be run. You can run it easily from the IDE (IntelliJ IDEA is able to do it when equipped with Scala plugin. VSCode needs Metals extension).&lt;/p&gt;

&lt;p&gt;To run the tests from the command line we can use &lt;code&gt;sbt&lt;/code&gt;, just write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sbt test
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in your command line.&lt;/p&gt;

&lt;p&gt;After few lines of the logs from the build stage you should see a simple text base test report similar to this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;[info] GitHubUserSpec:
[info] Feature: GitHub User Profile
[info]   As a programmer 
[info]   I want to check the user profile presence 
[info]   So I know if I can create an account with a given name 
[info]   Scenario: GitHub User Exists
[info]     Given a GitHub account exists 
[info]     When I send a request to the /user/{username} API endpoint 
[info]     Then I get the response with HTTP status code 200
[info] Run completed in 1 second, 811 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  End words
&lt;/h2&gt;

&lt;p&gt;In this article we've written our acceptance tests of the REST API using ScalaTest. Using &lt;code&gt;FeatureSpec&lt;/code&gt; and &lt;code&gt;GivenWhenThen&lt;/code&gt; trait we've been able describe the test scenarios and add the logic code of the test using STTP library to make the HTTP request and add a simple assertion on the result. &lt;/p&gt;

&lt;p&gt;I hope that this short guide gave you an inspiration to try ScalaTest in your testing project and convinced you that it might not be that hard to learn.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;You can find an official guide on the Scala Language website: &lt;br&gt;
  &lt;a href="https://docs.scala-lang.org/getting-started/sbt-track/testing-scala-with-sbt-on-the-command-line.html"&gt;https://docs.scala-lang.org/getting-started/sbt-track/testing-scala-with-sbt-on-the-command-line.html&lt;/a&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>testing</category>
      <category>scala</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
