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

Appendix B. Running Unit Tests

There are many ways to run tests written with JUnit, as all the major tools support it out-of-the-box. This section describes how to run JUnit tests using the most popular IDEs and build tools: Eclipse, IntelliJ IDEA, Gradle and Maven.

The tools presented in this section are capable of much more than simply executing tests. Please consult their documentation to find out more.

As far as Gradle and Maven are concerned, I will assume that your project has a layout compatible with what was discussed in Section 3.1. The main requirement is that your production code resides in src/ main/java and test classes in src/test/java. Both Gradle and Maven treat this layout as the default. If you use a different directory structure then you will need to inform your build tool about it. Please refer to the appropriate documentation about how to do it.

One could ask why it is that we need to bother with build tools, if our powerful IDEs are capable of running tests. The thing is, the tests we write will be executed on the machines of other developers (who may be using different IDEs), and even on the IDE-free environment of a CI server. Having a build script capable of running tests is the first step in helping to overcome the "it works on my machine" syndrome.

This appendix concentrates on running tests on your local machine. However, bear in mind that every team should have a separate machine (a continuous integration server) dedicated to the building (compiling, testing, packaging, etc.) of the code it is developing. Continuous integration is well beyond the scope of this book, but there is plenty of information available on the Internet if you need it.

B.1. Running Tests with Eclipse

Eclipse supports JUnit out of the box. There is no need to install any plugins.

If you prefer to use a mouse, then right-click on the selected test class (e.g. MoneyTest) and choose Run As / JUnit Test. Likewise, to run a single test method, right-click it and choose Run As / JUnit Test.

Figure B.1. Running a single test with Eclipse

To run more tests right-click on, for example, src/test/java directory (or any package inside it which includes some tests), in project tree view, and choose the same option as previously: Run As / JUnit Test.

270

Appendix B. Running Unit Tests

Figure B.2. Running multiple tests with Eclipse

Ufortunately, for various reasons Eclipse works inconsistently when running multiple tests. Sometimes +Run As is not available for certain packages. What will help here is creating your own configuration (Run As / Run Configurations...). All you need to do is choose a directory (e.g. src/test/java) or a package (e.g. com.practicalunittesting) to run tests from.

Figure B.3. Running multiple tests with Eclipse - custom configuration

If you prefer typing, then first make sure you have the correct test class in focus (i.e. that it is open in the editor and the cursor is there, or that it is highlighted in Package Explorer). To run all tests from this selected class, place the cursor somewhere outside any test method and press SHIFT+ALT+X followed by T. If you want to run a particular test method, place the cursor on this method, and use the same combination: SHIFT+ALT+X followed by T.

At the time of writing there is a bug in Eclipse which means that the keyboard shortcut only works if you type the T key really fast (before the pop-up windows shows up). In the event of difficulties this link should give you some insight into this problem: https://bugs.eclipse.org/ bugs/show_bug.cgi?id=369860.

271

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