Skip to content
Merged
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
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"repository": "https://github.com/exercism/xjava",
"active": true,
"problems": [
"hello-world",
"etl",
"nucleotide-count",
"word-count",
Expand Down
156 changes: 156 additions & 0 deletions hello-world/GETTING_STARTED.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# Getting Started

These exercises lean on Test-Driven Development (TDD), but they're not an exact match.

The following steps assume that you are in the base directory of the exercise.

You must have `gradle` installed.

## Step 1

Run the test suite. It's written using the JUnit framework, and can be run with gradle:

```sh
$ gradle test
```

This will fail, complaining that there is no method called `hello` for the `HelloWorld` class.

To fix the error, open `src/main/java/HelloWorld.java` and add a `hello` method that returns a `String`.
The method can return an empty string `""` for now:

```java
public class HelloWorld {
public static String hello() {
return "";
}
}
```

## Step 2

Run the tests again. It will give you a new error, since now the method exists, but the tests are expects the
method to accept one argument as input, a name.

The errors will look something like:

```
$ gradle test
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava
/exercism/exercises/java/hello-world/src/test/java/HelloWorldTest.java:9: error: method hello in class HelloWorld cannot be applied to given types;
assertEquals("Hello, World!", HelloWorld.hello(""));
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
...
4 errors
:compileTestJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileTestJava'.
> Compilation failed; see the compiler error output for details.
```

### Fixing the Error

To fix it, open `src/main/java/HelloWorld.java` and add a `name` to the method the signature:

```java
public class HelloWorld {
public static String hello(String name) {
return "";
}
}
```

Now the `hello` method accepts a `name` as an input.

### Understanding Test Failures

Test failures will either be compilation errors or test failures. So far, we've been dealing with compilation
errors. Once our test and main code successful compile, `gradle` will run the actual test suite.

## Step 3

Run the tests again. You'll notice that the gradle output passes the `:compileTestJava` phase and fails
at the `:test` phase.

The errors will look something like:

```
$ gradle test
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test
HelloWorldTest > helloNoName FAILED
org.junit.ComparisonFailure at HelloWorldTest.java:9
...
3 tests completed, 3 failed
:test FAILED
```

These are actual test failures. It means that both our main and test java code is running but the test is
expecting one outcome, but getting another.

Open `src/test/java/HelloWorldTest.java` and go to line 9.

```java
assertEquals("Hello, World!", HelloWorld.hello(""));
```

The test is expecting that `hello` returns "Hello, World!", when given an empty string (`""`) as input.
The easiest way to make the test pass is to return `"Hello, World!"` instead of `"".

```java
public class HelloWorld {
public static String hello(String name) {
return "Hello, World!";
}
}
```

## Wash, Rinse, Repeat

Run the tests again.

If it fails you're going to need to read the error message carefully to figure out what went wrong, fix the problem
in the main code, and then try again until all the tests pass.

```
$ gradle test
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test
BUILD SUCCESSFUL
```

## Submit

When everything is passing, you can submit your code with the following command:

```
$ exercism submit src/main/java/HelloWorld.java
```



12 changes: 12 additions & 0 deletions hello-world/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.10"
}

9 changes: 9 additions & 0 deletions hello-world/src/example/java/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class HelloWorld {
public static String hello(String name) {
if (name == null || name.isEmpty()) {
return "Hello, World!";
}

return String.format("Hello, %s!", name);
}
}
Empty file added hello-world/src/main/java/.keep
Empty file.
2 changes: 2 additions & 0 deletions hello-world/src/main/java/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class HelloWorld {
}
22 changes: 22 additions & 0 deletions hello-world/src/test/java/HelloWorldTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class HelloWorldTest {

@Test
public void helloNoName() {
assertEquals("Hello, World!", HelloWorld.hello(""));
assertEquals("Hello, World!", HelloWorld.hello(null));
}

@Test
public void helloSampleName() {
assertEquals("Hello, Alice!", HelloWorld.hello("Alice"));
}

@Test
public void helloAnotherSampleName() throws Exception {
assertEquals("Hello, Bob!", HelloWorld.hello("Bob"));
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ include 'etl'
include 'gigasecond'
include 'grade-school'
include 'hamming'
include 'hello-world'
include 'luhn'
include 'meetup'
include 'nucleotide-count'
Expand Down