Reference no: EM132243502
Data Structures And Object Oriented Programming Assignment - JUnit in Eclipse and Recursion Lab
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:
binaryToDecimal ("0000000000000") returns 0
binaryToDecimal ("0000000000001111") returns 15
binaryToDecimal ("0000010000000000") returns 1024
binaryToDecimal ("1111111111111111") returns 65535
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
|
1
|
0000000000000
|
0
|
Test all 0s
|
2
|
0000000000001111
|
15
|
Test odd number
|
3
|
0000010000000000
|
1024
|
Test even number
|
4
|
1111111111111111
|
65535
|
Test all 1s
|
Unit Tests for decimalToBinary Method
Test #
|
Input
|
Expected Output
|
Test Condition
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
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 implemented these methods in a utility class called BinString and you 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:
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 binaryToDecimal. Include at least two test method calls for each one. Here is an example JUnit test method:
@Test
public final void testBinaryToDecimal1() {
int expected = 0;
assertEquals(expected, BinString.binaryToDecimal("000000000000"));
}
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 error 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: Help -> Help 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.
Implement the recursive methods until all pass the provided unit tests. Once again show the green bar and completed RecursiveMethods.java file to your TA.
Attachment:- Assignment Files.rar