Question

For this activity you will create unit tests using JUnit and some of the available features...

For this activity you will create unit tests using JUnit and some of the available features of JUnit adhering to the Arrange, Act, Assert (AAA) unit testing methodology.

Specifications:

  • Select two different parts of your course project to unit test.
  • You will create two separate test class files in JUnit in the appropriate area.
  • Each JUnit test class must include the following:
    • At least two @Test methods.
    • Appropriate assertions.
  • One or more JUnit test class must include the following:
    • @Before method.

I'm not getting how to create the tests. I'm attempting to create tests on the GroceryDriver Class and Queue Class

Original Code so far is found at : https://github.com/GiantPygmy/GroceryCalculator

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

JUnit test cases :-

1) make no chage in Grocery.java

2) Add test cases to Main.java as shown below

//----Main.java

//Main class
import org.junit.Assert;
import org.junit.*;

import static org.junit.Assert.*;

import java.util.Scanner;

public class Main {

//create a method to check validity of cost

static boolean validatecost(String cost) {

int cost1 =Integer.parseInt(cost);

if (cost1>0) {return true;}

else

return false;
}
//Junit test to check validatecost() function gives no error
@Test
public void testJUnitValidateCostPositive() {

assertTrue(validatecost("2"));

}
//Junit test to check validatecost() function gives that error
@Test
public void testJUnitValidateCostNegative() {

assertTrue(validatecost("0"));

}

//create a method to check validity of quantity

static boolean validatequantity(String quantity) {

int quant =Integer.parseInt(quantity);

if (quant>=1) {return true;}

else

return false;
}

//Junit test to check validatecost() function gives no error
@Test
public void testJUnitValidateQuantityPositive() {

assertTrue(validatequantity("2"));

}
//Junit test to check validatecost() function gives that error
@Test
public void testJUnitValidateQuantityNegative() {

assertTrue(validatequantity("0"));

}

//JUnit test case to test Constructor of Grocery

@Test
public void testGroceryConstructor() {

/// arrange
Grocery obj;

///act
obj=new Grocery("mango",10,50);

///assert
assertEquals(obj.getName(),"mango");
assertTrue(obj.getQuantity()==10);
assertTrue(obj.getCost()==50);

}

//JUnit test case to test set/get methods of Grocery

@Test
public void testGrocerySetMethods() {

/// arrange
Grocery obj=new Grocery();

///act
obj.setName("mango");
obj.setCost(50);
obj.setQuantity(10);


///assert
assertEquals(obj.getName(),"mango");
assertTrue(obj.getQuantity()==10);
assertTrue(obj.getCost()==50);


}

/*

IF above test cases works fine then there is no need to test below code as it consists of the same (unit) functions

*/


//create a method to prompt user input

static String[] promptinput() {

System.out.println("Enter the choice 1 if you need to input");

Scanner sc = new Scanner(System.in);

int choice = sc.nextInt();

String[] input = new String[100];

String tempinput;

//takes input when choice =1

if(choice==1) {

int i=0;

Scanner sc1= new Scanner(System.in);

do{ System.out.println("Enter the grocery name,quantity and cost in that order and press -1 when done");

input[i] = sc.next();

tempinput = input[i];

i++;

}

while(!tempinput.contentEquals("-1"));

choice=0;

}

else {System.out.println("You do not wish to enter input.Exiting");}

return input;

} ///promptUser

//declare main method

public static void main(String[] args) {

// TODO Auto-generated method stub

String[] input = new String[100];

System.out.println("Enter the First grocery details");

input = promptinput();

if(validatecost(input[1])) {

if(validatequantity(input[2])) {

int quant =Integer.parseInt(input[1]);

int cost =Integer.parseInt(input[2]);

MyClass g0 = new MyClass( input[0], quant,cost);


System.out.println("The name:" + g0.getName().toString() + " quantity: " + g0.getQuantity() + " cost: " + g0.getCost());

}

}

else {System.out.println("Wrong input entered");}

System.out.println("Enter the Second grocery details");

input = promptinput();

if(validatecost(input[1])) {

if(validatequantity(input[2])) {

int quant =Integer.parseInt(input[1]);

int cost =Integer.parseInt(input[2]);

MyClass g1 = new MyClass( input[0], quant,cost);

System.out.println("The name:" + g1.getName().toString() + " quantity: " + g1.getQuantity() + " cost: " + g1.getCost());

}

}

else {System.out.println("Wrong input entered");}

System.out.println("Enter the Third grocery details");

input = promptinput();

if(validatecost(input[2])) {

if(validatequantity(input[1])) {

int quant =Integer.parseInt(input[1]);

int cost =Integer.parseInt(input[2]);

MyClass g2 = new MyClass( input[0], quant,cost);

System.out.println("The name:" + g2.getName().toString() + " quantity: " + g2.getQuantity() + " cost: " + g2.getCost());

}

}

else {System.out.println("Wrong input entered");}

}

}

