0% found this document useful (0 votes)
76 views

Junit Test Example Public Class Rectangle

This document contains the code for a JUnit test of a Rectangle class. It defines a Rectangle class with getter/setter methods and default/parameterized constructors. It then defines a RectangleTest class with @Test methods that instantiate Rectangles with different parameters and assert that the getter values match expectations. This allows testing the Rectangle class as its methods are implemented.

Uploaded by

Khaled Mansoury
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Junit Test Example Public Class Rectangle

This document contains the code for a JUnit test of a Rectangle class. It defines a Rectangle class with getter/setter methods and default/parameterized constructors. It then defines a RectangleTest class with @Test methods that instantiate Rectangles with different parameters and assert that the getter values match expectations. This allows testing the Rectangle class as its methods are implemented.

Uploaded by

Khaled Mansoury
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

401-27.

1
// JUnit test example public class Rectangle { // State variables. private int height; private int width; // Constructors Rectangle() { height=0; width =0; } Rectangle(int h,int w) { height=h; width =w; } // Readers public int getH() {return height;} public int getW() {return width;} public int getA() {return height* width;} // Writers public void setH(int h) {height=h;} public void setW(int w) {width =w;} public String toString() {return "Rectangle: height="+height+"; width="+ width + "; area="+getA()+".";} } // End of Rectangle class

401-27.2

401-27.3
import static org.junit.Assert.*; import org.junit.*; public class RectangleTest { Rectangle r; Rectangle[] rList=new Rectangle[5]; @Before // Will be performed before each test. public void testSetup() { System.out.println("Setup for test complete."); } @After // Will be performed after each test. public void testComplete() { System.out.println("Test complete."); } @Test public void test1() { // Can be run when reader methods and default // constructor are complete. r=new Rectangle(); try { assertTrue("Test 1: Default values are wrong.", r.getH()==0 && r.getW()==0); System.out.println("Test 1 completed successfully."); } catch (AssertionError e) { System.out.println(e.getMessage()); } } @Test public void test2() { // After area reader is written. r=new Rectangle(); try { assertTrue("Test 2: Default values are wrong.", r.getH()==0 && r.getW()==0 && r.getA()==0); System.out.println("Test 2 completed successfully."); } catch (AssertionError e) { System.out.println(e.getMessage()); } }

401-27.4
@Test public void test3() { // After second constructor is written. r=new Rectangle(); try { r=new Rectangle(10,20); assertTrue("Test 3: Initial values are wrong.", r.getH()==10 && r.getW()==20 && r.getA()==200); System.out.println("Test 3 completed successfully."); } catch (AssertionError e) { System.out.println(e.getMessage()); } } @Test public void test4() { // After second constructor is written. r=new Rectangle(); try { for (int i=0;i<5;i++) { rList[i] = new Rectangle(2*i,3*i); assertTrue("Test 4: Initial values are wrong.", rList[i].getH()==2*i && rList[i].getW()==3*i && rList[i].getA()==6*i*i); } System.out.println("Test 4 completed successfully."); } catch (AssertionError e) { System.out.println(e.getMessage()); } } @Test public void test5() { // After writers are written. r=new Rectangle(); try { for (int i=0;i<5;i++) { rList[i] = new Rectangle(2*i,3*i); rList[i].setH(11*i); rList[i].setW(7*i); assertTrue("Test 5: Modified values are wrong.", rList[i].getH()==11*i && rList[i].getW()==7*i && rList[i].getA()==77*i*i); } System.out.println("Test 5 completed successfully.");

401-27.5
} catch (AssertionError e) { System.out.println(e.getMessage()); }

@Test public void test6() { // toString test. r=new Rectangle(); try { for (int i=0;i<5;i++) { rList[i] = new Rectangle(2*i,3*i); rList[i].setH(11*i); rList[i].setW(7*i); assertTrue("Test 6: Modified values are wrong.", rList[i].getH()==11*i && rList[i].getW()==7*i && rList[i].getA()==77*i*i); System.out.println(rList[i]); } System.out.println("Test 6 completed successfully."); } catch (AssertionError e) { System.out.println(e.getMessage()); } } public static void main(String[] args) { org.junit.runner.JUnitCore.main("RectangleTest"); }

401-27.6
Output from correct Rectangle class test. Setup for test complete. Test 1 completed successfully. Test complete. Setup for test complete. Test 2 completed successfully. Test complete. Setup for test complete. Test 3 completed successfully. Test complete. Setup for test complete. Test 4 completed successfully. Test complete. Setup for test complete. Test 5 completed successfully. Test complete. Setup for test complete. Rectangle: height=0; width=0; area=0. Rectangle: height=11; width=7; area=77. Rectangle: height=22; width=14; area=308. Rectangle: height=33; width=21; area=693. Rectangle: height=44; width=28; area=1232. Test 6 completed successfully. Test complete. I changed setH() so that height was set to h*2 and reran the tests. Setup for test complete. Test 1 completed successfully. Test complete. Setup for test complete. Test 2 completed successfully. Test complete. Setup for test complete. Test 3 completed successfully. Test complete. Setup for test complete. Test 4 completed successfully. Test complete. Setup for test complete. Test 5: Modified values are wrong. Test complete. Setup for test complete. Rectangle: height=0; width=0; area=0. Test 6: Modified values are wrong. Test complete.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy