https://kotlinlang.org logo
Title
s

snowe

06/13/2018, 4:28 PM
it's why groovy has been used for years. Your tests can be named better.
z

zpearce

06/13/2018, 4:30 PM
@karelpeeters I am curious about the spaces in test names hate. A short, readable sentence describing the test seems like a good idea to me.
r

rook

06/13/2018, 4:32 PM
Spek lets you do that. I love it
k

karelpeeters

06/13/2018, 4:34 PM
Tests are just functions like any other, so I don't like breaking the naming convention. We're all used to camelCase by now, so it's not that much more difficult to read. Short explanations can go in comments or in the KDoc, where you're not constrained to "one short sentence".
And bit of a subjective thing but the backticks look rediculously ugly.
z

zpearce

06/13/2018, 4:35 PM
I tend to agree with that actually. I only use them for test names though, and kotlin keywords imported from java, ofc
s

snowe

06/13/2018, 4:38 PM
you shouldn't treat tests like other functions. They'll never be called on their own. They're meant to be a Spec (hence the name Spek) and as such they should describe what is being tested.
what's easier to read?
fun testGetAllEmploymentGapForAnApplicant_ApplicantIdNotFound_ThrowsNotFoundException() {
fun `Get of all Employment Gaps for an Applicant when ApplicantId not found throws NotFoundException`() {
r

rook

06/13/2018, 4:40 PM
The one with red text is easier to read 🤔 (just teasing)
😂 1
s

snowe

06/13/2018, 4:40 PM
the first is an actual test name in our code base that somebody translated when converting from java.
r

rook

06/13/2018, 4:41 PM
You should check out Spek, you can have nice names like that with having funky function names
s

snowe

06/13/2018, 4:43 PM
We've tried to switch to spek.
it still doesn't work great with spring sadly.
and in spek it's still a string, but it's instead it's a parameter to a function. If you don't like the backtick naming then I doubt you'd like spek's naming conventions
k

karelpeeters

06/13/2018, 4:45 PM
I don't really get what's being tested here, is there like a
database.getAllEmploymentGapForApplicant(applicantId)
?
s

snowe

06/13/2018, 4:45 PM
yes.
the naming's terrible, I'm just pointing out that the longer a test name gets the more ridiculous camel case is.
k

karelpeeters

06/13/2018, 4:46 PM
I'd argue you need to include the exact function name in the test though, don't try to rewrite that part with some spaces.
That's just asking for trouble when that function gets renamed.
Then you can't do much better than what you already have, maybe split out the
throws
part:
@Throws(NotFoundException::class)
fun getAllEmploymentGapForAnApplicant_ApplicantIdNotFound() {}
s

snowe

06/13/2018, 4:50 PM
tests should describe what they're testing not the function they're testing.
here's the exact test
@Test(expected = NotFoundException::class)
    @Throws(Exception::class)
    fun testGetAllEmploymentGapForAnApplicant_ApplicantIdNotFound_ThrowsNotFoundException() {
        val employmentGaps = Lists.newArrayList(employmentGap())

        whenever(applicantDataService.findOne(applicantId)).thenReturn(Optional.empty())

        val allEmploymentGapsForAnApplicant = employmentDataService.getAllEmploymentGapsForAnApplicant(applicantId)

        assertNotNull(allEmploymentGapsForAnApplicant)
        assertFalse(allEmploymentGapsForAnApplicant.isEmpty())
        allEmploymentGapsForAnApplicant.forEach { dto -> verify(dto, findEmploymentGapById(employmentGaps, dto.employmentGapId)) }

    }
Of course the point of this discussion isn't this test. You can pick apart any bit of code. What happens when you get a test that calls multiple functions, it's an integration test. or it handles calls to a db to persist items and then check logic around reading it out of the db. Are you going to add every function call in to the name?
k

karelpeeters

06/13/2018, 5:23 PM
You give it a short name without spaces, and you expand on that as needed in the KDoc.
s

snowe

06/13/2018, 5:31 PM
You're putting doc comments on your tests? What happens when the test fails in your CI build?
k

karelpeeters

06/13/2018, 5:33 PM
I rarely do, I just go look up the test name in the source and go from there.