Add a comment
Know the answer?
Add Answer to:
For this activity you will create unit tests using JUnit and some of the available features...
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
  • For this activity you will create unit tests using JUnit and some of the available features...

    For this activity you will create unit tests using JUnit and some of the available features of JUnit adhering to the Arrange, Act, Assert (AAA) unit testing methodology. Specifications: Select two different parts of your course project to unit test. You will create two separate test class files in JUnit in the appropriate area. Each JUnit test class must include the following: At least two @Test methods. Appropriate assertions. One or more JUnit test class must include the following: @Before...

  • This assignment is designed to give you practice with Javadoc, creating unit tests and using Junit....

    This assignment is designed to give you practice with Javadoc, creating unit tests and using Junit. Be sure to follow all style and documentation requirements for THIS class. See the style and documentation requirements posted on Canvas. You are to name your package assign1 and your file Palindrome.java. Palindrome Class Palindrome testString : String + Palindrome (String) + isPalindrome (): boolean The method isPalindrome is to determine if a string is a palindrome. A palindrome, for this assignment, is defined...

  • The role of the task at position G is to: (a) execute the unit tests using the JUnit jar. (b) build the solution by com...

    The role of the task at position G is to: (a) execute the unit tests using the JUnit jar. (b) build the solution by compiling the Java source files for the application. (c) create an archive of documentation for the system. (d) create a deployable archive containing the executable classes and information about the project. (e) None of the above. QUESTION 1 Suppose that you are given the following Ant build file. く?xml version= "1.0" encoding= " utf-8"?> <projet name-"exercise"...

  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

  • Description Create a polynomial class with linked lists and implement some basic functions. Specifications Polynomials are...

    Description Create a polynomial class with linked lists and implement some basic functions. Specifications Polynomials are stored in a linked list of Term objects (two classes: Polynomial and Term). Term objects have two data fields: coefficient and exponent, both positive integers. Polynomial will have some methods that create linked list functionality, but not all list operations are needed; include only those used by this program. Do not use a separate linked list class. The list will be non-circular. Make a...

  • AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static...

    AQueue.java class AQueue implements Queue { private E queueArray[]; // Array holding queue elements private static final int DEFAULT_SIZE = 10; private int maxSize; // Maximum size of queue private int front; // Index of front element private int rear; // Index of rear element // Constructors @SuppressWarnings("unchecked") // Generic array allocation AQueue(int size) { //BUG #1: maxSize = size maxSize = size+1; // One extra space is allocated rear = 0; front = 1; queueArray = (E[])new Object[maxSize]; //...

  • Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression...

    Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format The following operators are required to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation,...

  • Must be done in C# please! Thanks! In this assignment you will create a program named...

    Must be done in C# please! Thanks! In this assignment you will create a program named Test Scores that displays the average of all tests; average midterm score; average final score; and displays the midterm or final score of a particular student. Create a class level two dimensional integer array named Grades initialized with the following data: 897 242 301 987 116 450 865 128 992 109 88 75 94 70 90 87 81 79 97 79 78 80 92...

  • Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment...

    Purpose: Demonstrate the ability to create and manipulate classes, data members, and member functions. This assignment also aims at creating a C++ project to handle multiple files (one header file and two .cpp files) at the same time. Remember to follow documentation and variable name guidelines. Create a C++ project to implement a simplified banking system. Your bank is small, so it can have a maximum of 100 accounts. Use an array of pointers to objects for this. However, your...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

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