Question

Junit testing case Here are my methods I want to test. Scanner scan = new Scanner(System.in).useDelimiter("\n");...

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);
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

Add a comment
Know the answer?
Add Answer to:
Junit testing case Here are my methods I want to test. Scanner scan = new Scanner(System.in).useDelimiter("\n");...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How do I test this with JUnit? I watched a video on YouTube about how to...

    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)...

  • This is my code for a TicTacToe game. However, I don't know how to write JUnit...

    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...

  • Write one JUnit test in Java for the following: Essay class: import java.util.Scanner; public class Essay implements IAn...

    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...

  • public static void main(String[] args) { int option=0;    while(option<8){ Scanner input=new Scanner(System.in); System.out.println("Welcome! Please enter...

    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;...

  • Hey I need help on Java. I have to create a while loop that reads 5 fields in each row, nextInt, ...

    Hey I need help on Java. I have to create a while loop that reads 5 fields in each row, nextInt, nextInt, next, next, and nextLine. Then I have to instantiate a User object using those 5 fields and insert that object in the usersArr. I just need to see what this would look like if a text file with 5 fields for 5 users existed. Here's my code so far: int [] usersArr = new int[200];           System.out.print("Enter file name:...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    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...

  • I have a little problem about my code. when i'm working on "toString method" in "Course"...

    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;    }   ...

  • package bigIntegerPackage; import java.util.Scanner; public class BigIntMathTesterOne {    private static Scanner keyboard = new Scanner(System.in);...

    package bigIntegerPackage; import java.util.Scanner; public class BigIntMathTesterOne {    private static Scanner keyboard = new Scanner(System.in);    /* *    * @param args    */    public static void main(String[] args)    { /** * Sample valid input and resulting output *    "44444444445555555555666666666677777777770000000000" * stores ["4","4","4","4","4","4","4","4","4","4","5","5","5","5","5","5","5","5","5","5","6","6","6","6","6","6","6'<'6","6","6","7","7","7","7","7","7","7","7","7","7","0","0","0","0","0","0","0","0","0","0"] in ArrayList and sets the value to positive *returns string "44444444445555555555666666666677777777770000000000" * "100000" stores ["1","0","0","0","0","0"] in ArrayList and sets the value to positive *returns string "100000" *"+0" stores ["0"] in ArrayList and sets...

  • Hey Guys , I need help with this assignment: This component of the final exam will...

    Hey Guys , I need help with this assignment: This component of the final exam will have two data structures. The two data structures will be a stack and the other will be a hashmap. The hashmap object will be added to the stack every time the user picks an answer. The hashmap will have a key and value. The key will be the timestamp (system time when the user selected a choice) and the value will be the choice...

  • This is what I have so far. I'm getting an error on the ... case 3:...

    This is what I have so far. I'm getting an error on the ... case 3: System.out.println("Enter the rank of the card you want removed"); cards.remove(); System.out.println(); break; -------------------------------------- package PlayingCards; import java.util.Scanner; public class Driver { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean done = false; int menuInput; DeckOfCards cards = new DeckOfCards(); do { System.out.println("Enter the number of one of the choices below:"); System.out.println("1: Shuffle The Deck."); System.out.println("2: Print The Cards Remaining In...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT