Part 1
Testing with Junit Framework
JUnit is a unit testing framework for Java programming language and plays an important role in test-driven development. Using Junit, you will test the methods in a class to make sure that they work as expected.
In the exercises below, you will define a class with a few methods (Class Under Testing) and generate a Test class for the class under testing. In the Test class, a test method is generated for each of the methods to be tested in CUT. You will write the testing logic (in each of the testing methods) by calling assert methods. Assert is a method useful in determining Pass or Fail status of a test case. The assert methods are provided by the class org. junit. Assert which extends java. Object class.
For example, the method, Assertions. assertEquals() is used to assert that expected value from a method and actual value are equal. The method, assertEquals() has many overloaded methods for different data types e.g. int, long, float, String etc. It also supports passing error message to be printed in case test fails. e.g.
public static void assertEquals(int expected, int actual)
public static void assertEquals(int expected, int actual, String message)
public static void assertEquals(int expected, int actual, Supplier<String< messageSupplier)
Exercise 1
Using JUnit with Eclipse
Step 1:
If you have not done it already, please download
- jar
- hamcrest-core.jar
from https://github.com/junit-team/junit4/wiki/Download-and-Install and store them in a folder called junit.
Step 2:
Create a new Java project in Eclipse, called lab8testing.
Click on Project -> Properties, select Java Build Path, Libraries, click Add
- External JARs and browse to directory where you downloaded and saved the Junit jar files. Select jar and hamcrest-core.jar
click Open. You will see both files will appear on your screen in the list of libraries. Click OK
Next, check to see what JRE System Library you are using (also viewable in the same libraries section). If it is not 1.8 then remove that library by clicking on it an clicking the remove button. Then click on add library -> JRE System Library -> Execution Environment and select from the dropdown JavaSE-1.8 (jre) pictured below:
You may need to remove the module declaration from module-info.java
Step 3:
Create a package called lab8 under the project, lab8testing. Define a class, MyCalculator, to test:
public class MyCalculator {
public int add(int i, int j) {
return i+j;
}
// You must write a subtract() and a multiply()
// methods
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Now, from your screen with MyCalculator class, click on FileNewJUnitTestCase.
You should see the screen below. Select New Junit 4 test (as shown) and click next.
Check the methods, add, subtract and multiply.
It may prompt you to choose to add Junit to Java build path. Choose ok.
Now, you should see a new class, called MyCalculatorTest generated. This class one test method for each of the methods in class MyCalculator.
For example, if you have a method called add in MyCalculator, you will have a method called testAdd() in MyCalculatorTest class.
import static org.junit.Assert.*;
class MyCalculatorTest {
@Test
public void testAdd() {
fail(Not yet implemented); // Remove this line
// Add the following lines
MyCalculator calculator = new MyCalculator();
int result = calculator.add(100, 200);
// compare the expected result with the actual
assertEquals(300,result);
}
}
Now, while you are on MyCalculatorTest page, go to Run RunAs JUnitTest.
If there are no errors, you should see the screen below, where the green bar shows no errors.
Now change the number 300 in assertEquals(300,result) to 200. And Run RunAs JUnitTest.
What do you see?
The green bar turns red and indicates 1 failure. A failure trace is provided which shows that the assertion expected a value of 200, but the result was 300.
Exercise 2 :
Include the test code in testSubtract() and testMultiply as well and run them as Junit tests.
Exercise 3
Now add two more methods, divide() and mod() in MyCalculator. Note: Make sure that your code checks for division by 0. Generate test cases just like Exercises 1 and 2.
Exercise 4 :
Create a package called lab8exer4. In this package, create a class called StringManager with the code below:
public class StringManager {
// checks if the given parameter is a palindrome (reads same when
// read in reverse
public boolean palindrome(String original) {
String lineInLowerCase = original.replaceAll(\s+, ).toLowerCase();
StringBuffer line = new StringBuffer(lineInLowerCase);
StringBuffer reverse = line.reverse();
return (reverse.toString()).equals(lineInLowerCase);
}
// returns no. of words in a line of text
public int getNoOfWords(String lineOfText) {
String[] words = lineOfText.split(\s+) ;
return words.length;
}
public static void main(String[] args) {
}
}
Generate Testcases as you have done in the previous exercises.
References
Part 2
In this part, you will create an executable jar file from the given code. The code shows a public MVCDemo class with several non-public classes in the same file (for convenience).
Create a package called lab8part2 and create class called MVCDemo in the package. Copy and paste the code given in MVCDemo.java.
Go to File -> Java -> runnable JAR file
Click Finish. You may see that the jar file is created with some warnings. Make sure you know the location of where the jar file is saved.
Now open the command window and cd to the folder where you saved the jar file.
From the command prompt, type
java -jar name_of_jarfile.jar
You should be able to see the following window.
Click on the buttons, IncreasePrice and ReducePrice and see the changes.
This is an example of an application showing the MVC architectural style. The GUI window has two areas where the price of the item is shown as text and with colors. The price of the item is increased or reduced using the buttons. The changes are shown in the two views.
What is the controller part in this window?
The controller part in this window is the Increase Price and Reduce Price buttons because they contain the control logic of the program
Reviews
There are no reviews yet.