Author | Message |
---|---|
khsu
Posts: 30
|
Posted 13:48 Jan 24, 2014 |
I finished writing the program for this exercise, but how do I write a Junit test for it? This program requires a user input, which so far I can only test by using the test method - System.out.print("Expected:"); |
Eric Liao
Posts: 158
|
Posted 13:55 Jan 24, 2014 |
E 4.8Write a program that asks the user for the lengths of a rectangle's sides. Then print * The area and perimeter of the rectangle * The length of the diagonal (use the Pythagorean theorem) Write a JUnit test method to test your code.
So depending on how you write your method for the Rectangle class, you can simply pass the parameter in the method to test the return number. For example, assuming I write a Rectangle class like the following: public class Rectangle { private int length, width; // constructors public int getArea() { return length * width; } // getter and setters } In the test class, you only need to test like the following @Test public void testArea() { Rectangle r1 = new Rectangle(10, 20); // set up the width and height as initial state of r1 using constructor assertEquals( 200, r1.getArea() ); // and test area according to how you create the object } |
lakerfan94
Posts: 143
|
Posted 22:43 Jan 24, 2014 |
What I'm confused is that how are going to use the scanner when we make the rectangle class when, highly likely in the the JUnit test, we can just simply pass a parameter to initialize the length and width when we're using the JUnit test? |
rabbott
Posts: 1649
|
Posted 22:56 Jan 24, 2014 |
You can also call the Scanner from the JUnit test. For example:
There seems to be a glitch in the relationship between JUnit tests and the Terminal. If you run the preceding test, the terminal window might not show up. To make the terminal window visible, select Show Terminal from the View menu. A shortcut is simply to enter control-t. Last edited by rabbott at
23:06 Jan 24, 2014.
|
lakerfan94
Posts: 143
|
Posted 23:06 Jan 24, 2014 |
What I meant to ask is that how are we going to establish a constructor in the Rectangle class when, from my perception, we can initialize a length and a width inside the constructor by using the scanner. What's confusing me is how are we going to create a Rectangle object in our JUnit test, based on the type of constructor we make in the Rectangle class? |
mrente12
Posts: 18
|
Posted 23:06 Jan 24, 2014 |
Thanks! |
lakerfan94
Posts: 143
|
Posted 20:14 Jan 25, 2014 |
Last edited by lakerfan94 at
20:18 Jan 25, 2014.
|
rabbott
Posts: 1649
|
Posted 21:16 Jan 25, 2014 |
Normally you would ask the user for input in the JUnit test. The Rectangle class itself would not normally talk to the user. Type control-t to make the terminal appear if it isn't visible. That's an annoying bug. (Also, please don't use ALL CAPS.) Last edited by rabbott at
21:16 Jan 25, 2014.
|
lakerfan94
Posts: 143
|
Posted 21:45 Jan 25, 2014 |
I apologize for the all caps. When I clicked on "reply with quote" and clicked "submit", that type of font came out. I tried editing multiple times but I didn't know how to get the font to the normal font. I apologize for that. Last edited by lakerfan94 at
21:45 Jan 25, 2014.
|
rabbott
Posts: 1649
|
Posted 22:25 Jan 25, 2014 |
OK. I understand. |