Lilly
11/09/2021, 3:12 PMMervyn McCreight
11/09/2021, 3:14 PMGradle
- maybe the java-test-fixtures
plugin is something you are looking for:share code between my local unit tests
? I interpreted that as sharing code between tests in src/test/kotlin
and src/test/java
🤔Lilly
11/09/2021, 3:23 PMclass LegacyDeserializationTest : WordSpec({
val parameterPacketHexString = """
00 00 00 00 00 00 00 00 00 00 32 31 33....
""".trimIndent().replace(" ","").replace("\n", "")
val csvReaderCtx: CsvReaderContext.() -> Unit = {
charset = Charsets.ISO_8859_1
}
fun readTestFile(
fileName: String,
ctx: CsvReaderContext.() -> Unit
): Sequence<Map<String, String>> {
val file = File("src/test/resources/$fileName")
return csvReader(ctx).parseLinesAsSequence(file.inputStream())
}
"legacy parser" should {
"not throw Exception on deserialization" {
val contents = readTestFile("/webkey-details.csv", csvReaderCtx).asSequence()
val details = csvMapper<LegacyDetailModel> {
ignoreUnknownFields = true
// Actual field name of input to class field name
customKeyMap = mutableMapOf(
"controlType" to "composableType",
"default" to "defaultValue",
"dataType2" to "dataType",
"sinceVersion2" to "sinceVersion",
"untilVersion2" to "untilVersion",
"deprecated2" to "deprecated"
)
}.deserialize(contents)
...
}
Basically all of this code is needed for serialization toowasyl
11/09/2021, 3:29 PMLilly
11/09/2021, 3:30 PMwasyl
11/09/2021, 3:31 PM@Test
annotation, everything else is just regular code.
Of course tests should be simple so the sharing should be done sparingly (you don’t want to have to write tests for you tests)Lilly
11/09/2021, 3:37 PMdetails
variable because I need real world data to test deserialization and details
is a dependency of the Parser.deserialize
function:
fun deserialize(
source: ByteArray,
packetDetails: Sequence<LegacyDetailModel>
): WebKeys
wasyl
11/09/2021, 3:59 PMcsvReader(ctx).parseLinesAsSequence(file.inputStream())
is part of another production class that’s tested separately, then I’d test it as part of the whole deserialization test. That way the only input to the tests would be a File
(or a file name)Lilly
11/09/2021, 4:12 PMCsvParserTest
file. Did I understand you right that I should drop this file and do the testing of the parser in the deserialization test?
I have another question that comes in my mind: When doing the deserialization I produce values which I need for serialization. So the serialization test would base on the deserialization test. Is this legit? And if not how can I work around this? The problem here is that I need all values from deserialization to test serialization properly, so I can't just create a map with 2 or 3 values. Another problem is that the deserialize function is wrapped into shouldThrow
so I can't write
shouldNotThrow<Exception> {
webkeys = LegacyParser.deserialize(
source = bytes,
packetDetails = packetDetails
)
}
But I could do
shouldNotThrow<Exception> {
LegacyParser.deserialize(
source = bytes,
packetDetails = packetDetails
).also { webKeys = it }
}
Anyway there is no guarantee that the variable is set. Should tests base on other tests? I guess no. But what options do I have otherwise?wasyl
11/09/2021, 4:18 PMDid I understand you right that I should drop this file and do the testing of the parser in the deserialization test?That’s up to you of course, but I would consider that. I mean if I could easily create
val parser = LegacyParser(SomeDependency(), SomeOtherDependency())
then I’d do that, and test both dependencies + parser at the same time. All in all they’re clearly designed to work together, right?When doing the deserialization I produce values which I need for serialization. So the serialization test would base on the deserialization test. Is this legit?That’s what I usually end up doing when testing serialization, but I just do
original shouldBe deserialize(serialize(original))
. Right now your tests only check that the serialization/deserialization functionality doesn’t throw, which doesn’t say much about whether the serialization is valid. But checking if x == deserialize(serialize(x))
may be enough, unless you expect some specific format that goes to an external service for exampleLilly
11/09/2021, 4:24 PM