In: Computer Science
You must change file HelloWorld.java in hello-java so that the tests all pass
HelloWorld.java
package hw;
public class HelloWorld {
public String getMessage() {
return "hello world";
}
public int getYear() {
return 2019;
}
}
Main.java
package hw;
import java.util.Arrays;
public class Main {
public static void main(final String[] args) {
System.out.println("args = " + Arrays.asList(args));
final HelloWorld instance = new HelloWorld();
System.out.println(instance.getMessage());
System.out.println(instance.getYear());
System.out.println("bye for now");
}
}
TestHelloWorld.java
package hw;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestHelloWorld {
private HelloWorld fixture;
@Before
public void setUp() throws Exception {
fixture = new HelloWorld();
}
@After
public void tearDown() throws Exception {
fixture = null;
}
@Test
public void getMessage() {
assertNotNull(fixture);
assertEquals("hello world", fixture.getMessage());
}
@Test
public void getMessage2() { // this test is broken - fix it!
assertNull(fixture);
assertEquals("hello world", fixture.getMessage());
}
@Test
public void getYear() { // this test is OK, fix HelloWorld.java to make it pass!
assertNotNull(fixture);
assertEquals(2019, fixture.getYear());
}
}
package hw;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestHelloWorld {
private HelloWorld fixture;
@Before
public void setUp() throws Exception {
fixture = new HelloWorld();
}
@After
public void tearDown() throws Exception {
fixture = null;
}
@Test
public void getMessage() {
assertNotNull(fixture);
assertEquals("hello world", fixture.getMessage());
}
@Test
public void getMessage2() { // this test is broken - fix it!
/*
This test is broken because fixture is not null here
because fixture is assigned an object of HelloWorld in setUp method
This can not possibly be fixed by changing HelloWorld class in any way
only way to fix is to by using assertNotNull instead of assertNull here
*/
assertNotNull(fixture);
assertEquals("hello world", fixture.getMessage());
}
@Test
public void getYear() { // this test is OK, fix HelloWorld.java to make it pass!
assertNotNull(fixture);
assertEquals(2019, fixture.getYear());
}
}
