SlideShare a Scribd company logo
I need help creating a parametized JUnit test case for the following class.
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import static java.lang.Integer.valueOf;
public class IntegerSet {
/**
* The only instance variable in the class. No other variables allowed.
*/
private List list;
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
/**
* Initializes the empty set.
*/
public IntegerSet() {
list = new LinkedList();
}
/**
* Initializes a set with the numbers in the argument.
*/
public IntegerSet(Integer[] integers) {
this();
if (integers == null) {
throw new NullPointerException("The argument cannot be null");
} else {
for (Integer i : integers) {
if (!exists(i)) {
this.insertElement(i);
}
}
}
Collections.sort(list);
}
/**
* Inserts an integer to the set if the integer does not exist in the set
*/
public void insertElement(int i) {
if (!list.contains(i)) {
list.add(i);
}
}
/**
* Inserts to the set all the integers in the argument which do not exist in
* the set.
*/
public void insertAll(Integer[] data) {
if (data == null) {
throw new NullPointerException("The argument cannot be null");
// throw new
// IllegalArgumentException("The argument cannot be null");
} else {
Arrays.sort(data);
for (Integer num : data) {
if (!exists(num)) {
insertElement(num);
}
}
}
}
/**
* Deletes an integer from the set if it exists in the set.
*/
public void deleteElement(int i) {
list.remove(i);
}
/**
* Deletes all the elements in the set.
*/
public void deleteAll() {
list.clear();
}
/**
* Returns true if the argument exists in the set, false otherwise.
*/
public boolean exists(int i) {
return list.contains(i);
}
public String toString() {
char delimiter = ' ';
String str = "[";
for (Integer i : list) {
str += i;
str += delimiter;
}
return str + "]";
}
/**
* Finds the union of two sets. A null pointer is considered an empty set.
*
* Return "new IntegerSet()" when the result is an empty set.
*/
public static IntegerSet union(IntegerSet set1, IntegerSet set2) {
IntegerSet union = new IntegerSet();
if (set1.isEmpty() && set2.isEmpty()) {
return union;
}
if (!set1.list.isEmpty()) {
for (Integer num : set1.list) {
if (!union.list.contains(num)) {
union.list.add(num);
}
}
}
if (!set2.isEmpty()) {
for (Integer num : set2.list) {
if (!union.list.contains(num)) {
union.list.add(num);
}
}
}
return union;
}
/**
* Finds the intersection of two sets. A null pointer is considered an empty
* set.
*
* Return "new IntegerSet()" when the result is an empty set.
*/
public static IntegerSet intersection(IntegerSet set1, IntegerSet set2) {
IntegerSet intersection = new IntegerSet();
// check for null pointers
if (set1.list.isEmpty() || set2.list.isEmpty()) {
return intersection;
}
for (Integer num : set1.list) {
if ((!intersection.list.contains(num)) && set2.list.contains(num)) {
intersection.list.add(num);
}
}
return intersection;
}
/**
* Builds an array with the elements in the set.
*/
public Integer[] toArray() {
Integer[] alist;
Object[] s = list.toArray();
alist = new Integer[s.length];
for (int i = 0; i < s.length; i++) {
alist[i] = valueOf((Integer) s[i]);
}
Arrays.sort(alist);
return alist;
}
}
I'm not very good with JUnit. Could I get some help with how I'm supposed to set up a test case
like this?
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class IntegerSetTest {
public IntegerSetTest() {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void test() {
fail("Not yet implemented");
}
@Parameters
public void testIntersectionWithNullInput() {
}
@Parameters
public void testExists() {
}
@Parameters
public void testIsEmpty() {
}
@Parameters
public void testUnion() {
}
@Parameters
public void testCreateSetFromArray() {
}
@Parameters
public void testCreateSetFromNull() {
}
@Parameters
public void testDeleteAll() {
}
@Parameters
public void testDeleteEntry(){
}
@Parameters
public void testINsertAll(){
}
@Parameters
public void testAllNull(){
}
@Parameters
public void testIntersection(){
}
@Parameters
public void testUnionWithNullInput(){
}
}
Solution
package com.game;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.Parameterized.Parameters;
@FixMethodOrder
public class IntegerSetTest {
public IntegerSetTest() {
}
@Test
public void testUnion() {
Integer[] int1 = {1,2};
IntegerSet set1 = new IntegerSet(int1);
Integer[] int2 = {3,4};
IntegerSet set2 = new IntegerSet(int2);
IntegerSet unionResult = new IntegerSet();
IntegerSet set = unionResult.union(set1, set2);
Assert.assertEquals("Union of two integer sets are equal",4, set.size());
}
@Test
public void testCreateSetFromArray() {
Integer[] int1 = {1,2};
IntegerSet set1 = new IntegerSet(int1);
Assert.assertEquals("Successfully created a set from Array",2,set1.size());
}
@Test
public void testCreateSetFromNull() {
try{
IntegerSet set1 = new IntegerSet(null);
}catch(Exception e){
Assert.assertEquals("Successfully got exception error needed","The argument cannot be
null",e.getMessage());
}
}
@Test
public void testDeleteAll() {
Integer[] int1 = {1,2};
IntegerSet set1 = new IntegerSet(int1);
set1.deleteAll();
Assert.assertEquals("Successfully deletes all entries from Set",0,set1.size());
}
@Parameters
public void testDeleteEntry(){
}
@Parameters
public void testINsertAll(){
Integer[] int1 = {1,2};
IntegerSet set1 = new IntegerSet();
set1.insertAll(int1);
Assert.assertEquals("Successfully inserts all entries from Set",2,set1.size());
}
}

More Related Content

PDF
Please create the appropriate JUnit test cases to thoroughly.pdf
PDF
I really need help with the code for this in Java.Set operations u.pdf
PDF
package ADTs public interface CollectionADTltTgt .pdf
PDF
Here is what I got so far, I dont know how to write union, interse.pdf
PDF
Complete the class ArraySet1java which implements the SetA.pdf
DOCX
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
PDF
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
PDF
Hello need help on this lab- what you need to do is add a code to Arra.pdf
Please create the appropriate JUnit test cases to thoroughly.pdf
I really need help with the code for this in Java.Set operations u.pdf
package ADTs public interface CollectionADTltTgt .pdf
Here is what I got so far, I dont know how to write union, interse.pdf
Complete the class ArraySet1java which implements the SetA.pdf
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdf

Similar to I need help creating a parametized JUnit test case for the following.pdf (15)

PDF
package lab7 public class SetOperations public static.pdf
PDF
Collections 1 Insert an element at desired location Take th.pdf
PDF
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
DOCX
Use the following data set that compares age to average years lef.docx
DOCX
Hey i have attached the required file for my assignment.and addi
PDF
Tools09 – Reusing & Composing Tests with Traits
PDF
Step 1 The Pair Class Many times in writing software we come across p.pdf
PDF
Google guava
PDF
JAVA DUMP SET.pdf
PDF
1 The goal is to implement DataStructuresArrayStack accor.pdf
DOCX
FakeArrayList first - new FakeArrayl_ist(3)- first-set(2- 7-1)- first-.docx
PDF
public class TrequeT extends AbstractListT { .pdf
DOCX
-The tester method is below- It should work with the tester below only.docx
PDF
import java.util.LinkedList; import java.util.Random; import jav.pdf
DOCX
ECET 370 Success Begins/Newtonhelp.com
package lab7 public class SetOperations public static.pdf
Collections 1 Insert an element at desired location Take th.pdf
Listings for BinaryHeap.java and BinaryHeapTest.java are shown in th.pdf
Use the following data set that compares age to average years lef.docx
Hey i have attached the required file for my assignment.and addi
Tools09 – Reusing & Composing Tests with Traits
Step 1 The Pair Class Many times in writing software we come across p.pdf
Google guava
JAVA DUMP SET.pdf
1 The goal is to implement DataStructuresArrayStack accor.pdf
FakeArrayList first - new FakeArrayl_ist(3)- first-set(2- 7-1)- first-.docx
public class TrequeT extends AbstractListT { .pdf
-The tester method is below- It should work with the tester below only.docx
import java.util.LinkedList; import java.util.Random; import jav.pdf
ECET 370 Success Begins/Newtonhelp.com

More from fonecomp (20)

PDF
write a program that prompts the user to enter the center of a recta.pdf
PDF
write 3 3 slide on China and Germany. Individual work (1) Choose a c.pdf
PDF
Why do negotiations fail O Conflicts are boring O Conflicts are co.pdf
PDF
What was the court-packing plan, and why is it sig- nificant to a.pdf
PDF
Who are the major stakeholders that Sony must consider when developi.pdf
PDF
What sort of prevention techniques would be useful when dealing with.pdf
PDF
What are the main three types of organizational buyers How are they.pdf
PDF
The following code, is a one player battleship game in JAVA. Im tryi.pdf
PDF
Sharks are able to maintain their fluids hypertonic to the ocean env.pdf
PDF
Quantum Bank Inc. is a regional bank with branches throughout the so.pdf
PDF
Neeb Corporation manufactures and sells a single product. The com.pdf
PDF
Describe the primary, secondary, and tertiary structure of DNASo.pdf
PDF
I need help with this program for java.The program you are given t.pdf
PDF
Harrison works in a cubicle at a window next to Karen Ravenwoods cu.pdf
PDF
Hi there I am having difficulty in finalizing my Tetris game , below.pdf
PDF
How do the genomes of Archaea and Bacteria compare Drag and drop th.pdf
PDF
General question How would the technology affect who lives and who .pdf
PDF
Describe how partial diploids can be produced in E. coli.Solutio.pdf
PDF
A single layer of gold atoms lies on a table. The radius of each gol.pdf
PDF
An erect object is 25 cm from a concave mirror of radius 30 cm. The .pdf
write a program that prompts the user to enter the center of a recta.pdf
write 3 3 slide on China and Germany. Individual work (1) Choose a c.pdf
Why do negotiations fail O Conflicts are boring O Conflicts are co.pdf
What was the court-packing plan, and why is it sig- nificant to a.pdf
Who are the major stakeholders that Sony must consider when developi.pdf
What sort of prevention techniques would be useful when dealing with.pdf
What are the main three types of organizational buyers How are they.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
Sharks are able to maintain their fluids hypertonic to the ocean env.pdf
Quantum Bank Inc. is a regional bank with branches throughout the so.pdf
Neeb Corporation manufactures and sells a single product. The com.pdf
Describe the primary, secondary, and tertiary structure of DNASo.pdf
I need help with this program for java.The program you are given t.pdf
Harrison works in a cubicle at a window next to Karen Ravenwoods cu.pdf
Hi there I am having difficulty in finalizing my Tetris game , below.pdf
How do the genomes of Archaea and Bacteria compare Drag and drop th.pdf
General question How would the technology affect who lives and who .pdf
Describe how partial diploids can be produced in E. coli.Solutio.pdf
A single layer of gold atoms lies on a table. The radius of each gol.pdf
An erect object is 25 cm from a concave mirror of radius 30 cm. The .pdf

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Lesson notes of climatology university.
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Institutional Correction lecture only . . .
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Complications of Minimal Access Surgery at WLH
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
master seminar digital applications in india
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Lesson notes of climatology university.
GDM (1) (1).pptx small presentation for students
Microbial diseases, their pathogenesis and prophylaxis
Pharma ospi slides which help in ospi learning
Institutional Correction lecture only . . .
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
human mycosis Human fungal infections are called human mycosis..pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Complications of Minimal Access Surgery at WLH
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
master seminar digital applications in india
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Supply Chain Operations Speaking Notes -ICLT Program
A systematic review of self-coping strategies used by university students to ...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx

I need help creating a parametized JUnit test case for the following.pdf

  • 1. I need help creating a parametized JUnit test case for the following class. import java.util.Arrays; import java.util.Collections; import java.util.LinkedList; import java.util.List; import static java.lang.Integer.valueOf; public class IntegerSet { /** * The only instance variable in the class. No other variables allowed. */ private List list; public int size() { return list.size(); } public boolean isEmpty() { return list.isEmpty(); } /** * Initializes the empty set. */ public IntegerSet() { list = new LinkedList(); } /** * Initializes a set with the numbers in the argument. */ public IntegerSet(Integer[] integers) { this(); if (integers == null) { throw new NullPointerException("The argument cannot be null"); } else { for (Integer i : integers) { if (!exists(i)) { this.insertElement(i); }
  • 2. } } Collections.sort(list); } /** * Inserts an integer to the set if the integer does not exist in the set */ public void insertElement(int i) { if (!list.contains(i)) { list.add(i); } } /** * Inserts to the set all the integers in the argument which do not exist in * the set. */ public void insertAll(Integer[] data) { if (data == null) { throw new NullPointerException("The argument cannot be null"); // throw new // IllegalArgumentException("The argument cannot be null"); } else { Arrays.sort(data); for (Integer num : data) { if (!exists(num)) { insertElement(num); } } } } /** * Deletes an integer from the set if it exists in the set. */ public void deleteElement(int i) { list.remove(i); }
  • 3. /** * Deletes all the elements in the set. */ public void deleteAll() { list.clear(); } /** * Returns true if the argument exists in the set, false otherwise. */ public boolean exists(int i) { return list.contains(i); } public String toString() { char delimiter = ' '; String str = "["; for (Integer i : list) { str += i; str += delimiter; } return str + "]"; } /** * Finds the union of two sets. A null pointer is considered an empty set. * * Return "new IntegerSet()" when the result is an empty set. */ public static IntegerSet union(IntegerSet set1, IntegerSet set2) { IntegerSet union = new IntegerSet(); if (set1.isEmpty() && set2.isEmpty()) { return union; } if (!set1.list.isEmpty()) { for (Integer num : set1.list) { if (!union.list.contains(num)) { union.list.add(num);
  • 4. } } } if (!set2.isEmpty()) { for (Integer num : set2.list) { if (!union.list.contains(num)) { union.list.add(num); } } } return union; } /** * Finds the intersection of two sets. A null pointer is considered an empty * set. * * Return "new IntegerSet()" when the result is an empty set. */ public static IntegerSet intersection(IntegerSet set1, IntegerSet set2) { IntegerSet intersection = new IntegerSet(); // check for null pointers if (set1.list.isEmpty() || set2.list.isEmpty()) { return intersection; } for (Integer num : set1.list) { if ((!intersection.list.contains(num)) && set2.list.contains(num)) { intersection.list.add(num); } } return intersection; } /** * Builds an array with the elements in the set. */ public Integer[] toArray() {
  • 5. Integer[] alist; Object[] s = list.toArray(); alist = new Integer[s.length]; for (int i = 0; i < s.length; i++) { alist[i] = valueOf((Integer) s[i]); } Arrays.sort(alist); return alist; } } I'm not very good with JUnit. Could I get some help with how I'm supposed to set up a test case like this? import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class IntegerSetTest { public IntegerSetTest() { } @Before public void setUp() throws Exception { } @After public void tearDown() throws Exception { } @Test public void test() { fail("Not yet implemented"); } @Parameters
  • 6. public void testIntersectionWithNullInput() { } @Parameters public void testExists() { } @Parameters public void testIsEmpty() { } @Parameters public void testUnion() { } @Parameters public void testCreateSetFromArray() { } @Parameters public void testCreateSetFromNull() { } @Parameters public void testDeleteAll() { } @Parameters public void testDeleteEntry(){ } @Parameters public void testINsertAll(){ } @Parameters public void testAllNull(){
  • 7. } @Parameters public void testIntersection(){ } @Parameters public void testUnionWithNullInput(){ } } Solution package com.game; import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.Test; import org.junit.runners.Parameterized.Parameters; @FixMethodOrder public class IntegerSetTest { public IntegerSetTest() { } @Test public void testUnion() { Integer[] int1 = {1,2}; IntegerSet set1 = new IntegerSet(int1); Integer[] int2 = {3,4};
  • 8. IntegerSet set2 = new IntegerSet(int2); IntegerSet unionResult = new IntegerSet(); IntegerSet set = unionResult.union(set1, set2); Assert.assertEquals("Union of two integer sets are equal",4, set.size()); } @Test public void testCreateSetFromArray() { Integer[] int1 = {1,2}; IntegerSet set1 = new IntegerSet(int1); Assert.assertEquals("Successfully created a set from Array",2,set1.size()); } @Test public void testCreateSetFromNull() { try{ IntegerSet set1 = new IntegerSet(null); }catch(Exception e){ Assert.assertEquals("Successfully got exception error needed","The argument cannot be null",e.getMessage()); } } @Test public void testDeleteAll() { Integer[] int1 = {1,2}; IntegerSet set1 = new IntegerSet(int1); set1.deleteAll(); Assert.assertEquals("Successfully deletes all entries from Set",0,set1.size()); } @Parameters
  • 9. public void testDeleteEntry(){ } @Parameters public void testINsertAll(){ Integer[] int1 = {1,2}; IntegerSet set1 = new IntegerSet(); set1.insertAll(int1); Assert.assertEquals("Successfully inserts all entries from Set",2,set1.size()); } }