Modify your word search array program (Homework program #1) to create a test cla
ID: 3792070 • Letter: M
Question
Modify your word search array program (Homework program #1) to create a test class for it using the JUnit tool. When you run the test class using JUnit, it should indicate a pass or fail for each method. Please update the JUnit test cases so they use Assert statements for the pass / fail expected and actual results.
HW file
import java.util.Scanner;
import javax.swing.JOptionPane;
// John Lee
//hw3
public class WordArray {
static Scanner scanner = new Scanner(System.in);
public String[] createListOfWords(int listSize)
{
String[] wordList = new String[listSize];
for (int i = 0; i < wordList.length; i++) {
System.out.print("What is word number " + (i + 1));
String newWord = scanner.next();
wordList[i] = newWord;
}
return wordList;
}
public static void main(String[] args) {
System.out.print("How many words are in the list? ");
int listSize = scanner.nextInt();
WordArray wa=new WordArray();
String[] wordList=wa.createListOfWords(listSize);
wa.searchWord(wordList);
}
public int searchWord(String[] wordList) {
int wordCounter = 0;
System.out.print("Type a word to search for: ");
String userInput = scanner.next();
// Description of the program
/* JOptionPane.showMessageDialog(null, " This program calls a method to search an array for the word "
+ userInput);
System.out
.println(" This program calls a method to search an array for the word "
+ userInput);
*/
String in = "userInput";
for (int i = 0; i < wordList.length; i++) {
if (wordList[i].contains(userInput)) {
wordCounter = wordCounter+1;
/* JOptionPane.showMessageDialog(null, "Word found: "
+ wordList[i]);
*/ }
}
/* JOptionPane.showMessageDialog(null, " The word " + userInput
+ " appears " + wordCounter + " times");
*/ System.out.print("The word " + userInput + " appears " + wordCounter
+ " times");
return wordCounter;
}
}
Testing files
import static org.junit.Assert.*;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Test;
// John Lee
public class WordArrayTest2 {
@Test
public void test() {
WordArray wa = new WordArray();
wa.createListOfWords(5);
}
@Test
public void createListTest1() {
WordArray wa = new WordArray();
String[] list=wa.createListOfWords(-5);
if(list.length==5)
pass("pass");
if(list.length<5)
fail("fail");
}
@Test
public void createListTest2() {
WordArray wa = new WordArray();
wa.createListOfWords(2);
}
@Test
public void searhingforwordTest() {
WordArray wa = new WordArray();
String[] wordList = wa.createListOfWords(5);
int counter = wa.searchWord(wordList);
if (counter == 2) {
pass(" pass");
} else
fail(" fail");
}
private void pass(String string) {
System.out.println(string);
}
}
// Test2
import static org.junit.Assert.*;
import org.junit.Test;
//John Lee
public class WordArrayTestclassforjavaclass {
@Test
public void test() {
WordArray wa = new WordArray();
wa.createListOfWords(5);
}
@Test
public void createListTest1() {
WordArray wa = new WordArray();
wa.createListOfWords(-5);
}
@Test
public void createListTest2() {
WordArray wa = new WordArray();
wa.createListOfWords(2);
}
@Test
public void searhingforwordTest() {
WordArray wa = new WordArray();
String[] wordList = wa.createListOfWords(5);
int counter = wa.searchWord(wordList);
if (counter == 2) {
pass(" pass");
} else
fail(" fail");
}
private void pass(String string) {
System.out.println(string);
} }
Explanation / Answer
Hi,
I have changed your test class. and validate your methods and condition with asserttrue and assertequal methods.
Please have a look below file if you need more detail in any line of code please add comment,
package com.test;
//Test2
import static org.junit.Assert.*;
import org.junit.Test;
//John Lee
public class WordArrayTestclassforjavaclass {
@Test
public void test() {
WordArray wa = new WordArray();
wa.createListOfWords(5);
assertEquals("check the size of array", 5, wa.createListOfWords(5).length);
}
@Test
public void createListTest1() {
WordArray wa = new WordArray();
wa.createListOfWords(-5);
assertEquals("check the size of array", -5, wa.createListOfWords(5).length);
}
@Test
public void createListTest2() {
WordArray wa = new WordArray();
wa.createListOfWords(2);
assertEquals("check the size of array", 2, wa.createListOfWords(5).length);
}
@Test
public void searhingforwordTest() {
WordArray wa = new WordArray();
String[] wordList = wa.createListOfWords(5);
assertEquals("check the size of array", 5, wa.createListOfWords(5).length);
int counter = wa.searchWord(wordList);
assertEquals("check the frequency of word in a array please enter same word two times", 2, wa.searchWord(wordList));
if (counter == 2) {
pass(" pass");
} else
fail(" fail");
//assertTrue(condition);
}
private void pass(String string) {
System.out.println(string);
} }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.