Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions tutorials/intellijJUnit.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,48 @@ This tutorial assumes you are using Gradle to manage JUnit dependencies.

<!-- ======================================================== -->

## Setting Up JUnit with Gradle
<include src="junit.md#add-junit-to-gradle" />

#### Add Gradle Support

If your project doesn’t yet use Gradle, follow the [Gradle Tutorial](https://se-education.org/guides/tutorials/gradle.html) to set it up.

#### Add JUnit as a Dependency

1. Open the `build.gradle` file in your project’s root directory.
<box type="tip" seamless>

Press {{ show_keys("<kbd>Ctrl</kbd> <kbd>⇧Shift</kbd> <kbd>O</kbd>", "<kbd>⌘Cmd</kbd> <kbd>⇧Shift</kbd> <kbd>O</kbd>") }} and search for `build.gradle` to locate it quickly.
</box>
1. Follow the instructions [here](https://se-education.org/guides/tutorials/junit.html#configuring-gradle-for-junit) to add the JUnit dependency.
1. After editing `build.gradle`, refresh your Gradle project:
After editing `build.gradle`, refresh your Gradle project:
* Press {{ show_keys("<kbd>Ctrl</kbd> <kbd>⇧Shift</kbd> <kbd>I</kbd>", "<kbd>⌘Cmd</kbd> <kbd>⇧Shift</kbd> <kbd>I</kbd>") }}. {texts="['3.1)', '3.2)']"}
* In the Gradle tool window, click **Reimport All Gradle Projects**.

<!-- ======================================================== -->

## Writing Tests
<include src="junit.md#junit-conventions" />

<include src="junit.md#first-unit-test" />

You can write test classes manually by following [this guide](https://se-education.org/guides/tutorials/vscJUnitTesting.html#conventions-to-follow). Alternatively, you can let IntelliJ generate test templates for you:
<box type="tip" seamless>

1. Place your cursor on the class name you want to test.
1. Press {{ show_keys("<kbd>Alt</kbd> <kbd>Enter</kbd>", "<kbd>Option</kbd> <kbd>Enter</kbd>") }} or right-click and choose `Show Context Actions`.
1. Select `Create Test` from the menu.
![Create Test option in menu](./images/intellij/create-test.png)
1. Choose the methods to generate tests for.<br>
<pic src="./images/intellij/test-menu.png" width="450" />
You may also use IntelliJ to generate test templates
1. Place your cursor on the class name you want to test.
2. Press {{ show_keys("<kbd>Alt</kbd> <kbd>Enter</kbd>", "<kbd>Option</kbd> <kbd>Enter</kbd>") }} or **right-click** and choose `Show Context Actions`.
3. Select **`Create Test`** from the menu.
![Create Test option in menu](./images/intellij/create-test.png)
4. Choose the methods to generate tests for.
<pic src="./images/intellij/test-menu.png" width="450" />

IntelliJ will automatically create a test class and navigate you to it.
IntelliJ will automatically create the test class and navigate you to it.
</box>

<!-- ======================================================== -->

## Running Tests

You may refer to the [JUnit tutorial](https://se-education.org/guides/tutorials/junit.html#running-tests) for how to run your tests.
* To run a specific JUnit test class (e.g., `src/test/java/seedu/DukeTest.java`), right-click on the test class, and choose {{ play_button }} `Run {classname}`.

<box type="tip" seamless>

If the above doesn't work, you may want to go to `File` > `Settings` and change the`Run tests using:` setting to `Intellij IDEA` (instead of `Gradle`), as shown below:

<panel header="Expand to see screenshot ..." peek no-close no-switch>

![change Intellij settings to not use Gradle](images/gradle/intellijRunTestsUsingIntellij.png)
</panel>
</box>

* To run all tests in a folder (e.g., `src/test/java` folder), right-click on the folder, and choose {{ play_button }} `Run Tests in '...'`.

After running tests, the results will be shown in the `Run` tool window.<br>
![Run Tool menu](./images/intellij/run-menu.png)
Expand Down Expand Up @@ -90,4 +94,4 @@ Refer to [IntelliJ documentation](https://www.jetbrains.com/help/idea/creating-a

---

**Contributors**: Song Yuexi ([@YosieSYX](https://github.com/YosieSYX))
**Contributors**: Song Yuexi ([@YosieSYX](https://github.com/YosieSYX))
16 changes: 4 additions & 12 deletions tutorials/junit.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,14 @@ public class DukeTest {

{% set play_button = '<span class="text-success">:fas-play:</span>' %}

****In Intellij IDEA:****
****Using IDE:****

* To run a specific JUnit test class (e.g., `src/test/java/seedu/DukeTest.java`), right-click on the test class, and choose {{ play_button }} `Run {classname}`.

<box type="tip" seamless>

If the above doesn't work, you may want to go to `File` > `Settings` and change the`Run tests using:` setting to `Intellij IDEA` (instead of `Gradle`), as shown below:
If you are using IntelliJ, you may refer to the tutorial [here](https://se-education.org/guides/tutorials/intellijJUnit.html#running-tests)

<panel header="Expand to see screenshot ..." peek no-close no-switch>

![change Intellij settings to not use Gradle](images/gradle/intellijRunTestsUsingIntellij.png)
</panel>
</box>
If you are using VS Code, you may refer to the tutorial [here](https://se-education.org/guides/tutorials/vscJUnitTesting.html#running-tests)

* To run all tests in a folder (e.g., `src/test/java` folder), right-click on the folder, and choose {{ play_button }} `Run Tests in '...'`.
* Other supported IDEs (e.g., Eclipse, NetBeans, VS Code, etc.) have similar mechanisms.
Other supported IDEs (e.g., Eclipse, NetBeans, etc.) have similar mechanisms.

****Using Gradle:****

Expand Down
23 changes: 13 additions & 10 deletions tutorials/vscJUnitTesting.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ This tutorial covers the basics of using VS Code’s JUnit.

<include src="vscCreatingNewJavaProject.md#vsc-java-prereq" />

<include src="junit.md#junit-use-gradle" />

<!-- ======================================================== -->

This tutorial assumes you are using Gradle to manage JUnit.

<include src="junit.md#junit-use-gradle" />


<!-- ======================================================== -->

<include src="junit.md#add-junit-to-gradle" />
Expand All @@ -31,33 +35,32 @@ Restart VSCode after updating the `build.gradle` file, to ensure the changes tak

<!-- ======================================================== -->

<include src="junit.md#junit-conventions" />

<!-- ======================================================== -->
<include src="junit.md#junit-conventions" />

<include src="junit.md#first-unit-test" />

<box type="tip" seamless>
<box type="tip" seamless>
You may also use VS Code’s Java extension to generate test skeletons

- Right-click in your Java file, select "Source Action…", then choose "Generate Tests…".

**VS Code Java extension helps generate test method skeletons.** Right-click in your Java file and click on `Source Action...`, then choose `Generate Tests...`.
- If triggered from a main class file: VS Code prompts you to select the methods to test, then generates skeleton test methods for you to complete.

* If triggered from a main class file, VS Code prompts you to choose methods to test. It generates skeleton test methods which you can fill in.
* If triggered from a test file, it offers options to choose the types of test methods to add, including lifecycle methods and standard test methods.
- If triggered from an existing test file**: VS Code offers options to add various types of test methods, including lifecycle methods and standard test methods.
</box>

<!-- ======================================================== -->

## Running Tests

****Using VS Code:****

* Use the **green play button** next to the class or method to run tests.
![VS Code Run Tests](images/vscJUnit/vscRunTest.png)
* The Testing Explorer is a tree view to show all the test cases in your workspace.
Open the Testing Explorer by clicking the beaker icon in the left Activity Bar. You can run/debug tests and view their results from there as well.
![VS Code Testing Explorer](images/vscJUnit/vscTestingExplorer.png)

<!-- ======================================================== -->


## Checking Test Coverage

Expand Down