QuestionQuestion

Task 1 – Getting Started - Fibonacci. Download the Fibonacci.zip Eclipse project to your Eclipse workspace. Examine the source code, unit test code and generated JavaDocs. The Fibonacci object/method takes an argument, n, and generates the nth Fibonacci number. You should notice that in FibonacciTest.java, there is a testFibonacci method corresponding to the fibonacci method in the Fibonacci class. If you ever wish to create test classes, Eclipse has a wizard which will create these corresponding test methods for you.

In the test class we assert that the first 7 Fibonacci numbers must be equal to 0,1,1,2,3,5,8,13 by writing the expected value and calling the fibonacci method to generate the number. If the fibonacci method fails at generating the right number, it will throw an AssertionError with a message showing which number it failed to generate, and the JUnit framework will show the infamous red bar of a failed test.

When you run a test class, JUnit will run each method annotated with @Test separately and show a green bar if all of them pass and a red bar if any of them fail. It is important that anything happening in a test method is independent from the other test methods, otherwise you risk getting weird results. In this example, however, we have only one test method, paired with the setUp and tearDown methods, annotated with @Before and @After, respectively, to force JUnit to run them before and after every test case.

For this task, I ask that you do the following:
1. To run the test, select the FibonacciTest.java in the Project Explorer window and choose Run  Run As  JUnit Test. Notice the new JUnit window appears and shows a red bar indicating that the test failed. Below the listing of the running test cases you can see a stack of all failed test cases and their messages.
2. Now, find the bug in Fibonacci.java, correct it and run the test again.

Once you have completed this task, you should commit this project to your repository (with appropriate commit comments!).      

For this task, briefly describe what the error was and how you corrected it.

Task 2 – A Little More Advanced - Rectangle. Download the Rectangle.zip Eclipse project and import it to your Eclipse workspace. Examine the source code, unit test code and generated JavaDocs. The Rectangle class defines a rectangle by holding two points and has two methods to calculate the area of the rectangle and the length of its diagonal. In this task, we test two methods from the Rectangle class. These two methods are using the same set of objects. This set of objects is called a test fixture. You will often need to set up a test fixture once and use the created objects several times in each test method. Observe the method setUp(), which is annotated with @Before. This method will be executed before each test case. Likewise, you can annotate a method with @After, often called tearDown() if something needs to be cleaned up after each test case. JUnit will run each test method separately but uses the same test fixture for all of them. There are similar annotations, @BeforeClass and @AfterClass that will run once before/after the test class is executed – which may be useful for setting up database/network connections, activating logging activities or other things that are computationally intensive that you would not want to do before/after every test.

For this task, I ask that you do the following:
1. Open RectangleTest.java and examine the source code. It creates two rectangles with different points and checks to see if the area and diagonal matches the expected values.
2. Run it as a JUnit test to see the red bar of a failed test. Examine which test cases failed and check the failure messages. Find the bug in the code and correct it.
3. The bug in the code probably is a result difficult to understand/complex code which can hurt software quality and introduce bugs (as done here). Improve the code to make it higher quality and easier to spot this bug.

Once you have completed this task, you should commit this project to your repository (with appropriate commit comments!).

For this task, briefly describe what the error was and how you corrected it. Also, provide the source code for your improved getDiagonal() and getArea() methods.

Task 3 – On Your Own – A Vending Machine. You should now be prepared to test some relatively simply source code on your own. For this task, you will develop tests for code that implements a simple vending machine. Download the VendingMachine.zip Eclipse project from Blackboard (Projects/Project 2) and import it to your Eclipse workspace. Examine the source code and generated JavaDocs. Note that, this is simply an implementation of a vending machine; since it has no main method, you cannot actually execute it, as is.

Since this is a simple vending machine, there are only four slots with codes A, B, C, and D to identify them. Each slot only holds one item. A slot may be empty. To buy yummies from this machine, you put money into it and select the item you want using the code for the slot (e.g., the code "A" selects the item from the first slot). You will be writing a test suite that tests the functionality of this simple vending machine.

You should write unit tests for the following classes and functions:
• VendingMachineItem
o VendingMachineItem Constructor.
o String getName()
o String getPrice()
• VendingMachine
o void addItem(VendingMachineItem item, String code)
o VendingMachineItem removeItem(String code)
o void insertMoney(double amount)
o double getBalance()
o boolean makePurchase()
o double returnChange()   

