I was looking for some Java exercises to train and learn some algorithms when I came across the site exercism.io and I decided to do their exercises in Java. So far so good, I downloaded their app using chocolately and installed the gradle to run the tests. I found the methodological proposal of their exercises very interesting until the problem came.
The problem is as follows:
When you make the exercise request through cmd using the command:
$ exercism fetch java hello-world
The exercise already comes with bugs (intentionally) for you to find and solve them all. After the bugs are resolved, and running the command:
$ gradle test
Inside the exercise folder, everything should work fine, and then you're free to submit your answer using the command:
$ exercism submit src/main/java/HelloWorld.java
indicating the path of your code in the submission:
Now comes the problem.
The following code is used for testing:
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() {
assertEquals("Hello, Bob!", HelloWorld.hello("Bob"));
}
}
What I understood that this code should do is to get a string from the main program and test if the string is empty of type ""
or void
it should return: Hello, World! .
If it is written in the main program the name Alice or Bob it would return or Hello, Alice! or Hello, Bob!
My main program is as follows:
public class HelloWorld {
public static String hello(String name) {
return name;
}
}
I already tried to put the three valid strings concatenated, three returns in the method, I set up an array of strings and had them display one at a time and nothing.
The problem is that the exercise requirement calls for this to be resolved by editing the main program that is HelloWorld.java so this prevents me from changing the test program that is HelloWorldTest. java because that would be cheating.
The error that gradle
shows me is this one here:
PS C:\Users\anton\exercism\java\hello-world> gradle test
:compileJava
:processResources UP-TO-DATE
:classes
:compileTestJava
:processTestResources UP-TO-DATE
:testClasses
:test
HelloWorldTest > helloAnotherSampleName FAILED
org.junit.ComparisonFailure at HelloWorldTest.java:20
HelloWorldTest > helloSampleName FAILED
org.junit.ComparisonFailure at HelloWorldTest.java:15
HelloWorldTest > helloNoName FAILED
org.junit.ComparisonFailure at HelloWorldTest.java:9
3 tests completed, 3 failed
:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///C:/Users/anton/exercism/java/hello-world/build/reports/tests/inde
x.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 8.581 secs
If anyone knows how I solve this, send me a tip here.