Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Tomek Kaczanowski - Practical Unit Testing with JUnit and Mockito - 2013.pdf
Скачиваний:
224
Добавлен:
07.03.2016
Размер:
6.59 Mб
Скачать

Appendix B. Running Unit Tests

If you prefer typing, then first ensure that you have got the right test class in focus (that it is open in the editor and the cursor is there). To run all tests from this selected class, place the cursor somewhere outside any test method and press CTRL+SHIFT+F10. If you want to run a particular test method, then place the cursor on this method, and use the same combination: CTRL+SHIFT+F10.

In order to rerun the same test (or tests) press SHIFT+F10.

B.2.1. Debugging Tests with IntelliJ IDEA

As with running tests, you can run a debug session in more than just one way:

by right-clicking on a test class, tests package or test method and selecting the Debug ... option,

or by using the ALT+SHIFT+F9 keys.

To rerun the same debugging session press SHIFT+F10.

B.3. Running Tests with Gradle

In this section we will discuss running JUnit tests with Gradle. Gradle is a very powerful and complex build tool. Please refer to its documentation to learn about its various features.

Gradle recognizes JUnit tests out of the box; all that needs to be done is to add JUnit dependency for the testCompile scope (so the JUnit JAR is available in the classpath, to execute tests). The Listing B.1 listing shows a minimal build file - called build.gradle - that can be used to execute tests. It should be placed in the root of your application.

Listing B.1. Basic Gradle build script

apply plugin: 'java'

repositories { mavenCentral()

}

dependencies {

testCompile 'junit:junit:4.11'

}

Tells Gradle to prepare for the compilation of Java classes.

Defines the repository used to download dependencies (in this case it is the central repository of Maven, which contains JUnit JAR files).

Defines libraries used for the compilation (and execution) of tests (in this case junit-4.11.jar).

As you can see, Gradle uses a convention-over-configuration approach, so if you stick to the default values (by keeping code in src/main/java and tests in src/test/java, as we have done), then you will not need to specify them. That way build scripts are very concise.

To run the tests you will have to execute gradle test from the command line. You will see something similar to the following output:

273

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]