IntelliJ IDEA 2018.1 Help

Keymap:

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.

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

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'.

You can also click the icon on the main toolbar to run your application.

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 projects tool window, click the 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. (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 () 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.

Run Scala code using Scala worksheet

IntelliJ IDEA lets you create a Scala worksheet and use it to evaluate your Scala code results in a special window of the Scala editor.

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

We recommended 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.

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.

Enter your code and press to see the results. If you select the Interactive Mode checkbox then the code results will be displayed automatically.

While working with the .sc file, you can perform the following actions:

You can clear the Make project checkbox to improve evaluation performance. In this case, the automatic checking of project's changes is disabled. The results appear in the view on the right side of your code.

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.

Debug Scala code

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 Using Breakpoints topic.

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.

Press Shift+F9. Alternatively, on the main toolbar, click the icon to start a debugging process.

Debug Scala code using sbt shell

If you have forking enabled for the run or test tasks, debugging the tasks via the sbt shell will not work.

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 sbt projects tool window, click the icon, to start the sbt shell.

In the sbt shell tool window, click the icon to connect to the debugger server and start 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.

Run your 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 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.

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 Run | Edit Configurations 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 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.

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

Evaluate the results in the Run tool window.

You can open the sbt shell, run your tests and evaluate the results from the sbt shell using the test command.

Alternatively, to create a test suite, right-click on the class and from the context menu select Go to | Test.

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.

To edit the default templates for Scala test files, press Ctrl+Alt+S 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.

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.

Testing 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 code. Check the following example:

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 Run | Edit Configurations.

In the Run/Debug Configurations dialog, click the icon from the options on the left.