Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AndroidThreeTen not working in unit test without robolectric?

I'm having trouble creating a unit test without needing robolectric. I am using AndroidThreeTen.init(this) in my code and when I run my test if I disable robolectric I get an error: org.threeten.bp.zone.ZoneRulesException: No time-zone data files registered

and if I leave it enabled I get this: [Robolectric] com.mycomp.,yapp.utilities.log.LogTest.on Calling function w it returns an Int: sdk=28; resources=BINARY

I have tried using testImplementation ‘com.jakewharton.threetenabp:threetenabp:1.1.0’ made no difference. I have AndroidThreeTen.init(this) called in my application and testApplication. any ideas? this is my test

@Test
    fun `on Calling function i it returns an Int`() {
        assertThat("Returned class is not an Int", Log.i("Test", "Test"), isA(Int::class.java))
        assertThat("Returned Int is not 0", Log.i("Test", "Test"), `is`(0))
    }

Or do I have to use robolectric because of this? (Side note: Log is not the util.log from android but my own class) (edited)

like image 789
Crash1hd Avatar asked Mar 25 '19 14:03

Crash1hd


People also ask

What do you have to avoid in tests in unit testing?

Avoid Logic in Tests Doing so reduces the chance of introducing bugs into the test. The focus must remain on the end result, not on circumventions of implementation details. Without too many conditions, tests are also likely to be deterministic.

What makes a unit test self-validating?

Tests must be self-validating. This means each test must be able to determine if the actual output is according to the expected output. This determines if the test is passed or failed. There must be no manual interpretation of results.

Should unit test cover all cases?

It isn't realistic -- or necessary -- to expect 100% code coverage through unit tests. The unit tests you create depend on business needs and the application or applications' complexity. Aim for 95% or higher coverage with unit tests for new application code.

How do you automatically generate unit tests?

To generate unit tests, your types must be public. Open your solution in Visual Studio and then open the class file that has methods you want to test. Right-click on a method and choose Run IntelliTest to generate unit tests for the code in your method. IntelliTest runs your code many times with different inputs.


1 Answers

JVM unit tests don't run on Android runtime. Instead of ThreeTenABP, you can just use ThreeTenBP directly to get the same API initialised for a regular JVM.

In my project build.gradle I use a setup like:

implementation "com.jakewharton.threetenabp:threetenabp:${threetenabpVersion}"
testImplementation("org.threeten:threetenbp:${threetenbpVersion}") {
    exclude module: "com.jakewharton.threetenabp:threetenabp:${threetenabpVersion}"
}

where

threetenabpVersion = '1.2.0'
threetenbpVersion = '1.3.8'

This uses ThreeTenBP via ThreeTenABP normally, but in unit test configuration it adds TreeTenBP directly as a dependency, with its init code. Cannot remember exactly why I put in the exclude rule; it's been like that for a few years already.

like image 90
laalto Avatar answered Oct 02 '22 16:10

laalto