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.
note
To adjust configuration settings when you perform running or debugging processes, use the Run/Debug Configurations dialog. To access the dialog, select Run | Edit Configurations from the main menu.
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.
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 ShiftF10 to execute the application. Alternatively, in the editor gutter, click the icon and select Run 'name'.
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 sbt shell on the status bar) and run sbt commands directly from it including running your application.
In the sbt projects tool window, click the 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.
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.
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.
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.
Open your Scala application in the editor.
In the gutter, set your breakpoints for the lines of code you want to debug. For more information about breakpoints, refer to Breakpoints.
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.
Press ShiftF9.
Alternatively, on the project widget, click icon to start debugging.
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.
Press CtrlAlt0S to open settings and then select Build, Execution, Deployment | Debugger.
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 Debugger | Stepping and disable the Always do smart step into option in the Scala section.
Open your sbt project.
Open your application in the editor.
In the editor 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.
note
If forking is enabled for the
run
ortest
tasks, debugging the tasks via the sbt shell will not work.In the Settings dialog (CtrlAlt0S) , 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.
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.
tip
If you want to rewind while debugging, you can do it via the Drop Frame action. This is particularly helpful if you mistakenly stepped too far. This will not revert the global state of your application, but will at least let you revert to the previous stack frame.
tip
Any breakpoint can be quickly disabled by clicking on the gutter while holding Alt. To change breakpoint details, for example, conditions, press CtrlShiftF8.
The regular actions for run/debug are:
Action | Shortcut |
---|---|
Run | ShiftF10 |
Debug | ShiftF9 |
When in the debug mode, you can evaluate any expression by using the Evaluate expression tool AltF8. 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 ShiftF7 to choose a particular method.
Action | Shortcut |
---|---|
Toggle breakpoint | CtrlF8 |
Step into | F7 |
Smart step into | ShiftF7 |
Step over | F8 |
Step out | ShiftF8 |
Resume | F9 |
Evaluate expression | AltF8 |
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.
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 Sync 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.
Open a class for which you want to create a test and place the caret within the line containing the class declaration.
Press CtrlShift0T and select Create New Test.
Alternatively, to create a test suite, right-click the class and from the context menu select Go to | Test.
In the dialog that opens, specify your test settings and click OK.
Open the test in the editor, press CtrlShiftF10 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.
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.
On the main toolbar, click the icon to run the test.
Evaluate the results in the Run tool window.
tip
You can open the sbt shell, run your tests, and evaluate the results from the sbt shell using the
test
command.
tip
To create a new run/debug configuration for a test after opening the Run/Debug configurations dialog, click the in the upper-left conner and from the list that opens select ScalaTest or Specs2 to create either a ScalaTest run configuration or a Specs2 run configuration respectively.
tip
To edit the default templates for Scala test files, press CtrlAlt0S and from the options on the left, select Editor | File and Code Templates. From the options on the right, select the Code tab and adjust your template.
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.
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:
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 the context menu for one of the tests inside the scope, you can create a run configuration 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.
IntelliJ IDEA lets you run your test suite with code coverage.
Open your project.
Open the test you want to run in the editor.
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.
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:
In the main menu, go to Run | Edit Configurations.
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, refer to Code coverage
You can also adjust code coverage behavior.
Press CtrlAlt0S to open settings and then select >Build, Execution, Deployment | Coverage.
From the options on right, adjust the settings and click OK.
Thanks for your feedback!