You do not need to test other parts of the VendingMachine class, such as the constructor.

For each class, create a JUnit test case and name it [class name]Test.java. For example, VendingMachine.java will have a JUnit test case called VendingMachineTest.java. Inside your JUnit test case, you will have a series of test functions. Try to make each test function test only one aspect of a particular function. For example, if you would like to test if a function, named foobar() works correctly on normal inputs, put your test for normal inputs in a separate function from a test that checks if it throws an exception with weird inputs. This way, if JUnit reports that a test has not passed, you know exactly which aspect of your code made the test fail. Make sure you comment your testing code. In the test method comments, make sure that you add a brief sentence to describe exactly what you are testing in that test method. Once you've completed the unit tests, you need a test suite called AllTests.java which runs all the unit tests as a test suite. This is easily done via right clicking on your project selecting New  Other  Java  JUnit  JUnit Test Suite.

Warning! This may sound easy but it will require quite a bit of code. As you should have seen thus far, designing tests takes significant thought and the mentality that you have to “break the code”. A few suggestions you may want to consider:
• Read the comments in the Javadoc describing each function.
• Pay close attention to what each function is supposed to do, as described in the comments. Think of ways to use the public functions to verify that the function does what it is supposed to.
• Pay close attention to why exceptions are thrown
• Pay close attention to pre-conditions and post-conditions
• Remember to test "good" inputs and "bad" inputs
• Remember to test both sides of a boundary condition. For example, if I have a function that takes an argument x and returns true if x is >= 5, remember to write tests for x < 5 and x >= 5.

Finally, there are potential bugs in the code. The goal of testing is to find these bugs. For every bug that you find within the code, correct the bug in the code and describe it in a bugs.txt file that you create in the root directory of the project directory.
Hint: the better the testing you do on the VendingMachine code in this project, the easier subsequent projects will be.
For this task, briefly describe any bugs that you found. You should also upload your Vending Machine project to your GitHub account.

Task 4 – Summing it All Up. Upon completion, each student is to submit a short report that includes your write-ups from the previous tasks (clearly labeled) as well as:
• A description (2-3 paragraphs) of what you learned from this project (particularly Task 4)
• A description (2-3 paragraphs) of what you liked and didn’t like about JUnit’s support for unit testing

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

/* Javadoc style sheet */
/*
Overall document style
*/

@import url('resources/fonts/dejavu.css');

