Kotlin: unit test approve object after gson

I have such a JUnit test:

Test fun testCategoriesLoading() { val subscriber = TestSubscriber<List<ACategory>>() service.categories().subscribe(subscriber) subscriber.awaitTerminalEvent() subscriber.assertNoErrors() } 

service Retrofit that uses GsonConverter to deserialize json in

 data class ACategory(val id: String, val title: String, val parentId: String?, val hasChildren: Boolean) 

copies.

The test passes even if the ACategory is filled with id = null, title = null, etc.

So, as far as I know, gson using reflection and kotlin lazily solve these uncertainty constraints on first access.

Is there any way to force this solution? Some nice solution without direct access to the fields manually? I really do not want to write each statement manually.

+5
source share
2 answers

You can use the new reflection of Kotlin. If you have an ACategory instance, call

 ACategory::class.memberProperties .filter { !it.returnType.isMarkedNullable } .forEach { assertNotNull(it.get(aCategory)) } 

to access all properties that are marked as invalid and claim to be non-zero. Make sure you have a reflection of lib in the classpath.

Make sure you use the M14.

+1
source

We ended up with a hack for data classes (for us, only the use case is used, therefore it is normal).

The gsonConstructedObject.copy() call shows all exceptions

0
source

All Articles