3

I have a test suite that open JSON files. The tests do not pass on emulator <= API 23 and work fine on newer API Level.

There are two different kinds of exceptions:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated string at line 1 column 1025 $.ajsonelement

and

com.google.gson.JsonSyntaxException: java.io.EOFException: End of input at line 1 column 1025 path $.ajsonelement.

The weird thing is that those files work fine when I run the app and open them manually in the simulator.

I'm using gson but the problem also appears with Moshi.

fstephany
  • 2,254
  • 3
  • 25
  • 32

1 Answers1

2

Found the issue. The json files were in moduleName/src/test/resources/configs/. I moved them to moduleName/src/test/assets/configs/

The code to load them was:

val assetConfig = InstrumentationRegistry 
            .getInstrumentation() 
            .context 
            .assets 
            .open(configFileName) 
val scanner = Scanner(assetConfig) 
return scanner.useDelimiter("\\Z").next() Charsets.UTF_8)) 

I replaced it with a Guava helper:

val assetConfig = InstrumentationRegistry
            .getInstrumentation()
            .context
            .assets
            .open(configFileName)
return CharStreams.toString(InputStreamReader(assetConfig, Charsets.UTF_8))

Everything loads fine for all emulator between API 18 and 26.

fstephany
  • 2,254
  • 3
  • 25
  • 32