How do I create a unit test, for each class and application in Java, I am using
ID: 3583856 • Letter: H
Question
How do I create a unit test, for each class and application in Java, I am using netbeans. See code below.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package virtualworld;
/**
*
* @author
*/
//below is object/class MyClone for Virtual World
public class myclone {
//declaring instance variables
private String firstName;
private String lastName;
//inserting constructor through right-click on NetBeans
public myclone(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
//inserting getters and setters through right-click on NetBeans encapsulates instance variables
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String introduction(){
return "Your first name is " + firstName + " and your last name is " + lastName + "! Welcome to the Virtual World by Patricia!";
}
}
pet.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package virtualworld;
/**
*
* @author
*/
//below is second class for Virtual World "Pet"
public class pet {
//declaring instance variables
private String animalType;
private String animalColor;
private String animalName;
//inserting constructor through NetBeans right-click
public pet(String animalType, String animalColor, String animalName) {
this.animalType = animalType;
this.animalColor = animalColor;
this.animalName = animalName;
}
public String getAnimalType() {
return animalType;
}
public void setAnimalType(String animalType) {
this.animalType = animalType;
}
public String getAnimalColor() {
return animalColor;
}
public void setAnimalColor(String animalColor) {
this.animalColor = animalColor;
}
public String getAnimalName() {
return animalName;
}
public void setAnimalName(String animalName) {
this.animalName = animalName;
}
public String petIntroduction(){
return "Your pet is a " + animalType + ". Your pet is " + animalColor + ", and your pet's name is " + animalName;
}
}
shoutbox.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package virtualworld;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author
*/
public class ShoutBox {
public static void shoutOutCannedMessage(List list) {
System.out.println("This is a list of options: ");
int size = list.size();
for(int i=0; i {
System.out.println(list.get(i));
}
int userNumber;
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter number: ");
userNumber = scanner.nextInt();
System.out.println(list.get(userNumber));
}
public static void ShoutOutRandomMessage() {
int g;
//Below holds the words to be generated.
String[] subject= {" You", " Someone", " A relative"};
String[] verb= {" is", " was", " will be", " might be"};
String[] adjective= {" rich", " successful", " surprised", " educated"};
String[] object= {" with a new Java course", " with future kids", " with new pets", " with a new house"};
String[] adverb= {" soon. ", " in due time. ", " someday. ", " in some years. "};
Random k = new Random(); //this initializes a Random
int chosenPiece = k.nextInt(subject.length);
//the following will randomly create a sentence that acts as the fortune cookie.
for (g=1; g<=1; g++)
{
String randomFortune = subject[chosenPiece]
+ verb[chosenPiece]
+ adjective[chosenPiece]
+ object[chosenPiece]
+ adverb[chosenPiece];
System.out.println("Random fortune cookie says: " + randomFortune );
}
}
}
vituralworld.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package virtualworld;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import static virtualworld.ShoutBox.ShoutOutRandomMessage;
/**
*
* @author
*/
public class Virtualworld {
/**
* @param args the command line arguments
*/
//to remove code from main, I have created separate methods
//myApp gets whatever methods are in run() to execute
public static void main(String[] args) {
Virtualworld myApp = new Virtualworld();
myApp.run();
ShoutOutRandomMessage();
}
private List list;
//run is a method that gets the methods createMenu, createClone, createPet and ShoutOutRandomMessage to run
public void run(){
//shoutOutCannedMessage(); I have commented this out rather than delete it
createClone();
createPet();
createMenu();
}
/*this is the createClone method, it takes input and manipulates the myclone class
it produces an output by plugging in the data taken to myclone class and
produces the introduction
*/
// This creates the array list and invokes the shoutOutCannedMessage method
private void createMenu()
{
//list = new ArrayList(); I commented this out because I received a warning that it was unchecked
List list = new ArrayList();
list.add(0, " ");
list.add(1, "1. Java is fun!");
list.add(2, "2. Greece is famous for its archaeological history. ");
list.add(3, "3. Spain has great churros and hot chocolate. ");
list.add(4, "4. Sweden has beautiful architecture. ");
list.add(5, "5. Choose Denmark if you love Lego! ");
list.add(6, "6. South Africa has a great Safari option by Sun City!");
list.add(7, "7. Japan is fun and filled with gorgeous cherry trees.");
list.add(8, "8. The U.K. is a place with history right next to modernity");
list.add(9, "9. This is a project for IT 511");
list.add(10, "10. This was created by ");
ShoutBox.shoutOutCannedMessage(list);
}
/*public static void ShoutOutRandomMessage() {
int g;
//Below holds the words to be generated.
String[] subject= {" You", " Someone", " A relative"};
String[] verb= {" is", " was", " will be", " might be"};
String[] adjective= {" rich", " successful", " surprised", " educated"};
String[] object= {" with a new Java course", " with future kids", " with new pets", " with a new house"};
String[] adverb= {" soon. ", " in due time. ", " someday. ", " in some years. "};
Random k = new Random(); //this initializes a Random
int chosenPiece = k.nextInt(subject.length);
//the following will randomly create a sentence that acts as the fortune cookie.
for (g=1; g<=1; g++)
{
String randomFortune = subject[chosenPiece]
+ verb[chosenPiece]
+ adjective[chosenPiece]
+ object[chosenPiece]
+ adverb[chosenPiece];
System.out.println("Random fortune cookie says: " + randomFortune );
}
}
*/
/* This is commented rather than deleted
private void shoutOutCannedMessage()
{
System.out.println("This is a list of options: ");
int size = list.size();
for(int i=0; i {
System.out.println(list.get(i));
}
int userNumber;
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter number: ");
userNumber = scanner.nextInt();
System.out.println(list.get(userNumber));
}
*/
private void createClone()
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter your first name: ");
String firstName = input.next();
System.out.println("Please enter your last name: ");
String lastName = input.next();
myclone individual = new myclone(firstName, lastName);
System.out.println(individual.introduction());
}
/*this is the createPet method, it takes input and manipulates the pet class
it produces an output by plugging in the data taken to pet class and
produces the pet's introduction
*/
private void createPet()
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter the type of animal your pet will be (Ex. cat, dog, etc): ");
String animalType = input.next();
System.out.println("Please enter what color you want your pet to be (Ex. blue, green, brown, white): ");
String animalColor = input.next();
System.out.println("Please enter your pet's name: ");
String animalName = input.next();
pet kind = new pet(animalType, animalColor, animalName);
System.out.println(kind.petIntroduction());
}
}
Explanation / Answer
Some of the steps to create Unit test using Net beans
1)Creating the Java Class Library Project
Choose File > New Project from the main menu
Select Java Class Library from the Java category and click Next
Type the project nameand set the location
Deselect the Use Dedicated Folder option, if selected
Click finish
Write the Unit Tests
We need to create the unit test cases the classes in the project according to our project the unit test classes will be Myclone, shoutBox, Pet, virtualworld
Taken Myclone class to create the Unit Test cases
If you are using NetBeans IDE 7.1 or earlier you do not need to specify the test framework because JUnit is specified by default.
Modify the name of the test class toMycloneJUnit3Testin the Create Tests dialog.
When you change the name of the test class, you will see a warning about changing the name. The default name is based on the name of the class you are testing, with the word Test appended to the name
For example, for the class MyClass.java, the default name of the test class is MyClassTest.java.
Select JUnit in the Framework dropdown list.
Deselect Test Initializer and Test Finalizer. Click OK.
Select JUnit 3.x in the Select JUnit Version dialog box.
After Select, the IDE creates theMycloneJUnit3Test.java test class in the samplepackage under the Test Packages node in the Projects window.
If you look at the generated test class MycloneJUnit3Test.java in the editor, you can see that the IDE generated the following test class with test methods for the methods equal and scalarMultiplication.
public class MycloneJUnit3Test extends TestCase
{ //Test of equal method, of class Myclone.
public void testEqual() {
System.out.println("equal");
int[] a = null;
int[] b = null;
boolean expResult = false;
boolean result = Myclone.equal(a, b);
assertEquals(expResult, result); // TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
} //Test of scalarMultiplication method, of class Myclone.
public void testScalarMultiplication() {
System.out.println("scalarMultiplication");
int[] a = null;
int[] b = null;
int expResult = 0;
int result = Myclone.scalarMultiplication(a, b);
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("The test case is a prototype.");
}
}
Writing Test Methods for MyClone.java
Need to modify the output messages to run the tests, but you may want to modify the output to help identify the results displayed in the JUnit Test Results output window.
Open MycloneJUnit3Test.java in the editor.
Modify the test skeleton for testScalarMultiplication by changing the value of the println and removing the generated variables.
The test method should now look like the following (changes displayed in bold):
public void testScalarMultiplication() {
System.out.println("* MycloneJUnit3Test: testScalarMultiplication()");
assertEquals(expResult, result);
}
// Now add some assertions to test the method.
public void testScalarMultiplication() {
System.out.println("*MycloneJUnit3Test: testScalarMultiplication()");
assertEquals( 0, Myclone.scalarMultiplication(new int[] { 0, 0}, new int[] { 0, 0}));
assertEquals( 39, Myclone.scalarMultiplication(new int[] { 3, 4}, new int[] { 5, 6}));
assertEquals(-39, Myclone.scalarMultiplication(new int[] {-3, 4}, new int[] { 5,-6}));
assertEquals( 0, Myclone.scalarMultiplication(new int[] { 5, 9}, new int[] {-9, 5}));
assertEquals(100, Myclone.scalarMultiplication(new int[] { 6, 8}, new int[] { 6, 8}));
}
This test method uses the JUnit assertEquals method. To use the assertion, you supply the input variables and the expected result.
Modify the test skeleton for testEqual by deleting the generated method bodies and adding the following println.
System.out.println("*MycloneJUnit3Test: testEqual()");
// The test method should now look like the following:
public void testEqual() {
System.out.println("*MycloneJUnit3Test: testEqual()");
}
// Modify the testEqual method by adding the following assertions (displayed in bold).
public void testEqual() {
System.out.println("*MycloneJUnit3Test: testEqual()");
assertTrue(Myclone.equal(new int[] {}, new int[] {}));
assertTrue(Myclone.equal(new int[] {0}, new int[] {0}));
assertTrue(Myclone.equal(new int[] {0, 0}, new int[] {0, 0}));
assertTrue(Myclone.equal(new int[] {0, 0, 0}, new int[] {0, 0, 0}));
assertTrue(Myclone.equal(new int[] {5, 6, 7}, new int[] {5, 6, 7}));
assertFalse(Myclone.equal(new int[] {}, new int[] {0}));
assertFalse(Myclone.equal(new int[] {0}, new int[] {0, 0}));
assertFalse(Myclone.equal(new int[] {0, 0}, new int[] {0, 0, 0}));
assertFalse(Myclone.equal(new int[] {0, 0, 0}, new int[] {0, 0}));
assertFalse(Myclone.equal(new int[] {0, 0}, new int[] {0}));
assertFalseMyclone.equal(new int[] {0}, new int[] {}));
assertFalse(Myclone.equal(new int[] {0, 0, 0}, new int[] {0, 0, 1}));
assertFalse(Myclone.equal(new int[] {0, 0, 0}, new int[] {0, 1, 0}));
assertFalse(Myclone.equal(new int[] {0, 0, 0}, new int[] {1, 0, 0}));
assertFalse(Myclone.equal(new int[] {0, 0, 1}, new int[] {0, 0, 3}));
}
//This test uses the JUnit assertTrue and assertFalsemethods to test a variety of possible results. For the test of this method to pass, the assertTrue must all be true and assertFalse must all be false.
Save your changes.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.