Question
[JAVA] [JUnit] Hi I have a class that runs a puzzle I'd like to write a JUNIT test to determine if I get the correct
[JAVA]
[JUnit]
Hi I have a class that runs a puzzle I'd like to write a JUNIT test to determine if I get the correct output given an input. I am testing the sink method. I am currently failing my test as I'm not sure how to just test the sink method. First is my puzzle class I am testing and next is the test. I'd like for the sink method to give the "You've found the treasure!" result given a user entering an "A" into the console. I'm not sure how to set a user input and then test the output the program gives. Thanks in advance!
import java.awt.Point;
import java.util.Scanner;
//Whirlpool Puzzle Decorator Second Puzzle asks Questions gives result to console
public class Puzzle extends PlainWhirlpool {
public Puzzle(OceanMap oceanMap) {
super(oceanMap);
}
//Method for User Interaction Question
@Override
public void Sink() {
System.out.println("Okay Now Questions 2 for the Treasure ");
System.out.println("What grade will I get for this project?");
Scanner scanner = new Scanner(System.in);
String anwser = scanner.nextLine();
String secret = "A";
String secret2 = "A+";
String secret3 = "a";
String result1 = "You've Found The Treasure!";
String result2 = ":( Wrong Anwser Keep Playing";
if(anwser.equals(secret3) || anwser.equals(secret2) || anwser.equals(secret)) {
System.out.print(result1);
}else {
System.out.print(result2);
}
}
--Test--
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.junit.Test;
public class PuzzleTest {
@Test
public void PuzzleTest() {
OceanMap oceanMap;
int dimensions = 25;
int scale = 33;
oceanMap = OceanMap.getInstance(dimensions, scale);
Puzzle map = new Puzzle(oceanMap);
map.Sink();
String input = "A";
InputStream in = new ByteArrayInputStream(input.getBytes());
System.setIn(in);
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.out.print(outContent);
assertEquals(outContent.toString(), "You've Found The Treasure!");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started