This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void assertJ() throws Exception { | |
assertThatThrownBy(this::methodThrowsIllegalArgument) | |
.isInstanceOf(IllegalArgumentException.class) | |
.hasMessage("Invalid Argument"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Rule | |
public ExpectedException thrownException = ExpectedException.none(); | |
@Test | |
public void exceptionRule() { | |
thrownException.expect(IllegalArgumentException.class); | |
thrownException.expectMessage("Invalid Argument"); | |
methodThrowsIllegalArgument(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.assertj.core.api.Assertions.assertThatThrownBy; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.junit.Assert.assertThat; | |
import static org.junit.Assert.fail; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.ExpectedException; | |
public class ExceptionTestingTest { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test(expected = IllegalArgumentException.class) | |
public void expected() { | |
methodThrowsIllegalArgument(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void tryCatch() { | |
try { | |
methodThrowsIllegalArgument(); | |
fail("Expected IllegalArgumentException!"); | |
} catch (IllegalArgumentException exception) { | |
assertThat(exception.getMessage(), is("Invalid Argument")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Rule | |
public JUnitSoftAssertions softly = new JUnitSoftAssertions(); | |
String openKnowledge = "offen, kundig, gut!"; | |
@Test | |
public void exampleNormalAssertion() { | |
assertThat(openKnowledge).startsWith("kundig").endsWith("offen"); | |
// -> java.lang.AssertionError: Expecting: <"offen, kundig, gut!"> to start with: <"kundig"> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void fileAssertion() { | |
File file = createFile("openKnowledge.txt", "offen, kundig, gut!"); | |
assertThat(file).exists().hasExtension("txt").isFile().isRelative(); | |
assertThat(contentOf(file)).startsWith("offen").contains("kundig").endsWith("gut!"); | |
} |