Expert answer:JUnit is a
framework for testing parts, or units of your code. In general, these units are
considered to be the methods of each class. JUnit can help you to make sure
that each of your classes work as expected. In unit testing you will usually
write one test class for each of the classes that you want to test. Your test
class will often include a test method for each method implemented by the class
being tested. But keep in mind that this is not always feasible, or necessary.
A test case will, and should often touch on more than a single method. These tests should be written to make it likely that when all the tests pass,
the code is functioning as required.
follow attachment to find the documents that explain the assignment and requires.
junit_assignment_2_2017.docx
bowling.rar
Unformatted Attachment Preview
Testing Methodology
Unit testing using JUnit
What is JUnit?
JUnit is a framework for testing parts, or units of your code. In general, these units are
considered to be the methods of each class. JUnit can help you to make sure that each
of your classes work as expected. In unit testing you will usually write one test class for
each of the classes that you want to test. Your test class will often include a test method
for each method implemented by the class being tested. But keep in mind that this is not
always feasible, or necessary. A test case will, and should often touch on more than a
single method. These tests should be written to make it likely that when all the tests pass,
the code is functioning as required.
JUnit with Eclipse
Run Eclipse IDE. We will create a new workplace project
so click File -> New -> Project,
then choose Java and click Next.
Type in a project name — for example, ProjectWithJUnit. Click Finish.
The new project will be generated in your IDE. Let’s configure our Eclipse IDE,
so it will add the JUnit library to the build path.
Click on Project -> Properties, select Java Build Path, Libraries, click Add
External JARs and browse to directory where your JUnit is stored. Pick junit.jar
and click Open.
You will see that JUnit will appear on your screen in the list of libraries. By
clicking Okay you will force Eclipse to rebuild all build paths.
To create such a test, right-click on the ProjectWithJUnit title,
select New -> Other, expand the “Java” selection, and choose JUnit.
On the right column of the dialog, choose Test Case, then click Next.
Example
public class HelloWorld {
public String say() {
}
return(“Hello World!”);
}
import junit.framework.TestCase;
public class TestThatWeGetHelloWorldPrompt extends TestCase {
public TestThatWeGetHelloWorldPrompt(
String name) {
super(name);
}
public void testSay() {
HelloWorld hi = new HelloWorld();
assertEquals(“Hello World!”, hi.say());
}
public static void main(String[] args) {
junit.textui.TestRunner.run(
TestThatWeGetHelloWorldPrompt.class);
}
}
Basic Information
Before you begin, we would like to call your attention to the following conventions:
• A Test Case Class is named [classname]Test.java, where classname is the name of
the class that is tested.
• A Test Case Method is a method of the Test Case Class which is used to test one or
more of the methods of the target class. Test Case Methods are annotated with @Test
to indicate them to JUnit. Cases without @Test will not be noticed by JUnit.
JUnit assertions are used to assert that a condition must be true at some point in the test
method. JUnit has many types of assertions. The following is a selection of the most
commonly used assertions:
_ assertEquals(expected, actual): Assert that expected value is equal to the actual value.
The expected and actual value can be of any type, for example integer, double, byte,
string, char or any Java object. If the expected and actual values are of type double or
oat, you should add a third parameter indicating the delta. It represents the maximum
difference between expected and actual value for which both numbers are still considered
equal.
_ assertTrue(condition): Asserts that the Boolean condition is True.
_ assertFalse(condition): Asserts that the Boolean condition is False.
_ assertNull(object): Asserts that an object is null.
_ assertNotNull(object): Asserts that an object is not null.
_ assertSame(expected object, actual object): Asserts that two variables refer to the
same object.
_ assertNotSame(expected object, actual object): Asserts that two variables do not
refer to the same object.
Whenever an assertion fails, an AssertionError is thrown, which is caught by the JUnit
framework and presented as a red bar, indicating test failure. Assert statements accept
an extra message parameter before the other parameters.
Loading the Project
Download the file bowling.rar from blackboard
In Eclipse, choose File -> New -> Java Project. Give it a name (“Lab1″, for instance) and
click Finish.
Running the Test Cases
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.
Assignment Tasks
•
You have been given a template for the bowling class, game class and frame class.
You have to write minimum 60 test cases for the 3 classes.
•
First, write test cases for the methods of each class, and then fill in the methods with
code that will make your test cases pass.
Create GameTest.java
Create FrameTest.java
Create BowlingTest.java
If you run your test cases for GameTest.java, BowlingTest.java, and FrameTest.java
at this point, they should be all pass.
Write the following test cases for Frame class
o testOneThrow()
o testNeverKnockDownAllThePins()
o testStrike()
o testFrameScore()
Write the following test cases for GameTest class
o testEmptyGame()
o testOneThrow1()
o testThreeThrows()
o testThreeThrowsStrike()
o testPerfectGame()
o testAllOnesGame()
Atleast 20 test case should often touch on more than a single method.
Archive your project folder in zip format and name it FirstName1.LastName1FirstName2.LastName2.zip where the first name and last name refers to your name
and your partner’s name participating in the assignment.
Comment your code and comment all the test cases
Uses 5 different assert statements. The most common assert statement is
assertEquals() which takes at least two arguments. Each JUnit test case (method)
•
•
•
•
•
•
•
•
•
•
•
should have at least one of the assert statements listed below, otherwise the test
passes, which can be misleading if you never actually check the value of any data.
Examine the API for JUnit and add a few additional methods and test cases that use
other Assert class methods, such as assertArrayEquals, assertTrue, assertFalse,
assertNull and fail.
Working independently. Your answers to the questions on this assignment will be individually marked, and must be
your own work. You will be assigned 0 marks for this entire assignment, if any of your answers to individual
questions bears a close resemblance to another student’s submission, or to something previously published on the
internet or elsewhere
Reference
Lab session1: http://www.vogella.com/tutorials/JUnit/article.html
Lab session2: https://courses.cs.washington.edu/courses/cse143/11wi/eclipse‐tutorial/junit.shtml
Rubric
Rubric for Assignment 2
Ratings
Criteria
Test
Coverage
Tests cover
Tests cover all
most edge
edge cases.
cases.
40 pts
30 pts
The program works
and meets all of the
Code
specifications.
Correctness
40 pts
Total Points: 100
A test suite exists and runs in JUnit,
but doesn’t actually test anything.
20 pts
The program works and
produces the correct results and
displays them correctly. It also
meets most of the other
specifications.
30 pts
The code is very well
organized and very easy to
Code
follow.
Readability
20pts
Pts
Tests do not
run in JUnit.
10 pts
The program
The code
produces correct
does not
results but does not produce
display them
correct
correctly.
results.
20 pts
10 pts
The code is readable only by
someone who knows what it is
supposed to be doing.
10pts
The code is poorly
organized and very
difficult to read.
0 pts
No
Tests.
0 pts
40
pts
No
Code. 40
0 pts
pts
2
0pts
…
Purchase answer to see full
attachment
You will get a plagiarism-free paper and you can get an originality report upon request.
All the personal information is confidential and we have 100% safe payment methods. We also guarantee good grades
Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.
You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.
Read moreEach paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.
Read moreThanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.
Read moreYour email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.
Read moreBy sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.
Read more