IntelliJ IDEA 2023.3 Help

Run, debug, and test Scala projects

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.

For a general overview of running programs in IntelliJ IDEA, refer to Running Applications.

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 editor gutter, click the Run icon and select Run 'name'.

    Run main

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 tool window, click the icon and select sbt Settings.

    On the sbt settings page, under the sbt shell select the build option (required sbt 0.13.5+) and click OK to save the changes.

    sbt settings

    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 (click sbt shell on the status bar) and run sbt commands directly from it including running your application.

  3. In the sbt projects tool window, click the 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 output

Reload changes and hot swapping

Sometimes, you need to insert minor changes into your code without shutting down the process. Since the Java VM has a HotSwap feature, IntelliJ IDEA handles these cases automatically when you call Make.

Alternative ways to run Scala code

For quick code evaluation, you can use a Scala worksheet that enables you to interactively run your code. Press Ctrl+Alt+Shift+X to create a light version of the Scala worksheet and Ctrl+Alt+W to run it.

Alternatively, you can open Scala REPL and use it to import any class or module of your project and run its methods. For more information, refer to Scala worksheet, Scala REPL and Ammonite.

If you need, you can also deploy your Scala application to a server. For more information, refer to Working with Application Servers.

Debug Scala code

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

For an overview of debugging in IntelliJ IDEA, refer to Debugging.

Debug Scala code using Intellij IDEA

  1. Open your Scala application in the editor.

  2. In the gutter, set your breakpoints for the lines of code you want to debug. For more information about breakpoints, refer to Breakpoints.

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

  4. Press Shift+F9.

    Alternatively, on the project widget, click icon to start debugging.

  5. Evaluate the results in the Debug tool window.

    For more information about using options in the Debug tool window, refer to the Debug code section.

You can adjust the default debugger settings and if you need, turn off the smart step into feature which is enabled by default for the Scala code.

Access the debugger settings

  1. Press Ctrl+Alt+S to open the IDE settings and then select Build, Execution, Deployment | Debugger.

  2. From the options on the right, you can edit the general debugger settings, or scroll down to the Scala section and adjust the Scala-specific debugger settings.

    the Debugger settings: Scala

    If you need to disable Smart step into for the Scala code, select Debugger | Stepping and disable the Always do smart step into option in the Scala section.

    the Stepping settings

Debug Scala code using sbt shell

  1. Open your sbt project.

  2. Open your application in the editor.

  3. In the editor gutter, set your breakpoints for the lines of code you want to debug.

    Set a breakpoint

    You cannot debug code defined in actual .sbt files, but you can debug code in Scala files that can be invoked from build.sbt.

  4. In the Settings dialog (Ctrl+Alt+S) , select Build, Execution, Deployment | Build Tools | sbt.

  5. On the sbt page, select the Enable debugging and click OK to save the changes.

  6. In the sbt shell tool window, click the icon to connect to the debugger server and start a debugging session.`

    IntelliJ IDEA also creates a run/debug configuration for the debugging session.

    Run/Debug Configuration

    You can edit the run/debug configuration settings, but the default settings should be enough to successfully debug your code.

  7. In the sbt shell enter the run command to run the program.

    sbt shell: run

    Evaluate your results in the Debug tool window.

    Debug tool window

Shortcuts for running and debugging

The regular actions for run/debug are:

Action

Shortcut

Run

Shift+F10

Debug

Shift+F9

When in the debug mode, you can evaluate any expression by using the Evaluate expression tool Alt+F8. This tool provides code completion in the same way as in the editor, so it's easy to enter any expression.

Sometimes, you may want to step into a particular method, but not the first one, which will be invoked. In this case, use Smart step into by pressing Shift+F7 to choose a particular method.

Action

Shortcut

Toggle breakpoint

Ctrl+F8

Step into

F7

Smart step into

Shift+F7

Step over

F8

Step out

Shift+F8

Resume

F9

Evaluate expression

Alt+F8

Test Scala applications

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

For information on how to debug code in Scala tests, refer to Debug code.

Run test

Test Scala applications using Scala Test

  1. Open your project and configure dependencies:

    • If you have an sbt project, open the build.sbt file and specify the following dependencies for ScalaTest:

      libraryDependencies += "org.scalactic" %% "scalactic" % "3.2.17"
      libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.17" % "test"

      Click icon in the sbt projects tool window to refresh your project or use the Reload project after changes in the build scripts option specified in the Build Tools 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 for which you want to create a test and place the caret within the line containing the class declaration.

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

    Alternatively, to create a test suite, right-click the class and from the context menu select Go to | 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 the test class and from the context menu select Run 'test name'.

    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 from the main menu.

  6. In the Run/Debug Configurations dialog, on the right side, specify settings for the test suite and click OK. The configuration has standard options, and you can find further details in the Run tests section.

    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.

  7. On the main toolbar, click the icon to run the test.

  8. 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:

  • 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 test scope. For example:

    import org.scalatest.GivenWhenThen import org.scalatest.featurespec.AnyFeatureSpec class StackFeatureSpec extends AnyFeatureSpec with GivenWhenThen { Feature("Feature 1") { Scenario("Scenario A") { } Scenario("Scenario B") { } } Feature("Feature 2") { Scenario("Scenario C") { } } Feature("empty") { } }
  2. In the editor, depending on your test scope, you can perform the following:

    • If you open the context menu for one of the tests inside the scope, you can create a run configuration for the specified test inside that scope.

      Run/Debug Configuration: 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.

      Run/Debug Configuration: scope
  3. Run your tests and view the output in the Run tool window.

    Run tool window: tests passed

    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 you want to run in the editor.

  3. In the gutter, click and select the Run 'name' with Coverage option.

    Alternatively, you go to Run | Run with Coverage in the main menu, to run your code with coverage.

    IntelliJ IDEA runs the test and displays the Coverage tool window with code coverage information.

Configure coverage in a run configuration

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. In the main menu, go to Run | Edit Configurations.

  2. In the Run/Debug Configurations dialog, click the 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, refer to Configure coverage

Configure coverage settings

You can also adjust code coverage behavior.

  1. Press Ctrl+Alt+S to open the IDE settings and then select >Build, Execution, Deployment | Coverage.

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

Last modified: 22 February 2024