DEV Community

Cover image for Tests Everywhere - Scala
Roger Viñas Alcon
Roger Viñas Alcon

Posted on • Edited on

Tests Everywhere - Scala

GitHub logo rogervinas / tests-everywhere

🤠 Tests, Tests Everywhere!

Scala testing this simple Hello World with ScalaTest and Mockito

Show me the code

Implementation

  1. Create HelloMessage class in HelloMessage.scala:
class HelloMessage:
  def text: String = "Hello World!"
Enter fullscreen mode Exit fullscreen mode

We can create it as a class as Mockito is able to mock final classes.

  1. Same way create HelloConsole class in HelloConsole.scala:
class HelloConsole:
  def print(text: String): Unit =
    println(text)
Enter fullscreen mode Exit fullscreen mode
  1. Create HelloApp class in HelloApp.scala:
class HelloApp(message: HelloMessage, console: HelloConsole):
  def printHello(): Unit =
    console.print(message.text)
Enter fullscreen mode Exit fullscreen mode
  1. Create main class in Main.scala that wraps it all together:
@main def main(): Unit =
  val message = HelloMessage()
  val console = HelloConsole()
  val app = HelloApp(message, console)
  app.printHello()
Enter fullscreen mode Exit fullscreen mode

Test

Following ScalaTest > Writing your first test and ScalaTest + Mockito guides ...

  1. Test HelloMessage in HelloMessageSpec.scala:
class HelloMessageSpec extends AnyFlatSpec {

  "A HelloMessage" should "return hello world" in {
    val message = HelloMessage()
    assertResult(message.text) {
      "Hello World!"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Test HelloApp in HelloAppSpec.scala:
class HelloAppSpec extends AnyFlatSpec {

  "A HelloApp" should "print hello message" in {
    val messageText = "Hello Test!"

    // 2.1 Create a mock of HelloMessage
    val message = mock[HelloMessage]
    // 2.2 Return "Hello Test!" whenever text is called
    when(message.text).thenReturn(messageText)

    // 2.3 Create a mock of HelloConsole
    val console = mock[HelloConsole]

    // 2.4 Create a HelloApp, the one we want to test, passing the mocks
    val app = HelloApp(message, console)
    // 2.5 Execute the method we want to test
    app.printHello()

    // 2.6 Verify HelloConsole mock print() method
    // has been called once with "Hello Test!"
    verify(console).print(messageText)
  }
}
Enter fullscreen mode Exit fullscreen mode
  1. Test output should look like:
[info] HelloMessageSpec:
[info] A HelloMessage
[info] - should return hello world
[info] HelloAppSpec:
[info] A HelloApp
[info] - should print hello message
[info] Run completed in 515 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 2, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 6 s
Enter fullscreen mode Exit fullscreen mode

Happy Testing! 💙

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️