body {
    background-color:#ffffff;
    color:#353833;
    font-family:'DejaVu Sans', Arial, Helvetica, sans-serif;
    font-size:14px;
    margin:0;
}
a:link, a:visited {
    text-decoration:none;
    color:#4A6782;
}
a:hover, a:focus {
    text-decoration:none;
    color:#bb7a2a;
}
a:active {
    text-decoration:none;
    color:#4A6782;
}
a[name] {
    color:#353833;
}
a[name]:hover {
    text-decoration:none;
    color:#353833;
}
pre {
    font-family:'DejaVu Sans Mono', monospace;
    font-size:14px;
}
h1 {
    font-size:20px;
}
h2 {
    font-size:18px;
}
h3 {
    font-size:16px;
    font-style:italic;
}
h4 {
    font-size:13px;
}
h5 {
    font-size:12px;
}
h6 {
    font-size:11px;
}
ul {
    list-style-type:disc;
}
code, tt {
    font-family:'DejaVu Sans Mono', monospace;
    font-size:14px;
    padding-top:4px;
    margin-top:8px;
    line-height:1.4em;
}
dt code {
    font-family:'DejaVu Sans Mono', monospace;
    font-size:14px;
    padding-top:4px;
}
table tr td dt code {
    font-family:'DejaVu Sans Mono', monospace;
    font-size:14px;
    vertical-align:top;
    padding-top:4px;
}
sup {
    font-size:8px;
}
/*
Document title and Copyright styles
*/
.clear {
    clear:both;
    height:0px;
    overflow:hidden;
}
.aboutLanguage {
    float:right;
    padding:0px 21px;
    font-size:11px;
    z-index:200;
    margin-top:-9px;
}
.legalCopy {
    margin-left:.5em;
}
.bar a, .bar a:link, .bar a:visited, .bar a:active {
    color:#FFFFFF;
    text-decoration:none;
}
.bar a:hover, .bar a:focus {
    color:#bb7a2a;
}
.tab {
    background-color:#0066FF;
    color:#ffffff;
    padding:8px;
    width:5em;
    font-weight:bold;
}
/*
Navigation bar styles
*/
.bar {
    background-color:#4D7A97;
    color:#FFFFFF;
    padding:.8em .5em .4em .8em;
    height:auto;/*height:1.8em;*/
    font-size:11px;
    margin:0;
}
.topNav {
    background-color:#4D7A97;
    color:#FFFFFF;
    float:left;
    padding:0;
    width:100%;
    clear:right;
    height:2.8em;
    padding-top:10px;
    overflow:hidden;
    font-size:12px;
}
.bottomNav {
    margin-top:10px;
    background-color:#4D7A97;
    color:#FFFFFF;
    float:left;
    padding:0;
    width:100%;
    clear:right;
    height:2.8em;
    padding-top:10px;
    overflow:hidden;
    font-size:12px;
}
.subNav {
    background-color:#dee3e9;
    float:left;
    width:100%;
    overflow:hidden;
    font-size:12px;
}
.subNav div {
    clear:left;
    float:left;
    padding:0 0 5px 6px;
    text-transform:uppercase;
}
ul.navList, ul.subNavList {
    float:left;
    margin:0 25px 0 0;
    padding:0;
}
ul.navList li{
    list-style:none;
    float:left;
    padding: 5px 6px;
    text-transform:uppercase;
}
ul.subNavList li{
    list-style:none;
    float:left;
}
.topNav a:link, .topNav a:active, .topNav a:visited, .bottomNav a:link, .bottomNav a:active, .bottomNav a:visited {
    color:#FFFFFF;
    text-decoration:none;
    text-transform:uppercase;
}
.topNav a:hover, .bottomNav a:hover {
    text-decoration:none;
    color:#bb7a2a;
    text-transform:uppercase;
}
.navBarCell1Rev {
    background-color:#F8981D;
    color:#253441;
    margin: auto 5px;
}
.skipNav {
    position:absolute;
    top:auto;
    left:-9999px;
    overflow:hidden;
}
/*
Page header and footer styles
*/
.header, .footer {
    clear:both;
    margin:0 20px;
    padding:5px 0 0 0;
}
.indexHeader {
    margin:10px;
    position:relative;
}
.indexHeader span{
    margin-right:15px;
}
.indexHeader h1 {
    font-size:13px;
}
.title {
    color:#2c4557;
    margin:10px 0;
}
.subTitle {
    margin:5px 0 0 0;
}
.header ul {
    margin:0 0 15px 0;
    padding:0;
}
.footer ul {
    margin:20px 0 5px 0;
}
.header ul li, .footer ul li {
    list-style:none;
    font-size:13px;
}
/*
Heading styles
*/
div.details ul.blockList ul.blockList ul.blockList li.blockList h4, div.details ul.blockList ul.blockList ul.blockListLast li.blockList h4 {
    background-color:#dee3e9;
    border:1px solid #d0d9e0;
    margin:0 0 6px -8px;
    padding:7px 5px;
}
ul.blockList ul.blockList ul.blockList li.blockList h3 {
    background-color:#dee3e9;
    border:1px solid #d0d9e0;
    margin:0 0 6px -8px;
    padding:7px 5px;
}
ul.blockList ul.blockList li.blockList h3 {
    padding:0;
    margin:15px 0;
}
ul.blockList li.blockList h2 {
    padding:0px 0 20px 0;
}
/*
Page layout container styles
*/
.contentContainer, .sourceContainer, .classUseContainer, .serializedFormContainer, .constantValuesContainer {
    clear:both;
    padding:10px 20px;
    position:relative;
}
.indexContainer {
    margin:10px;
    position:relative;
    font-size:12px;
}
.indexContainer h2 {
    font-size:13px;
    padding:0 0 3px 0;
}
.indexContainer ul {
    margin:0;
    padding:0;
}
.indexContainer ul li {
    list-style:none;
    padding-top:2px;
}...

By purchasing this solution you'll be able to access the following files:
Solution1.zip and Solution2.docx.

$88.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Software Testing and Analysis Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats