Question

In: Computer Science

You must change file HelloWorld.java in hello-java so that the tests all pass HelloWorld.java package hw;...

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());
  }
}

Solutions

Expert Solution

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());
    }
}


Related Solutions

You must write tests for the following: all tests must be written in JUnit 5. Customer...
You must write tests for the following: all tests must be written in JUnit 5. Customer Class Checking that the customer number autoincrements with each new customer object created Order Class Checking that the order number autoincrements with each new order object created Adding a product that is already in the order Removing a product that is in the order - quantity less than the quantity in the order Removing a product that is in the order - quantity that...
You need to complete the methods getSmallerValue, getLargerValue, compareTo, and equals. Must pass these tests: Test...
You need to complete the methods getSmallerValue, getLargerValue, compareTo, and equals. Must pass these tests: Test that each domino is smaller than every domino after it in the list (compareTo method). - Test that each domino is greater than every domino before it in the list (compareTo method). - Tests if three dominoes with the same values are equal (equal method). Code: public class Domino implements Comparable<Domino> { /** * The smallest possible value for a side of a domino....
The language is java package hw; public class MyLinkedList<E> { SLLNode<E> head = null; public MyLinkedList()...
The language is java package hw; public class MyLinkedList<E> { SLLNode<E> head = null; public MyLinkedList() {} // O(1) public MyLinkedList(E[] elements) { // O(elements.length) for(int i=elements.length-1;i>=0;i--) add(elements[i]); } public void printLinkedList() { // T(N) = O(N) System.out.print("printLinkedList(): "); SLLNode<E> node = head; while(node != null) { System.out.print(node.info + " "); node = node.next; // move to the next node } System.out.println(); } public void add(E e) { // T(N) = O(1) SLLNode<E> newNode = new SLLNode<E>(e); newNode.next = head;...
This code must be written in Java. This code also needs to include a package and...
This code must be written in Java. This code also needs to include a package and a public static void main(String[] args) Write a program named DayOfWeek that computes the day of the week for any date entered by the user. The user will be prompted to enter a month, day, and year. The program will then display the day of the week (Sunday..Saturday). The following example shows what the user will see on the screen: This program calculates the...
Hello, this is a TAX scenario for a class on pass-through entities. Please show all calculations...
Hello, this is a TAX scenario for a class on pass-through entities. Please show all calculations and work for each question (a-e listed below): KEY INFORMATION: John, Karl, and Laura form an entity under state law to be known as JKL. The entity elects to be taxed as a partnership. The three owners make the following initial contributions: John contributes $26,000 in cash and agrees to do all of the accounting work required by the partnership for 3 full years....
Modify HW#1. C++ use,Write a multithreaded program that tests your solution to HW#1. You will create...
Modify HW#1. C++ use,Write a multithreaded program that tests your solution to HW#1. You will create several threads – for example, 100 – and each thread will request a pid, sleep for a random period of time, and then release the pid. (Sleeping for a random period approximates the typical pid usage in which a pid is assigned to a new process, the process executes and terminates, and the pid is released on the process’ termination).On UNIX and Linux systems,...
CASH CONVERSION CYCLE - HELLO, THIS IS ALL ONE QUESTION, THANK YOU SO MUCH IN ADVANCE!...
CASH CONVERSION CYCLE - HELLO, THIS IS ALL ONE QUESTION, THANK YOU SO MUCH IN ADVANCE! Parramore Corp has $20 million of sales, $3 million of inventories, $4 million of receivables, and $2 million of payables. Its cost of goods sold is 85% of sales, and it finances working capital with bank loans at an 6% rate. Assume 365 days in year for your calculations. Do not round intermediate steps. What is Parramore's cash conversion cycle (CCC)? Do not round...
ou must write tests for the following: You must write tests for the following (which may...
ou must write tests for the following: You must write tests for the following (which may include Custom Exceptions): BankAccount Class Tests are written such that any deposit that is made greater than 10000 is not accepted. Tests are written such that balance in the BankAccount does not go below 0. Care should be taken for above tests at the time of Initial Deposit and at the time of Withdrawal and future Deposits. Tests should be written such that the...
Hello I have a question about Java. example So when you put “sum 1 2 3”...
Hello I have a question about Java. example So when you put “sum 1 2 3” in console, you do not separate those. Need a space between sum 1, 2 in the console. What is Sum? sum 1 2 3 6 (answer) sum 123456 21 sum 100 2000 3000 5100 sum 20 50 40 1 111 public static void main (String[] args) { System.out.Println(“What is Sum?”); String a=“”; a = scnr.nextLine(); String[] b = a.split(“”); if(b[0].equals(“sum”) { } Please do...
a. all Hypothesis Tests must include all four steps, clearly labeled; b. all Confidence Intervals must...
a. all Hypothesis Tests must include all four steps, clearly labeled; b. all Confidence Intervals must include all output as well as the CI itself c. include which calculator function you used for each problem. 2. An experiment was done to see whether open-book tests make a difference. A calculus class of 48 students agreed to be randomly assigned by the draw of cards to take a quiz either by open-notes or closed-notes. The quiz consisted of 30 integration problems...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT