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
Create or import a Scala project as you would normally create or import any other project in IntelliJ IDEA.
Open your application in the editor.
Press Shift+F10 to execute the application. Alternatively, in the left gutter of the editor, click the icon and select Run 'name'.
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.
Open your sbt project.
- 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.
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 sbr shell on the status bar) and run sbt commands directly from it including running your application. In the sbt projects tool window, click sbt Tasks node.
In the list that opens, select the run task that will run a main method. The result of execution is displayed in sbt shell tool window.
Debug Scala code
IntelliJ IDEA lets you debug your code using IntelliJ IDEA debugger or the sbt shell.
Debug Scala code using Intellij IDEA
Open your Scala application in the editor.
In the left gutter, set your breakpoints for the lines of code you want to debug. For more information on breakpoints, please see the Breakpoints topic.
If you need, you can access the Run/Debug configurations dialog (Run | Edit Configurations) and adjust the settings, but usually the default settings are enough to successfully start and complete your debugging session.
Press Shift+F9. Alternatively, on the main toolbar, click the icon to start a debugging process.
- Evaluate the results in the Debug tool window.
For information on how to use options in the Debug tool window, see 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
In the Settings/Preferences dialog Ctrl+Alt+S, go to .
- 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.
If you need to disable smart step into for the Scala code, select and disable the Always do smart step into option in the Scala section.
Debug Scala code using sbt shell
Open your sbt project.
Open your application in the editor.
- In the editor, in the left gutter, set your breakpoints for the lines of code you want to debug.
You cannot debug code defined in actual .sbt files, but you can debug code in Scala files that can be invoked from build.sbt.
In the Settings/Preferences dialog Ctrl+Alt+S, select Build, Execution, Deployment | Build Tools | sbt.
On the sbt page, select the Enable debugging and click OK to save the changes.
- 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.
If you need, you can edit the run/debug configuration settings, but the default settings should be enough to successfully debug your code. In the sbt shell enter the
run
command to run the program. Evaluate your results in the 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
- 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 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.
- If you have an sbt project, open the build.sbt file and specify the following dependencies for ScalaTest:
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.
Press Ctrl+Shift+T and select Create New Test.
In the dialog that opens, specify your test settings and click OK.
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'.
IntelliJ IDEA creates a run/debug configuration for the test automatically, but if you want to edit settings in your configuration, click
on the main menu.- 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 Run 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.
On the main toolbar, click the icon to run the test.
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.
Create your test scope. For example, check the following code:
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") { } }- 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.
If you open a context menu for the whole scope, you can create a run configuration for all tests inside the scope.
Run your tests and view the output in the Run tool window. 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.
Open your project.
Open the test in question in the editor.
- In the left gutter, click the 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:From the main menu, select
.In the Run/Debug Configurations dialog, click the icon from the options on the left.
From the list that opens, select the configuration you need.
From the options on right, click the Code Coverage tab.
Adjust the default settings and click OK.
For more information, please see Configure coverage
You can also adjust code coverage behavior.
Press Ctrl+Alt+S to open the Settings/Preferences page.
From the options on the left, select
.From the options on right, adjust the settings and click OK.