Question

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 "Invalid Answer";

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I have modified the above code. Instead of taking input from the user, I have sent "answer" as an argument.You have to execute all the below files in Eclipse. First You need to create a "testing" package .In this package you have to create all the following files.

Essay.java

package testing;

public class Essay
{

    private String question;
    private String answer;

    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)
    {
           this.answer=userAnswer;
    }

    public String getAnswer()
    {

     

        if(answer.length() <=140)
        {

            return answer;

        }
        else
        {

            return "Invalid Answer";

        }

    }

}

questionTest.java

package testing;

import static org.junit.jupiter.api.Test;
import static org.junit.Assert.*;

public class questionTest {

   @Test
   void test()
   {
       Essay test1=new Essay("how are you?");
       String q=test1.getQuestionText();
       assertEquals("how are you?",q);
      
   }

}


answerTest.java

package testing;

import org.junit.jupiter.api.Test;
import static org.junit.Assert.*;

public class answerTest {

   @Test
   void test()
   {
       Essay test2=new Essay("How are you?");
       test2.answer("I am Fine!");
       String q1=test2.getAnswer();
       assertEquals("I am Fine!",q1);
   }

}

How to create files

JUnitTest RE System Library JavaSE-9] src testing answerTestjava Essay.java questionTest.java module-info.java

How to implement JUnit test case

module-info.java *Essay.java module-info.java answerTestjava questionTest.java 1 package testing; 2 3e import org.junit.jupit

Execution

G Package Exploreru JUnit Finished after 0.049 seconds Errors: 0 Failures: 0 Runs: 2/2

Add a comment
Know the answer?
Add Answer to:
Write one JUnit test in Java for the following: Essay class: import java.util.Scanner; public class Essay implements IAn...
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
  • In Java, write JUnit tests to verify various question-type objects for the following below: public interface...

    In Java, write JUnit tests to verify various question-type objects for the following below: public interface IAnswer {    public String getAnswer();    } import java.util.Scanner; YesNo class: public class YesNo implements IAnswer{            private String question;            public YesNo(String q){                this.question = q;            }                       //This function returns question text            public String getQuestionText() {                return question;...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • Can you help me with this code in Java??? import java.util.Scanner; public class Main { public...

    Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...

  • import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y,...

    import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...

  • I cannot get this to work in IntelliJ import java.util.Scanner; public class Fibonacci { public static...

    I cannot get this to work in IntelliJ import java.util.Scanner; public class Fibonacci { public static int fibonacci(int n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); System.out.println(fibonacci(n)); in.close(); } }

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

  • // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public...

    // please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...

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