Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 1- Creating Unit Test Plans The first part of this lab involves analyzing the requirements and the design for a method and completing a

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Part 1- Creating Unit Test Plans The first part of this lab involves analyzing the requirements and the design for a method and completing a test plan for it. Unit testing requires an understanding of the what the unit should accomplish and what results should be obtained from a correctly implemented unit. After completing this exercise, you will be able to: .Describe the basic concepts and techniques of unit testing. .Analyze the requirements and design of a software unit in order to create a test plan for the unit. Suppose you are part of a team developing a software unit that will convert an un-signed 16-bit binary number (in string format) to a Java int value. For example, the method binaryToDecimal does the following binaryToDecima1 "0000000000000 returns0 binaryToDecinal (#0000000000001111") returns 15 binaryToDecinal (#0000010000000000") returns 1024 Recall that a test case consists of an input value, an expected value and a comment about what is being tested. For the examples shown above the test cases might look like this: Unit Tests for binaryToDecimal Method Test # Input Expected Output Test Condition Test all 0s Test odd number Test even number Test all Is 0000000000001111 0000010000000000 15 1024 65535 Unit Tests for decimalToBinary Method Test # Input Expected Output Test Condition In addition, another method exists that will convert an int (in base 10) to a binary number represented as a String. What are some test cases that you would write to test this method? Write them out in a table format. Part 2- Unit Testing in Eclipse Create a new Eclipse project called Lab6. Add the JUnit4 Library to your project as you did for last week's lab Suppose that another programmer has implement are responsible for testing it. Download BinString.java from Blackboard import it into a your Lab6 project. For the first part of the lab, you will create a JUnit test class for the BinString class. To create a test class: ed these methods in a utility class called BinString and you 1. Select the menu options File > New > JUnit Test Case 2. Click on the radio button New JUnit 4 test 3. Type BinStringTest for the class name if the wizard does not automatically create this name 4. Check that the Class under test: is BinString (if not, type it in) and click Next 5. Select all the methods to be tested in the BinString class (do not select anything in the Object class). There should be 4 methods selected, then click Finish 6. Add code to the test class to create and run tests of the two methods decimaltoBinary and binary ToDecimal. Include at least two test method calls for each one. Here is an example method: @Test public final void testBinaryToDecimal1o JUnit test int expected 0 assertEqualsCexpected, BinString.binaryToDecimalC" 7. To run your test class, select your test class and choose Run as>JUnit Test from the Run drop- down menu in the toolbar. You can view the results of the test in the JUnit window. Red bar means the tests failed, green bar indicates that all tests pass. The goal here is to write tests that will uncover any potential errors. Were you able to discover the eror in BinString? Show your TA the corrected code and describe how you corrected the bug For more information on JUnit testing, you can refer to this Eclipse tutorial: HelpHelp Contents-> Java Development User Guide->Getting Started-Basic tutorial->Writing and running JUnit tests Part 1- Unit Testing Recursive Methods Download the RecursiveMethods.java and RecursiveMethodsTest.java files and import them into your Lab6 project folder. Execute the test and they should fail because the RecursiveMethods class consists of method stubs public class BinStringt public static String convert (String s) return decimalToBinary(sum(s)) //returns the integer sum of the ASCII characters in the string public static int sum(String s) t return 0; if(s.length) 1)( return ((int) (s.charAt(e))); return ((int) (s.charAt (e))) + sum (s.substring(1)); //converts the parameter from decimal to ASCII public static String decimalToBinary(int x) i return ""; if(x % 2 1){ return "1"decimalToBinary(x/2); return "" decimalToBinary(x/2); // converts the parameter string of 0 or 1 characters that // represents a binary number to a decimal integer public static int binaryToDecimal(String binaryNum) int sum; int twoPower1; for (int i binaryNum .length() - 1; > 0; -) { if (binaryNum.charAt(i)1') else if (binaryNum.charAt(i) !-0) twoPowertwoPower; sumsum t twoPower; throw new IllegalArgumentException( "Invalid binary number."); return sum; public class RecursiveMethods returns a String of character, ch. The length is determined by the second parameter, length public static String buildStringOfCharacters (char ch, int length) return ""; * returns an int array that has the elements in reverse order of the parameter array, nums * Process this recursively beginning with the last element. public static int[] reverseNumArray (int [] nums, int backIndex) { return nums; * returns true if the int array parameter is sorted from smallest to largest, false otherwise * Process this recursively beginning with the first element. public static boolean isSmallestToLargest (intl] values, int firstIndex) { return false * @returns true if the parameter String, str is a palindrome false otherwise public static boolean sPalndrome(String str, int begin, int end) { return false import static org-junit.Assert.; import org.junit.Test; Programmer Programmer #1: #2 : public class RecursiveMethodTest @Test public final void testBuildStringOfCharacters1() assertEquals (null, "g00", RecursiveMethods.buildString0fCharacters(", 3)); @Test public final void testBuildStringOfCharacters2) 1 assertEquals (null, "???????", RecursiveMethods.buildStringofCharacters(7)) @Test public final void testBuildStringOfCharacters3() assertEquals(null, "", RecursiveMethods . buildstringOfCharacters('%", 0)); @Test public final void testReverseNumArray1() [ int[] input1, 2, 3}; int[] expectedoutput-(3, 2, 1; int[] actualOutput-RecursiveMethods.reverseNumArray (input, input.length-1); assertArrayEquals (expectedOutput, actualOutput); @Test public final void testReverseNumArray2(O int[] input1, 2, 2}; int [ ] expectedOutput 12, 2, 1); int[] actualOutput-RecursiveMethods.reverseNumArray (input, input.length-1); assertArrayEquals (expectedOutput, actualOutput); @Test public final void testReverseNumArray3(O int[] input1, 0, 1]; int [ ] expectedOutput {1, 0, 1); int[] actualOutput-RecursiveMethods.reverseNumArray (input, input.length-1); assertArrayEquals (expectedOutput, actualOutput); @Test public final void testIsSmallestToLargest1() int [ ] input {1, 2, 3); assertTrue (RecursiveMethods.isSmallestToLargest(input, 0)); @Test public final void testIsSmallestToLargest1() int[] input1, 2, 3}; assertTrue (RecursiveMethods.isSmallestToLargest(input, 0)); Test public final void testIsSmallestToLargest2() int[] input1, 2, 2}; assertTrue (RecursiveMethods.isSmallestToLargest(input, 0)); @Test public final void testIsSmallestToLargest3() int[] input -1, e, 1}; assertFalse (RecursiveMethods.isSmallestToLargest (input, 0)) Test public final void testIsSmallestToLargest4) int[] input3, 3, 3; assertTrue (RecursiveMethods.isSmallestToLargest(input, 0)); @Test public final void testIsPalindrome1) String str"madam"; assertTrue(RecursiveMethods.isPalindrome (str, 0, str.length)-1)) Test public final void testIsPalindrome2) String str"naan"; assertTrue(RecursiveMethods.isPalindrome (str, 0, str.length)-1)) @Test public final void testIsPalindrome3() String str-"nope"; assertFalse (RecursiveMethods.isPalindrome(str, , str.length()-1)); Test public final void testIsPalindrome4) String str-"a"; assertTrue(RecursiveMethods.isPalindrome (str, 0, str.length)-1)) @Test public final void testIsPalindrome5) String str"apple" assertFalse (RecursiveMethods.isPalindrome(str, , str.length()-1))

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction To Data Mining

Authors: Pang Ning Tan, Michael Steinbach, Vipin Kumar

1st Edition

321321367, 978-0321321367

More Books

Students also viewed these Databases questions