|
| 1 | +# Getting Started |
| 2 | + |
| 3 | +These exercises lean on Test-Driven Development (TDD), but they're not an exact match. |
| 4 | + |
| 5 | +The following steps assume that you are in the base directory of the exercise. |
| 6 | + |
| 7 | +You must have `gradle` installed. |
| 8 | + |
| 9 | +## Step 1 |
| 10 | + |
| 11 | +Run the test suite. It's written using the JUnit framework, and can be run with gradle: |
| 12 | + |
| 13 | +```sh |
| 14 | +$ gradle test |
| 15 | +``` |
| 16 | + |
| 17 | +This will fail, complaining that there is no method called `hello` for the `HelloWorld` class. |
| 18 | + |
| 19 | +To fix the error, open `src/main/java/HelloWorld.java` and add a `hello` method that returns a `String`. |
| 20 | +The method can return an empty string `""` for now: |
| 21 | + |
| 22 | +```java |
| 23 | +public class HelloWorld { |
| 24 | + public static String hello() { |
| 25 | + return ""; |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +## Step 2 |
| 31 | + |
| 32 | +Run the tests again. It will give you a new error, since now the method exists, but the tests are expects the |
| 33 | +method to accept one argument as input, a name. |
| 34 | + |
| 35 | +The errors will look something like: |
| 36 | + |
| 37 | +``` |
| 38 | +$ gradle test |
| 39 | +:compileJava |
| 40 | +:processResources UP-TO-DATE |
| 41 | +:classes |
| 42 | +:compileTestJava |
| 43 | +/exercism/exercises/java/hello-world/src/test/java/HelloWorldTest.java:9: error: method hello in class HelloWorld cannot be applied to given types; |
| 44 | + assertEquals("Hello, World!", HelloWorld.hello("")); |
| 45 | + ^ |
| 46 | + required: no arguments |
| 47 | + found: String |
| 48 | + reason: actual and formal argument lists differ in length |
| 49 | +
|
| 50 | + ... |
| 51 | +
|
| 52 | +4 errors |
| 53 | +:compileTestJava FAILED |
| 54 | +
|
| 55 | +FAILURE: Build failed with an exception. |
| 56 | +
|
| 57 | +* What went wrong: |
| 58 | +Execution failed for task ':compileTestJava'. |
| 59 | +> Compilation failed; see the compiler error output for details. |
| 60 | +``` |
| 61 | + |
| 62 | +### Fixing the Error |
| 63 | + |
| 64 | +To fix it, open `src/main/java/HelloWorld.java` and add a `name` to the method the signature: |
| 65 | + |
| 66 | +```java |
| 67 | +public class HelloWorld { |
| 68 | + public static String hello(String name) { |
| 69 | + return ""; |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +Now the `hello` method accepts a `name` as an input. |
| 75 | + |
| 76 | +### Understanding Test Failures |
| 77 | + |
| 78 | +Test failures will either be compilation errors or test failures. So far, we've been dealing with compilation |
| 79 | +errors. Once our test and main code successful compile, `gradle` will run the actual test suite. |
| 80 | + |
| 81 | +## Step 3 |
| 82 | + |
| 83 | +Run the tests again. You'll notice that the gradle output passes the `:compileTestJava` phase and fails |
| 84 | +at the `:test` phase. |
| 85 | + |
| 86 | +The errors will look something like: |
| 87 | + |
| 88 | +``` |
| 89 | +$ gradle test |
| 90 | +:compileJava |
| 91 | +:processResources UP-TO-DATE |
| 92 | +:classes |
| 93 | +:compileTestJava |
| 94 | +:processTestResources UP-TO-DATE |
| 95 | +:testClasses |
| 96 | +:test |
| 97 | +
|
| 98 | +HelloWorldTest > helloNoName FAILED |
| 99 | + org.junit.ComparisonFailure at HelloWorldTest.java:9 |
| 100 | +
|
| 101 | +... |
| 102 | +
|
| 103 | +3 tests completed, 3 failed |
| 104 | +:test FAILED |
| 105 | +``` |
| 106 | + |
| 107 | +These are actual test failures. It means that both our main and test java code is running but the test is |
| 108 | +expecting one outcome, but getting another. |
| 109 | + |
| 110 | +Open `src/test/java/HelloWorldTest.java` and go to line 9. |
| 111 | + |
| 112 | +```java |
| 113 | +assertEquals("Hello, World!", HelloWorld.hello("")); |
| 114 | +``` |
| 115 | + |
| 116 | +The test is expecting that `hello` returns "Hello, World!", when given an empty string (`""`) as input. |
| 117 | +The easiest way to make the test pass is to return `"Hello, World!"` instead of `"". |
| 118 | + |
| 119 | +```java |
| 120 | +public class HelloWorld { |
| 121 | + public static String hello(String name) { |
| 122 | + return "Hello, World!"; |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +## Wash, Rinse, Repeat |
| 128 | + |
| 129 | +Run the tests again. |
| 130 | + |
| 131 | +If it fails you're going to need to read the error message carefully to figure out what went wrong, fix the problem |
| 132 | +in the main code, and then try again until all the tests pass. |
| 133 | + |
| 134 | +``` |
| 135 | +$ gradle test |
| 136 | +:compileJava |
| 137 | +:processResources UP-TO-DATE |
| 138 | +:classes |
| 139 | +:compileTestJava |
| 140 | +:processTestResources UP-TO-DATE |
| 141 | +:testClasses |
| 142 | +:test |
| 143 | +
|
| 144 | +BUILD SUCCESSFUL |
| 145 | +``` |
| 146 | + |
| 147 | +## Submit |
| 148 | + |
| 149 | +When everything is passing, you can submit your code with the following command: |
| 150 | + |
| 151 | +``` |
| 152 | +$ exercism submit src/main/java/HelloWorld.java |
| 153 | +``` |
| 154 | + |
| 155 | + |
| 156 | + |
0 commit comments