I have a set of 10 unit tests. In reality it's jus...
# random
c
I have a set of 10 unit tests. In reality it's just 5 unit tests duplicated because I test with my prod url and my staging url. If I want to de-dupe them, whats the best way to do that in junit4? seems like paramterized tests might be what i want BUT parameterized tests make you define an input and output. i just want to change the inputs. Is there some other feature of junit I'm missing?
c
Use parameterized tests. There is no such thing as a parameterized test output. The parameters can be used for whatever the test requires.
c
Alright. I will go for that. FWIW, an example of my current test is
Copy code
@Test
fun `test prod`() = runBlocking {
        val apolloClient = ApolloClient.Builder()
            .serverUrl("my prod url")
            .build()

        val result =  ..
        assert(result.isSuccess)
}
and
Copy code
@Test
fun `test staging`() = runBlocking {
        val apolloClient = ApolloClient.Builder()
            .serverUrl("my staging url")
            .build()

        val result =  ..
        assert(result.isSuccess)
}
so I just would like to combine those two into 1
c
could drop them into a utility method that takes the server url as a paramter.
e
it's a bit more awkward in JUnit 4 than it is in JUnit 5, but
Copy code
@RunWith(Parameterized::class)
class MyTest(val serverUrl: String) {
    @Test
    fun test() = runBlocking {
        val apolloClient = ApolloClient.Builder()
            .serverUrl(serverUrl)
            .build()
    }

