java-assertj: Rozdiel medzi revíziami
		
		
		
		
		
		
		Skočit na navigaci
		Skočit na vyhledávání
		
				
		
		
		
		
		
		
		
		
	
 (Vytvorená stránka „{{DISPLAYTITLE:AssertJ}}  Knižnica AssertJ poskytuje krajšie asserty pre automatické testy. Napríklad:  {|border="1" cellspacing="0" cellpadding="2" ! JUnit ! Assert…“)  | 
				|||
| (Jedna medziľahlá úprava od rovnakého používateľa nie je zobrazená.) | |||
| Riadok 28: | Riadok 28: | ||
                  .doesNotContain("abc");  |                   .doesNotContain("abc");  | ||
|}  | |}  | ||
| + | |||
| + | == Asserty pre vlastné typy ==  | ||
| + | |||
| + | Máme napríklad takúto triedu:  | ||
| + | |||
| + |  public record Position(int x, int y) {}  | ||
| + | |||
| + | Vytvoríme pre ňu assert:  | ||
| + | |||
| + |  public class PositionAssert extends AbstractAssert<PositionAssert, Position> {  | ||
| + | |||
| + |      protected PositionAssert(Position actual) {  | ||
| + |          super(actual, PositionAssert.class);  | ||
| + |      }  | ||
| + | |||
| + |      public PositionAssert hasX(int expectedX) {  | ||
| + |          isNotNull();  | ||
| + |          if (actual.x != expectedX) {  | ||
| + |              failWithMessage("Expected X %d but was %d", expectedX, actual.x);  | ||
| + |          }  | ||
| + |          return this;  | ||
| + |      }  | ||
| + | |||
| + |      public PositionAssert hasY(int expectedY) {  | ||
| + |          isNotNull();  | ||
| + |          if (actual.y != expectedY) {  | ||
| + |              failWithMessage("Expected Y %d but was %d", expectedY, actual.y);  | ||
| + |          }  | ||
| + |          return this;  | ||
| + |      }  | ||
| + | |||
| + |  }  | ||
| + | |||
| + | Pridáme statickú funkciu na elegantné získanie assertu:  | ||
| + | |||
| + |  public static PositionAssert assertThat(Position position) {  | ||
| + |      return new PositionAssert(position);  | ||
| + |  }  | ||
| + | |||
| + | A môžeme písať testy v štýle AssertJ:  | ||
| + | |||
| + |  @Test  | ||
| + |  public void testPosition() {  | ||
| + |      var p = new Position(3, 4);  | ||
| + |      assertThat(p).hasX(3)  | ||
| + |                   .hasY(4);  | ||
| + |  }  | ||
Aktuálna revízia z 15:52, 1. február 2023
Knižnica AssertJ poskytuje krajšie asserty pre automatické testy. Napríklad:
| JUnit | AssertJ | 
|---|---|
assertTrue(3 < 4);  | 
assertThat(3).isLessThan(4);  | 
assertTrue("abc".contains("a"));
assertTrue("abc".contains("b"));
assertTrue("abc".contains("c"));
 | 
assertThat("abc").contains("a")
                 .contains("b")
                 .contains("c");
 | 
assertFalse(list.isEmpty());
assertFalse(list.contains("abc"));
 | 
assertThat(list).isNotEmpty()
                .doesNotContain("abc");
 | 
Asserty pre vlastné typy
Máme napríklad takúto triedu:
public record Position(int x, int y) {}
Vytvoríme pre ňu assert:
public class PositionAssert extends AbstractAssert<PositionAssert, Position> {
    protected PositionAssert(Position actual) {
        super(actual, PositionAssert.class);
    }
    public PositionAssert hasX(int expectedX) {
        isNotNull();
        if (actual.x != expectedX) {
            failWithMessage("Expected X %d but was %d", expectedX, actual.x);
        }
        return this;
    }
    public PositionAssert hasY(int expectedY) {
        isNotNull();
        if (actual.y != expectedY) {
            failWithMessage("Expected Y %d but was %d", expectedY, actual.y);
        }
        return this;
    }
}
Pridáme statickú funkciu na elegantné získanie assertu:
public static PositionAssert assertThat(Position position) {
    return new PositionAssert(position);
}
A môžeme písať testy v štýle AssertJ:
@Test
public void testPosition() {
    var p = new Position(3, 4);
    assertThat(p).hasX(3)
                 .hasY(4);
}