Need Help writing a test code using J Unit for this following JAVA Class package
ID: 3702446 • Letter: N
Question
Need Help writing a test code using J Unit for this following JAVA Class
package shapes;
import java.awt.Color;
import java.awt.geom.Point2D;
import java.util.Objects;
/**
* Represents a circle.
*
* <p>Invariant:
* <br>myRadius must be greater than zero
* <br>AND myCenter must not be null
* <br>AND myColor must not be null</p>
*
* @author Ricky
* @version 2.5
*/
public class Circle {
// class constants
/**
* A default radius to use when no other value is specified - (1.0 - a unit circle).
*/
private static final double DEFAULT_RADIUS = 1.0; // unit circle
/**
* A default Point for the center of a Circle - (0.0, 0.0) the coordinate system origin.
*/
private static final Point2D DEFAULT_CENTER = new Point2D.Double(0.0, 0.0); // origin
/**
* A default Color to use when no other Color is specified - Color.BLACK.
*/
private static final Color DEFAULT_COLOR = Color.BLACK;
// instance fields
/**
* The radius of this Circle.
*/
private double myRadius;
/**
* The center of this Circle.
*/
private Point2D myCenter; // Note that java.awt.Point is mutable
/**
* The color of this Circle.
*/
private Color myColor;
// constructors
/**
* Constructs a Circle with the specified radius, location, and color.
*
* <p>Precondition: The parameters must not violate the class invariant.
* That is, theRadius must be greater than zero
* AND theCenter must not be null
* AND theColor must not be null</p>
*
* @param theRadius the radius to assign to this Circle
* @param theCenter the center point to assign to this Circle
* @param theColor the color to assign to this Circle
* @throws IllegalArgumentException if theRadius is less than or equal to zero
* @throws NullPointerException if theCenter is null OR theColor is null
*/
public Circle(final double theRadius,
final Point2D theCenter,
final Color theColor) {
if (theRadius <= 0) {
throw new IllegalArgumentException("The radius must be a positive value!");
}
myRadius = theRadius;
/*
* Note that the Objects class and requireNonNull() were added to Java in version 7.
* The method will throw an exception if the parameter passed to the method is null.
* This simplifies the code by eliminating the need for an if / else test
* and an explicit throws statement.
*/
myColor = Objects.requireNonNull(theColor);
// We must make a defensive copy of the mutable Point parameter
// to avoid an encapsulation violation.
// So, we must NOT simply write myCenter = theCenter!
// One way to make a defensive copy is to use a copy constructor if the class has one.
// Unfortunately Point2D.Double does not have a copy constructor.
// Another way is to use an overloaded constructor:
myCenter = new Point2D.Double(theCenter.getX(), theCenter.getY());
// OR
// We can use a no argument constructor and then use setter(s).
//myCenter = new Point2D.Double(); // creates Point at (0, 0)
//myCenter.setLocation(theCenter);
// OR
// We can use the clone() method if the class we want to copy implements Cloneable.
// Point2D.Double does implement Cloneable, so this works also.
//myCenter = (Point2D) Objects.requireNonNull(theCenter).clone();
}
/**
* Constructs a black unit circle centered at the origin.
*/
public Circle() {
this(DEFAULT_RADIUS, DEFAULT_CENTER, DEFAULT_COLOR);
}
// instance methods
// 'mutators', sometimes called 'commands', or sometimes called 'setters'
/**
* Sets the radius of this Circle to the specified value.
*
* <p>Precondition: theRadius must be greater than zero</p>
* <p>Postcondition: this Circle will be assigned the specified radius</p>
*
* @param theRadius the radius value to assign to this Circle
* @throws IllegalArgumentException if theRadius is less than or equal to zero
*/
public final void setRadius(final double theRadius) {
if (theRadius <= 0) {
throw new IllegalArgumentException();
}
myRadius = theRadius;
}
/**
* Sets the location of the center of this Circle to the specified point.
*
* <p>Precondition: thePoint must not be null</p>
* <p>Postcondition: this Circle will be assigned the specified center Point</p>
*
* @param thePoint the center value to assign to this Circle
* @throws NullPointerException if thePoint is null
*/
public final void setCenter(final Point2D thePoint) {
// make a defensive copy of the mutable Point parameter
// to avoid an encapsulation violation
myCenter = (Point2D) Objects.requireNonNull(thePoint).clone();
}
/**
* Sets the Color of this Circle to the specified value.
*
* <p>Precondition: theColor must not be null</p>
* <p>Postcondition: this Circle will be assigned the specified Color</p>
*
* @param theColor the Color value to assign to this Circle
* @throws NullPointerException if theColor is null
*/
public final void setColor(final Color theColor) {
myColor = Objects.requireNonNull(theColor);
}
// 'accessors', sometimes called 'queries', or sometimes called 'getters'
/**
* Returns the radius of this Circle.
*
* @return the radius of this Circle
*/
public final double getRadius() {
return myRadius;
}
/**
* Returns the center Point of this Circle.
*
* @return the center Point of this Circle
*/
public final Point2D getCenter() {
// return a defensive copy of the mutable Point field
// to avoid an encapsulation violation
return new Point2D.Double(myCenter.getX(), myCenter.getY());
// OR
//return (Point2D) myCenter.clone();
}
/**
* Returns the Color of this Circle.
*
* @return the Color of this Circle
*/
public final Color getColor() {
return myColor;
}
/**
* Calculates and returns the diameter of this Circle.
*
* @return The diameter of this Circle
*/
public double calculateDiameter() {
return 2 * myRadius;
}
/**
* Calculates and returns the circumference of this Circle.
*
* @return the circumference of this Circle
*/
public double calculateCircumference() {
return Math.PI * calculateDiameter();
}
/**
* Calculates and returns the area of this Circle.
*
* @return the area of this Circle
*/
public double calculateArea() {
return 2 * myRadius * Math.PI;
}
// overridden methods from class Object
/**
* {@inheritDoc}
*
* The String representation of this Circle will be formatted as follows:
* <br>Circle [radius=(current value), center=(current value), color=(current value)].
*/
@Override
public String toString() {
final StringBuilder builder = new StringBuilder(128); // default initial size = 16
builder.append(getClass().getSimpleName()); // the class name without the package name
builder.append(" [radius=");
builder.append(String.format("%.2f", myRadius));
builder.append(", center=");
builder.append(myCenter);
builder.append(", color=");
builder.append(myColor);
builder.append(']');
return builder.toString();
}
}
Explanation / Answer
Program:
Public void test_login() throws Exception {// starting
clickOnButton("Login");
wait.until(() -> webDriver.findElement(By.linkText("Logout")).isDisplayed());
}// end of thr function
protected void clickOnButton(String label) {
WebElement button = findButton(label);
wait.until(button::isDisplayed);
button.click();
}// end of thr function
Version 1.8
Fixed bug in MultithreadingTester introduced in version 1.5
Version 1.7
Added annotations @IncludeCategories and @ExcludeCategories. In contrast to JUnit 4, which only offers the annotations @IncludeCategory and @ExcludeCategory which allow to specify a single category, these new annotations allow you to specify multiple categories. The annotations can be used with WildcardPatternSuite and withParallelSuite. Example:
@RunWith(WildcardPatternSuite.class)
@SuiteClasses("**/*Test.class")
@ExcludeCategories({SlowTests.class, FlakyTests.class})
public class NormalTests {}
Version 1.6
Minor bugfixes in PollingWait class
Version 1.5
Added deadlock detection to MultithreadingTester
Version 1.4
Added new utility class MultithreadingTester
The SuiteClasses annotation accepts multiple wildcard patterns now as well as negated patterns. E.g.
@RunWith(WildcardPatternSuite.class)
@SuiteClasses({"**/*Test.class", "!samples/**"})
public class AllTests {}
Version 1.3
Added PollingWait and RunnableAssert for waiting that an asynchronous operation succeeds. Unlike theWebDriverWait class provided by Selenium this class does not wait for an artificial condition, which might be wrong and can make your test non-deterministic, this class waits until your assertions become true (or a configurable timeout is reached). Example:
private PollingWait wait = new PollingWait().timeoutAfter(5, SECONDS).pollEvery(100, MILLISECONDS);
@Test
public void test_auto_complete() throws Exception {
...
wait.until(new RunnableAssert("'cheesecake' is displayed in auto-complete <div>") { @Override public void run() throws Exception {
WebElement autoCompleteDiv = driver.findElement(By.id("auto-complete"));
assertThat(autoCompleteDiv, isVisible());
assertThat(autoCompleteDiv, containsText("cheesecake"));
}});
}
Version 1.2
ParallelRunner extends the Theories runner provided by JUnit now, and can be used as a replacement for it. It still executes all normal @Test methods concurrently. Furthermore it executes the calls to @Theory methods with different parameter assignments concurrently too.
Version 1.1
The WildcardPatternSuite runner supports the annotations @IncludeCategory and @ExcludeCategory now and can therefore be used as a replacement for the Categories runner provided by JUnit. Example:
@RunWith(WildcardPatternSuite.class)
@SuiteClasses("**/*Test.class")
@IncludeCategory(SlowTests.class)
public class OnlySlowTests {}
New runner InnerTestClassesSuite which runs all inner test classes of the class annotated with@RunWith(InnerTestClassesSuite.class). In contrast to the Enclosed runner provided by JUnit, it detects if an inner class is actually a test class and ignores all other inner classes. Example:
@RunWith(InnerTestClassesSuite.class)
public class LoginBeanTests {
public static class UnitTests {
@Test
public void test1() { ... }
}
@Configuration
public static class IntegrationTestsConfig { ... }
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = IntegrationTestsConfig.class)
public static class IntegrationTests {
@Test
public void test2() { ... }
}
}
Version 1.0
bundled with JUnit 4.10, Hamcrest 1.3, and Mockito 1.9.5
WildcardPatternSuite runner for specifying the children classes of a test suite with a wildcard pattern like this:
@RunWith(WildcardPatternSuite.class)
@SuiteClasses("**/*Test.class")
public class AllTests {}
ParallelSuite for concurrent execution of test classes. You can either list the test classes, if you use the @SuiteClassesannotation provided by JUnit itself, for example:
@RunWith(ParallelSuite.class)
@SuiteClasses({
LoginFrontendTest.class,
FillOutFormFrontendTest.class,
...
})
public class AllFrontendTests {}
or you can use a wildcard pattern if you use the @SuiteClasses annotation of JUnit Toolbox:
@RunWith(ParallelSuite.class)
@SuiteClasses("**/*FrontendTest.class")
public class AllFrontendTests {}
ParallelRunner for concurrent execution of the test methods in a test class. Example:
@RunWith(ParallelRunner.class)
public class FooTest {
@Test
public void test1() {
}
@Test
public void test2() {
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.