IntelliJ IDEA 2018.2 Help

Run, debug and test Scala

IntelliJ IDEA lets you run, debug and test your Scala applications as you would normally do with any other applications in IntelliJ IDEA. IntelliJ IDEA also lets you run your Scala code with coverage and configure code coverage settings.

Run Scala applications

You can run your Scala code through IntelliJ IDEA, use sbt shell, or use Scala worksheet for a quick code evaluation.

Run a Scala application via Intellij IDEA

  1. Create or import a Scala project as you would normally create or import any other project in IntelliJ IDEA.

  2. Open your application in the editor.

  3. Press Shift+F10 to execute the application. Alternatively, in the left gutter of the editor, click the icons toolwindows toolWindowRun svg icon and select Run 'name'.

    scala run app editor

Run a Scala application using the sbt shell

You can run your application using the sbt shell that is a part of any sbt project.

  1. Open your sbt project.

  2. If you want to delegate your builds and imports to sbt, in the sbt projects tool window, click the icons general settings svg icon to open the sbt settings.
    On the sbt settings page, select the Use sbt shell for build and import (required sbt 0.13.5+) option and click OK.

    sbt settings for running sbt shell
    (This option is also available when you create or import an sbt project.)
    If you don't want to delegate your builds and imports to sbt, you can still work in the sbt shell (sbt shell icon) and run sbt commands directly from it including running your application.

  3. In the sbt projects tool window, click sbt Tasks node.

  4. In the list that opens, select the run task that will run a main method.

    sbt tool window run task
    The result of execution is displayed in sbt shell tool window.
    sbt shell run app

Run Scala code using Scala worksheet

IntelliJ IDEA lets you create a Scala worksheet to evaluate your Scala code results on the fly.

  1. Right-click on your project and select New|Scala Worksheet.

    We recommend that you create your worksheet in src directory to simplify an inclusion of the project's classes in the classpath. That might be helpful for testing purposes.

  2. In the New Scala Worksheet window, type a name of your Scala worksheet and click OK.

    As a result, a file with .sc extension opens.

    sc worksheet
  3. Enter your code and press icons toolwindows toolWindowRun svg to see the results.

  4. While working with the .sc file, you can perform the following actions:
    • You can evaluate a Scala object.

    • You can fold the output without affecting your code on the left side and expand only that block of the output that matches a specific statement.

      sc wsheet unfold

    • You can click the settings icons general settings svg icon on the toolbar of the worksheet editor and configure the current worksheet settings or default worksheet settings for the whole project that will affect other worksheets.
      sc ws settings
      • If you want to see the results automatically, select the Interactive Mode checkbox.

      • Clear the Make project checkbox to improve evaluation performance. In this case, the automatic checking of project's changes is disabled.

      • You can select the Run type option to see different evaluation modes. For example, select the REPL type to quickly evaluate Scala expressions. When you add new expressions they are executed incrementally.

      • Notice that the Use class path of module option is disabled since the worksheet is a file that can you can share between other projects and should stay where you created it. You can change its location in the default settings, but it will be applied across the whole project and will affect other worksheets. If you need to change a class path, we recommend that you create and use a scratch file instead.

        sc scratch file

      • You can also select a compiler profile for your worksheet. Click the icons general settings svg to access the Scala compiler settings for more options. Keep in mind that if you add/change a profile through the compiler settings page and you have an sbt project, your changes will be discarded on the next project import.

      • For more options on the worksheet settings, select File|Settings|Languages and Frameworks|Scala and click the Worksheet tab.
        sc worksheet settings
        For example, on the Worksheet tab, you can set the output cutoff limit, specify in what mode you want to run the worksheet, or configure how you want IntelliJ IDEA to treat .sc and scratch files.

Debug Scala code

IntelliJ IDEA lets you debug your code using IntelliJ IDEA debugger or the sbt shell.

Debug Scala code using Intellij IDEA

  1. Open your Scala application in the editor.

  2. In the left gutter, set your breakpoints for the lines of code you want to debug. For more information on breakpoints, please see the Using Breakpoints topic.

  3. If you need, you can access the Run/Debug configurations (Run | Edit Configurations) and adjust the settings, but usually the default settings are enough to successfully start and complete your debugging session.

  4. Press Shift+F9. Alternatively, on the main toolbar, click the icons actions startDebugger svg icon to start a debugging process.

  5. Evaluate the results in the Debug tool window.

    For information on how to use options in the Debug tool window, please see Debug tool window references.

