Junit testing case
Here are my methods I want to test.
Scanner scan = new Scanner(System.in).useDelimiter("\n");
public String prompt_FirstName() {
System.out.println("First Name:");
String firstName = scan.next();
return firstName;
}
Here's what i have so far
@Test
public void testPrompt_first()
{
Menu inputOutput= new Menu();
String input = "Jane";
InputStream in = new ByteArrayInputStream(input.getBytes());
System.setIn(in);
String s = inputOutput.prompt_FirstName();
assertEquals("Jane", s);
}Note: Done accordingly. Please comment for any problem. Please Uprate. Thanks
Menu.java
import java.util.Scanner;
public class Menu {
Scanner scan = new
Scanner(System.in).useDelimiter("\n");
public String prompt_FirstName() {
System.out.println("First Name:");
String firstName = scan.next();
return firstName;
}
}
testCaseIO.java
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class testCaseIO {
//variable for input and print
Stream
private final InputStream systemIn
= System.in;
private final PrintStream systemOut = System.out;
//variables for ByteArrayInput
private ByteArrayInputStream testInputStream;
private ByteArrayOutputStream testOutputStream;
//setting up initial output
@Before
public void setUpOutput() {
testOutputStream = new ByteArrayOutputStream();
System.setOut(new
PrintStream(testOutputStream));
}
//getting automatic input
private void setForAutoInput(String data) {
testInputStream = new
ByteArrayInputStream(data.getBytes());
System.setIn(testInputStream);
}
//After execution restore previous input and
output
@After
public void restoreSystemInputOutput() {
System.setIn(systemIn);
System.setOut(systemOut);
}
//Testing for positive case
@Test
public void testCasePass() {
final String input = "Jane";
setForAutoInput(input);
Menu obj=new Menu();
String output=obj.prompt_FirstName();
assertEquals(input, output);
}
//testing for negative case
@Test
public void testCaseFail() {
final String input = "Jane";
setForAutoInput(input);
Menu obj=new Menu();
String output=obj.prompt_FirstName();
assertNotEquals(input, output+"no");
}
}
Output:

Junit testing case Here are my methods I want to test. Scanner scan = new Scanner(System.in).useDelimiter("\n");...
This is my code for a TicTacToe game. However, I don't know how to write JUnit tests. Please help me with my JUnit Tests. I will post below what I have so far. package cs145TicTacToe; import java.util.Scanner; /** * A multiplayer Tic Tac Toe game */ public class TicTacToe { private char currentPlayer; private char[][] board; private char winner; /** * Default constructor * Initializes the board to be 3 by 3 and...
How do I test this with JUnit? I watched a video on YouTube about how to create and run a Simple JUnit test in Eclipse IDE but I still don't understand. Here's what I need to test: public class ReverseDomainName { public static void main(String[] args) { String domain = "cs.princeton.edu"; System.out.println("domain is " + domain); System.out.println("reverse of domain is " + rev_domain(domain)); } public static String rev_domain(String domain)...
Write one JUnit test in Java for the following: Essay class: import java.util.Scanner; public class Essay implements IAnswer{ private String question; public Essay(String q){ this.question = q; } //This function returns question text public String getQuestionText() { return question; } //This function takes answer from user public void answer(String userAnswer) { // Take care of answer } @Override public String getAnswer() { System.out.println(question); Scanner scan = new Scanner(System.in); System.out.print("Answer: "); String ans =scan.nextLine(); scan.close(); if(ans.length() <=140){ return ans; }else{ return...
Need help in Java network, I'm trying to pass Junit 4 tests. my Code: public Character findIt( String url){ try { new URL("myurl").toURI(); System.out.println("pass"); } return null; } Test case: @Test public void testForValidOrNot(){ ValidOrNot validOrNot = new ValidOrNot(); assertEquals( new Character('pass'), validOrNot.findIt( "my url" ) ); Trying to see why...
Could you help me pleas , this is my code I want change it to insert student by user , and i have problem when i want append name it just one time i can't append or present more one. >>>>>>>>>>>>>>>>>>>. import java.util.Iterator; import java.util.Scanner; public class studentDLLTest { static int getChoice() { Scanner in = new Scanner(System.in); int choice; do { System.out.print("\nYour choice? : "); choice = in.nextInt(); } while (choice < 1 || choice > 9); return choice;...
public static void main(String[] args) { int option=0; while(option<8){ Scanner input=new Scanner(System.in); System.out.println("Welcome! Please enter your name"); String name=input.next(); Person user= new Person(name); System.out.println("Your id: "+user.getID()); System.out.println("Login successful"); System.out.println("------------------------------"); while(option!=7){ System.out.println("1.Create and host a new meeting"); System.out.println("2.Cancel a meeting"); System.out.println("3.Attend an existing meeting"); System.out.println("4.Leave a meeting"); System.out.println("5.Display my meetings"); System.out.println("6.Display meetings organized by me"); System.out.println("7.Logout"); System.out.println("8.Exit the app"); option=input.nextInt(); switch(option){ case 1: break; case 2: break; case 3: break; case 4: break; case 5: break; case 6: break;...
Java Help Please: I'm doing something wrong with my while loop. I want my code to check the phone number that is input to make sure it matches. If it doesn't I want it to print out Error! and I want my While loop to ask the user to try again by inputting another phone number. When the user puts in a phone number that matches I want the program to continue to output my tokens. When I run this...
I have a little problem about my code. when i'm working on "toString method" in "Course" class, it always say there is a mistake, but i can not fix it: Student class: public class Student { private String firstName; private String lastName; private String id; private boolean tuitionPaid; public Student(String firstName, String lastName, String id, boolean tuitionPaid) { this.firstName=firstName; this.lastName=lastName; this.id=id; this.tuitionPaid=tuitionPaid; } ...
Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...
Trying to of-else statement to sort 3 strings . import java.util.*; public class test { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String w1 = "", w2 ="", w3 = ""; String top = null, mid = null, bot = null; System.out.println("Please enter the first word"); w1 = scan.next(); System.out.println("Please enter the Second word"); w2 = scan.next(); System.out.println("Please enter...