    companion object {
        @JvmStatic
        @Parameterized.Parameters(name = "{0}")
        fun data(): Iterable<Array<*>> = listOf(
            arrayOf("my prod url"),
            arrayOf("my staging url"),
        )
    }
}
JUnit 5's
Copy code
@ParameterizedTest
@ValueSource(strings = ["my prod url", "my staging url"]
fun test(serverUrl: String) {
    // ...
is more concise and more flexible, if you can migrate
c
oh hell yeah. junit 4's is awkware. while we're here... and it just took me 15 minutes to get dependencies squared away. what the heck is junit vs jupiter. lol
e
c
thanks @ephemient
FWIW, I was able to simplify the data() method a bit
Copy code
@JvmStatic
      @Parameterized.Parameters(name = "{0}")
      fun data(): Iterable<Any> = listOf(
        "my prod url",
        "my staging url",
      )
e
you need an array to wrap multiple parameters and I usually pass one specifically for test naming, but if that works then sure
e.g.
Copy code
class MyTest(ignoredName: String, val serverUrl: String) {
    companion object {
        @JvmStatic
        @Parameterized.Parameters(name = "{0}")
        fun data(): Iterable<Array<*>> = listOf(
            arrayOf("prod", "my prod url"),
            arrayOf("staging", "my staging url"),
        )
the
{0}
makes it shows up as
[prod]
or
[staging]
in the test report
if the argument you're passing is naturally identifiable by
toString
then it doesn't matter of course, and if you don't mind the default index-based naming, then I guess you don't need to bother either
c
oh. cool. i didn't notice that difference. i'll use that instead. cheers
e
Just as an aside, are you actually making requests to your server? If so, these are probably closer to integration tests, and not unit tests.
e
yeah good point, if that's the case I'd create separate test configurations for different environments instead
c
Yeah, they are not unit tests, but we would like to test e2e request being made + deserialization. "seaprate test configuration" hm. any docs you can point to for that? noob here that doesn't know what that means
k
this looks like environment specific thing, that should be "injected" to your app as regular external config
e
you can split it out from your unit tests and inject it into the test environment, e.g.
Copy code
plugins {
    `jvm-test-suite`
}
testing {
    suites {
        val integrationTest by registering(JvmTestSuite::class) {
            useJUnit()
            targets.all {
                testTask.configure {
                    systemProperty("serverUrl", project.property("serverUrl"))
                }
            }
        }
    }
}
with a
val serverUrl: String = System.getProperty("serverUrl")
lookup in
src/integrationTest/kotlin/*
and running with
./gradlew integrationTest -PserverUrl='my staging url'
or
-PserverUrl='my prod url'
, etc.
you could even hack together
Copy code
val integrationTest by sourceSets.creating
dependencies {
    integrationTest.implementationConfigurationName(kotlin("test-junit"))
}
testing {
    suites {
        val stagingIntegrationTest by registering(JvmTestSuite::class) {
            dependencies {
                implementation(files(integrationTest.runtimeClasspath))
            }
            useJUnit()
            targets.all {
                testTask.configure {
                    systemProperty("serverUrl", "my staging url")
                    testClassesDirs = files(testClassesDirs, integrationTest.output.classesDirs)
                }
            }
        }
        val prodIntegrationTest by registering(JvmTestSuite::class) {
            dependencies {
                implementation(files(integrationTest.runtimeClasspath))
            }
            useJUnit()
            targets.all {
                testTask.configure {
                    systemProperty("serverUrl", "my prod url")
                    testClassesDirs = files(testClassesDirs, integrationTest.output.classesDirs)
                }
            }
        }
    }
}
if you would rather have separate tests executions for different pre-defined environment using the same test classes
c
Thanks. is it silly to say that I want my test to just always test all of my 3 environments? i also setup a system property so that you can invoke this from command line, and give it a new url if you want. I will have to try to see the benefit of implementing the above vs my current route.
thanks though everyone. ive learned a ton. ❤️
d
E2E in a JUnit4/5 test is like a 🐇 🕳️ 🛣️ to🪦. E2E in any kind of non manual test to me smells like red flags.
e
I don't know about it being to be manual, but JUnit is called J*Unit* for a reason
c
if done properly - perhaps in a separate module, or separate application - JUnit makes a nice framework, along with something like RestAssured, to drive E2E tests. Just don’t try and shoehorn them in alongside unit tests.
☝️ 1
e
separate test suite within the same module is fine too, as Gradle's
jvm-test-suite
makes easy
👍 1
c
Yeah. I know that its sorta e2e (its not really like UI e2e test, but testing that a network call works and is deserialized properly catches like 90% of our bugs). so i dont really have any other thoughts on how to structure this. open to ideas.
e
sounds more like you need a way to share a JSON schema between projects; testing serialization/deserialization on live services is just a workaround
c
I guess, but it's moreso that the backend just gets into a completed effed up state and sends us back gibberish sometimes. so the schema hasn't changed, its just the backend being a pain.
i complain to backend devs and they're like "i see that that endpoint 200s. must be a client code issue"
but in reality its some deeply nested thing thats broken and breaks deserialization
e
Sounds like you might need unit tests on the backend 😅
d
Yeah. I know that its sorta e2e (its not really like UI e2e test, but testing that a network call works and is deserialized properly catches like 90% of our bugs). so i dont really have any other thoughts on how to structure this. open to ideas.
Would a more simple test that proves you can deserialize the expected JSON suffice? You could store the JSON (and variants of it even) in a file and then run it against the test. If it is just deserialization that is needed then you do not need to connect to the real server for that. A simple test like this could be used to prove you can handle proposed responses from an endpoint before the endpoint is even deployed and operational.
e
it's a sporadic backend problem, and you're expecting frontend tests to catch it? IMO that really should be the responsibility of the backend to validate its own output
d
I implying that the client should know if they can or can not handle valid and invalid JSON responses.
c
yea, but you can validate that internally w/o having a flaky server to talk to
💯 1
d
I have found this article to be very useful over the years: https://learn.microsoft.com/en-us/azure/architecture/patterns/anti-corruption-layer
I acknowledge this is talking about server development but the principals/pattern itself is platform agnostic.
User this pattern when:
• Two or more subsystems have different semantics, but still need to communicate.
e
Looking forward to reading that, since we practice defensive programming to defend ourselves from our backend team's style which is adversarial programming 😅
😆 1
c
Give offensive programming a try ;)
d
At first glance I looked at that and thought it was going to suggest to add cuss words to logs and do other nefarious things.
c
well, you can always still do that blob sweat smile
d
I will abstain lol
e
"the best defense is a good offense"
e
I would've thought it means that if the backend sends back invalid data, we should catch that, and in response send them a bunch of invalid requests.
😂 1
Maybe that's Mutually Assured Destruction programming
☝️ 1
c
seems like we’ve lowered ourselves to internal denial-of-service. MAD indeed!
💯 1
c
I 100% agree that backend tests should catch this etc. But they dont. and they dont write proper tests. ive brought it up with my manager etc. and it drives me crazy to get bug reports constantly opened for the android app. So by writing tests in the android codebase that I can execute... it basically allows me to do a full health check of the app without having to run the app and explore every leaf/screen of the app to find out that something stupid is broken again. I initially was trying to write critical path E2E UI tests to test this, but just checking the network layer specifically has had great results. i literally just run this every 15 minutes and ping myself on slack, which i then yell at the backend team that thier servicec messed up again.
i hate it
but it pays the bills.
e
Sounds like there are bigger issues that tests can't fix 😬
c
but tests can catch them though. and thats all i need. 😄