Created
July 24, 2017 10:25
-
-
Save TonyHaddad91/ef934998fb8330f36aa6eac1974e35f2 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
class ExampleUnitTest { | |
lateinit var nonNullBook: Book | |
/** | |
* This method will be called before each test we have | |
*/ | |
@Before | |
fun thisMethodShouldBeCalledBeforeEachTestCall() { | |
println("Before Test method called") | |
nonNullBook = Book(300) | |
} | |
/** | |
* This method will be called after each test we have | |
*/ | |
@After | |
fun thisMethodShouldBeCalledAfterEachTestCall() { | |
println("After Test method called") | |
} | |
/** | |
* Here we used the default value of the book type so we won't get any addition | |
* on the book price | |
* 10*20=200 that's our expected price | |
*/ | |
@Test | |
fun calculateBookPrice_WhenSoftCopy(): Unit { | |
var book: Book = Book(10, 20) | |
assertEquals(200, book.calculateBookPrice()) | |
} | |
/** | |
* Here we used the default value of the book type so we won't get any addition | |
* on the book price | |
* 10*20=200+20=200 that's our expected price | |
* You can also add an failure message as first parameter for any assert method | |
*/ | |
@Test | |
fun calculateBookPrice_WhenHardCopy(): Unit { | |
var book: Book = Book(10, 20, Type.HARD_COPY) | |
assertEquals("Fail expected price is 220", 220, book.calculateBookPrice()) | |
} | |
/** | |
* Here we need to check isACookBook | |
* and we are assuming that any book with less than 200 pages is a CookBook | |
* so we will create a book with 100 page and we should verify that the result should be true | |
*/ | |
@Test | |
fun isACookBook_WhenLessThan201_ThenShouldBeTrue(): Unit { | |
var book: Book = Book(100) | |
assertTrue(book.isACookBook()) | |
} | |
@Test | |
fun isACookBook_WhenMoreThan200_ThenShouldBeFalse(): Unit { | |
var book: Book = Book(500) | |
assertFalse(book.isACookBook()) | |
} | |
/** | |
* In this test we are excepting RunTimeException to occur | |
* if not this test will fail | |
*/ | |
@Test(expected = RuntimeException::class) | |
fun thisTestWillExpectARunTimeExceptionToOccur(): Unit { | |
var book = Book(-5) | |
} | |
/** | |
* In this test we are adding maximum execution time | |
* and here this test will fail because while(true) will take infinite time | |
*/ | |
@Ignore | |
@Test(timeout = 2000) | |
fun thisMethodShouldFailIfTheExecutionTimeMoreThan2Sec(): Unit { | |
while (true) { | |
} | |
} | |
/** | |
* This test will check if the nonNullBook is null or not | |
* and this test will pass because we are | |
*/ | |
@Test | |
fun verifyThatTheObjectIsNotNull() { | |
assertNotNull(nonNullBook) | |
} | |
@Test | |
fun verifyThatTheObjectIsNull(): Unit { | |
var book: Book? = null | |
assertNull(book) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment