Skip to content

Instantly share code, notes, and snippets.

View ArunYogeshwaran's full-sized avatar
🌎
Office

Arun ArunYogeshwaran

🌎
Office
  • Zenjob. Ex-VMware
  • Berlin
View GitHub Profile
// A test with relevant details abstracted out.
@Test
fun `get ML recommended jobs with new user expect predefined list`() {
// Test arrangements and setup.
val engine = new RecommendationEngine(getEnvDetails(), getLocalInfo(), getSessionProperties())
val jobs = engine.getJobs(getTestUserInfo())
assertThat(jobs).isEqualTo(expectedJobs)
}
@ArunYogeshwaran
ArunYogeshwaran / StateVsInteractionTesting.kt
Created July 6, 2023 14:02
The example below shows a system that toggles the "favorite" state of a given job in the cache.
// Unit under test.
fun toggleFavoriteForJob(job: Job) {
if (job.isFavorite) {
cache.unfavorite(job)
} else {
cache.favorite(job)
}
}
// A unit test using interaction testing.
@ArunYogeshwaran
ArunYogeshwaran / FriendReferral.kt
Created July 6, 2023 14:04
The example below shows a class that contains the logic of a feature in an app where a user can invite another user based on certain internal conditions.
fun referFriend(userId: String, referral: Referral) {
if (isReferralValid(referral)) {
processReferral(userId)
}
}
private fun isReferralValid(referral: Referral): Boolean {
  // Checks the validity of the referral.
}
// The condition here is that the private method is made public or package-private using the `VisibleForTesting` annotation.
@Test
fun `is referral valid with new referral expected value is true`() {
val friendReferral = getInstance()
asserTrue(friendReferral.isReferralValid(…))
}
// The condition here is that the private method is made public or package-private using the `VisibleForTesting` annotation.
@Test
fun `process referral valid with new referral verify referral successful`() {
@Test
fun `refer friend with new referral verify referral successful`() {
val friendReferral = getInstance()
friendReferral.referFriend("testUserId", getStaleReferral())
assertThat(referralList.contains("testUserId")).isTrue()
}
@Test
fun `refer friend with stale referral verify referral failue`() {
val friendReferral = getInstance()
@ArunYogeshwaran
ArunYogeshwaran / AudioPlayer.kt
Created July 6, 2023 14:08
Behavior-driven tests can be easily understood by someone who's familiar with the system requirements. They also serve as indirect documentation of the code being tested, and they help in reducing the size of a test method, as only one behavior is tested at any time, even though the method under test does many things.
fun playArtist(artist: Artist) {
if (artist != Artist.TAYLOR_SWIFT) {
return IllegalArgException()
}
addToRecentlyPlayed(artist)
player.play(artist.getRandomSong())
}