Debug Scala code using sbt shell

  1. Open your sbt project.

  2. Open your application in the editor.

  3. In the editor, in the left gutter, set your breakpoints for the lines of code you want to debug.
    scala code breakpoints
  4. In the sbt projects tool window, click the sbt shell icon icon, to start the sbt shell.

  5. In the sbt shell tool window, click the icons actions startDebugger svg icon to connect to the debugger server and start debugging session.
    IntelliJ IDEA also creates a Run/Debug configuration for the debugging session.

    sbt run debug config for sbt shell
    If you need, you can edit the run/debug configuration settings, but the default settings should be enough to successfully debug your code.

  6. Run your program.

    sbt shell program
    Evaluate your results in the Debug tool window.
    sbt debug tool window

Test Scala applications

IntelliJ IDEA lets you test your Scala applications using ScalaTest and Specs2. You can also set and run the test scopes.

Test Scala applications using Scala Test

  1. Open your project.
    • If you have an sbt project, open the build.sbt file and specify the following dependencies for ScalaTest:
      libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.1"
      libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"

      Click icons actions refresh svg icon in the sbt projects tool window to refresh your project or use the Auto-import option specified in the sbt settings to automatically refresh your project each time you make changes to build.sbt.

    • If you have a regular Scala project, use the Project Structure dialog, to configure test libraries.

  2. Open a class in the editor, for which you want to create a test and place the cursor within the line containing the class declaration.

  3. Press Ctrl+Shift+T and select Create New Test.

  4. In the dialog that opens, specify your test settings and click OK.

  5. Open the test in the editor, press Ctrl+Shift+F10 or right-click on the test class and from the context menu select Run 'test name'.

  6. IntelliJ IDEA creates a run/debug configuration for the test automatically, but if you want to edit settings in your configuration, click Run | Edit Configurations on the main menu.

  7. In the Run/Debug Configurations dialog, on the right-hand side, specify settings for the test suite and click OK.
    The configuration has standard options and you can find further details in the Performing Tests section. However, you can also specify the following Scala-related options:
    • You can select the Use sbt shell checkbox to run your test via sbt shell and the Use UI with sbt to display the test results in the same format as for platform test runner.

    • You can select the Test kind option to specify what kind of test you want to run. For example, you can select Regular expression and set class and test patterns.

  8. On the main toolbar, click the icons toolwindows toolWindowRun svg icon to run the test.

  9. Evaluate the results in the Run tool window.

Test a Scala application using Specs2

The procedure for testing a Scala application using Specs2 is the same as the procedure described in the Test a Scala application using ScalaTest section except for the following options:

  • You need to specify the following dependency for your sbt project:
    libraryDependencies ++= Seq("org.specs2" %% "specs2-core" % "3.9.5" % "test")

    For regular Scala projects, use the Project Structure dialog, to configure the test library.

  • The Use UI with sbt option that displays the test results in the same format as for platform test runner is not available.

Test scopes in Scala

IntelliJ IDEA lets you test scopes using ScalaTest or Specs2.

You can run tests inside a scope or test the whole scope in your Scala projects.

  1. Create your code. Check the following example:

    scalaTest code RunTestInScope

  2. In the editor, depending on your test scope you can perform the following:
    • If you open context menu for one of the tests inside the scope, you can create a run configuration just for the specified test inside that scope.

      scalaTest RunDebugConf for Test in Scope

    • If you open a context menu for the whole scope, you can create a run configuration for all tests inside the scope.

      scalaTest RunDebug for Scope

  3. Run your tests and view the output in the Run tool window.

    scalaTes console RunTestInScope
    You can also use the sbt shell to run scope tests.

Run Scala tests with coverage

IntelliJ IDEA lets you run your test suite with code coverage.

  1. Open your project.

  2. Open the test in question in the editor.

  3. In the left gutter, click the icons toolwindows toolWindowRun svg icon and select the Run 'name' with Coverage option.
    IntelliJ IDEA runs the test and displays the Coverage tool window with code coverage information.

    You can also adjust default code coverage settings or code coverage behavior if you need.

    If you have already run you test suite, IntelliJ IDEA creates the run/debug configuration automatically. You can open the created run configuration and adjust the settings.
    If you want to create a new run configuration for the test suite, do the following:

    1. From the main menu, select Run | Edit Configurations.

    2. In the Run/Debug Configurations dialog, click the icons general add icon from the options on the left.

    3. From the list that opens, select the configuration you need.

    4. From the options on right, click the Code Coverage tab.

    5. Adjust the default settings and click OK.
      For more information, please see Configuring Code Coverage Measurement

    You can also adjust code coverage behavior.

    1. Press Ctrl+Alt+S to open the Settings/Preferences page.

    2. From the options on the left, select Build, Execution, Deployment | Coverage.

    3. From the options on right, adjust the settings and click OK.

Last modified: 20 November 2018