Test AssertEquals Pole Prostokąta

0

Cześć, potrzebuję wykonać test sprawdzający pole prostokąta (potem obwód i inne figury). Wszystko utworzone, klasa Runner, klasa Rectangle, no ale z tym testem mam kłopot. Proszę o pomoc:)

public class Rectangle {
double a;
double b;
public Rectangle(double a, double b) {
this.a = a;
this.b = b;
}
public double getArea() {
return a * b;
}
public double getPerimeter() {
return (2 * (a + b));
}
}

public class Runner {
public static void main(String[] args) {
Rectangle liczymy = new Rectangle(5, 10);
System.out.println("Pole prostokąta: " + liczymy.getArea());
System.out.println("Obwód prostokąta: " + liczymy.getPerimeter());

}

}

import org.junit.Test;
import static junit.framework.Assert.assertEquals;
public class Rectangle {
@Test
public void testGetArea (){
Rectangle l = new Rectangle();
assertEquals(50, l.getArea());
}
}

0

W teście tworzysz prostokąt bez podania parametrów. Jak program ma policzyć pole?

0

Znak mnożenia jest, tylko tutaj go nie widać. W Runner liczy dobrze.

@Test

    public void testGetArea (){

    Rectangle l = new Rectangle();

    double a = 5;
    double b = 10;

  assertEquals(50.0, a*b);

}

Teraz test jest poprawny ale nie wiem czy to jest prawidłowo napisane....
Nie trzeba wykorzystać .getArea()?

0

Teraz jest poprawnie. Jeszcze przydałby się jakiś if żeby nie podawać ujemnych wartości.

public class RectangleTest {

    @Test

    public void testGetArea (){

          assertEquals(6.0, new Rectangle(2,3).getArea());
          assertEquals(8.0, new Rectangle(2,4).getArea());
          assertEquals(66.0, new Rectangle(-2,-33).getArea());
          assertEquals(100.0, new Rectangle(2,50).getArea());
          assertEquals(25.0, new Rectangle(0.5,50).getArea());
          assertEquals(3.0, new Rectangle(1.5,2).getArea());
          assertEquals(4.20, new Rectangle(2,2.10).getArea());

    }
0

Zgadzam się.

1 użytkowników online, w tym zalogowanych: 0, gości: 1