Last active
December 27, 2017 01:32
-
-
Save b1a9id/77478275cd3709adb5e63dfeb97b2817 to your computer and use it in GitHub Desktop.
基本的なアサーション
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 com.example.core.model.*; | |
import org.junit.jupiter.api.*; | |
import static org.junit.jupiter.api.Assertions.*; | |
import static org.junit.jupiter.api.Assumptions.*; | |
class StandardAssertionTest { | |
@Nested | |
class DependentAssertions { | |
private Person person; | |
@BeforeEach | |
void init() { | |
person = new Person("Ryosuke", "Uchitate", 27, Gender.MAN); | |
} | |
@Test | |
void nameAssertions() { | |
assertAll("This is name test.", | |
() -> assertEquals("Ryosuke", person.getFirstName()), | |
() -> assertEquals("Uchitate", person.getLastName()) | |
); | |
} | |
@Test | |
void propertiesAssertions() { | |
assertAll("properties", | |
() -> { | |
String firstName = person.getFirstName(); | |
assertNotNull(firstName); | |
assertAll("firstName", | |
() -> assertTrue(firstName.startsWith("R")), | |
() -> assertFalse(firstName.endsWith("i")) | |
); | |
}, | |
() -> { | |
String lastName = person.getLastName(); | |
assertNotNull(lastName); | |
assertAll("lastName", | |
() -> assertTrue(lastName.startsWith("U")), | |
() -> assertFalse(lastName.endsWith("i")) | |
); | |
}, | |
() -> { | |
assertAll("age", | |
() -> assertTrue(person.getAge() > 0), | |
() -> assertFalse(person.getAge() < 0) | |
); | |
}, | |
() -> { | |
Gender gender = person.getGender(); | |
assertNotNull(gender); | |
assertAll("gender", | |
() -> assertTrue(gender == Gender.MAN), | |
() -> assertFalse(gender == Gender.WOMAN) | |
); | |
} | |
); | |
} | |
} | |
@Nested | |
class ExceptionAssertions { | |
@Test | |
void exceptionTest() { | |
Throwable exception = assertThrows( | |
IllegalArgumentException.class, () -> Gender.valueOf("TEST") | |
); | |
assertEquals(exception.getMessage(), "No enum constant com.example.core.model.Gender.TEST"); | |
} | |
} | |
@Nested | |
class FailAssertions { | |
@Test | |
void failTest() { | |
fail("This is fail test."); | |
} | |
} | |
@Nested | |
class AssumptionAssertions { | |
@Test | |
void validTest() { | |
assumeTrue("/Users/uchitate".equals(System.getenv("HOME"))); | |
} | |
@Test | |
void invalidTest() { | |
assumeFalse("/Users/uchitate".equals(System.getenv("HOME")), () -> "This is invalid."); | |
} | |
@Test | |
void executeTestWhenValid() { | |
assumeTrue("/Users/uchitate".equals(System.getenv("HOME"))); | |
assertTrue("test".length() == 4); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment