public class Coin {
public static final int PENNY = 1;
public static final int NICKEL = 5;
public static final int DIME = 10;
public static final int QUARTER = 25;
public static final int HALFDOLLAR = 50;
public static final int SILVERDOLLAR = 100;
public static final int INVALID = 0;
private int value;
public Coin( String coin ) throws
InvalidCoinException
{
String toUpperCoin =
coin.toUpperCase();
if ( toUpperCoin.equals( "PENNY" )
) value = PENNY;
else if ( toUpperCoin.equals(
"NICKEL" ) ) value = NICKEL;
else if ( toUpperCoin.equals(
"DIME" ) ) value = DIME;
else if ( toUpperCoin.equals(
"QUARTER" ) ) value = QUARTER;
else if ( toUpperCoin.equals(
"HALFDOLLAR" ) ) value = HALFDOLLAR;
else if ( toUpperCoin.equals(
"SILVERDOLLAR" ) ) value = SILVERDOLLAR;
else throw new
InvalidCoinException("Invalid coin!!");
}
public int getValue() {return value;}
public String toString() { return new
String(Integer.toString(value)); }
}
How to get test result using Junit for the
following:
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class CoinTest {
@Test
void testCoin() {
}
@Test
void testGetValue() {
}
@Test
void testToString() {
}
}
Here is the completed code for the JUnit test. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// CoinTest.java
import static org.junit.Assert.*;
import org.junit.Test;
public class CoinTest {
private Coin coin;
@Test
public void testCoin() {
try {
// testing Coin constructor by passing all possible valid coin
// names.
coin = new Coin("Penny");
// ensuring that Coin is initialized ith value=Coin.PENNY
assertEquals(coin.getValue(), Coin.PENNY);
// similarly testing other values
coin = new Coin("Nickel");
assertEquals(coin.getValue(), Coin.NICKEL);
coin = new Coin("Dime");
assertEquals(coin.getValue(), Coin.DIME);
coin = new Coin("Quarter");
assertEquals(coin.getValue(), Coin.QUARTER);
coin = new Coin("SILVERDOLLAR");
assertEquals(coin.getValue(), Coin.SILVERDOLLAR);
coin = new Coin("HALFDOLLAR");
assertEquals(coin.getValue(), Coin.HALFDOLLAR);
} catch (InvalidCoinException e) {
// if there is an exception, then something is not right
fail();
}
// now we try to pass an invalid value, to see if exception is thrown
try {
coin = new Coin("something");
// if no exception is thrown, it is an error
fail();
} catch (InvalidCoinException e) {
// if thrown, test passed.
}
}
@Test
public void testGetValue() {
// by testing constructor, we automatically test getValue() method. So
// this method might be redundant. Anyway I'm just testing to see even
// if we pass the coin name in any case, the resultant value is always
// expected ones.
try {
coin = new Coin("Penny");
assertEquals(coin.getValue(), Coin.PENNY);
coin = new Coin("PeNNy");
assertEquals(coin.getValue(), Coin.PENNY);
coin = new Coin("haLfDoLLar");
assertEquals(coin.getValue(), Coin.HALFDOLLAR);
} catch (InvalidCoinException e) {
fail();
}
}
@Test
public void testToString() {
try {
// initializing coin
coin = new Coin("Penny");
// ensuring that toString() method returns a String with value
// associated with the passed coin.
assertTrue(coin.toString().equals(Integer.toString(Coin.PENNY)));
coin = new Coin("Quarter");
assertTrue(coin.toString().equals(Integer.toString(Coin.QUARTER)));
coin = new Coin("Nickel");
assertTrue(coin.toString().equals(Integer.toString(Coin.NICKEL)));
} catch (InvalidCoinException e) {
fail();
}
}
}
public class Coin { public static final int PENNY = 1; public static final...