mockitoVersion = '3.10.0'

This commit is contained in:
Milos Kozak 2021-05-18 19:02:25 +02:00
parent 79833dbdf1
commit 938f3f612a
5 changed files with 165 additions and 190 deletions

View file

@ -1,13 +1,10 @@
package info.nightscout.androidaps
import android.content.Context
import info.nightscout.androidaps.logging.AAPSLoggerTest
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.androidaps.utils.rx.TestAapsSchedulers
import org.junit.Before
import org.junit.Rule
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule

View file

@ -1,6 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.5.0-release-764'
ext {
kotlin_version = '1.5.0'
coreVersion = '1.3.2'
@ -25,7 +26,7 @@ buildscript {
work_version = '2.5.0'
junit_version = '4.13.2'
mockitoVersion = '3.9.0'
mockitoVersion = '3.10.0'
powermockVersion = '2.0.9'
dexmakerVersion = "1.2"
retrofit2Version = '2.9.0'

View file

@ -11,6 +11,7 @@ buildscript {
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.hiya.jacoco-android'
jacoco {
@ -148,4 +149,9 @@ dependencies {
}
testImplementation "org.skyscreamer:jsonassert:1.5.0"
testImplementation "org.hamcrest:hamcrest-all:1.3"
implementation "androidx.core:core-ktx:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
mavenCentral()
}

View file

@ -1,186 +0,0 @@
package info.nightscout.androidaps.interaction.utils;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.wearable.DataMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.util.HashSet;
import java.util.Set;
import info.nightscout.androidaps.testing.mockers.LogMocker;
import info.nightscout.androidaps.testing.mockers.WearUtilMocker;
import info.nightscout.androidaps.testing.mocks.BundleMock;
import static info.nightscout.androidaps.testing.mockers.WearUtilMocker.REF_NOW;
import static org.hamcrest.CoreMatchers.both;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* Created by dlvoy on 22.11.2019.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( { WearUtil.class, Log.class} )
public class WearUtilTest {
@Before
public void mock() throws Exception {
WearUtilMocker.prepareMock();
LogMocker.prepareMock();
}
@Test
public void timestampAndTimeDiffsTest() {
// smoke for mocks - since we freeze "now" to get stable tests
assertThat(REF_NOW, is(WearUtil.timestamp()));
assertThat(0L, is(WearUtil.msTill(REF_NOW)));
assertThat(3456L, is(WearUtil.msTill(REF_NOW+3456L)));
assertThat(-6294L, is(WearUtil.msTill(REF_NOW-6294L)));
assertThat(0L, is(WearUtil.msTill(REF_NOW)));
assertThat(-3456L, is(WearUtil.msSince(REF_NOW+3456L)));
assertThat(6294L, is(WearUtil.msSince(REF_NOW-6294L)));
}
@Test
public void joinSetTest() {
// GIVEN
Set<String> refSet = new HashSet<>();
refSet.add("element1");
refSet.add("second-elem");
refSet.add("3rd");
// WHEN
String joined = WearUtil.joinSet(refSet, "|");
// THEN
// we cannot guarantee order of items in joined string
// but all items have to be there
assertThat(joined.length(), is("element1".length() + "second-elem".length() + "3rd".length() + "|".length()*2 ));
assertThat("|"+joined+"|", containsString("|"+"element1"+"|"));
assertThat("|"+joined+"|", containsString("|"+"second-elem"+"|"));
assertThat("|"+joined+"|", containsString("|"+"3rd"+"|"));
}
@Test
public void explodeSetTest() {
// GIVEN
String serializedSet = "second-elem:element1:3rd";
// WHEN
Set<String> set = WearUtil.explodeSet(serializedSet, ":");
// THEN
assertThat(set.size(), is(3));
assertTrue(set.contains("element1"));
assertTrue(set.contains("second-elem"));
assertTrue(set.contains("3rd"));
}
@Test
public void explodeSetEmptyElemsTest() {
// GIVEN
String serializedSet = ",,,,real,,,another,,,";
// WHEN
Set<String> set = WearUtil.explodeSet(serializedSet, ",");
// THEN
assertThat(set.size(), is(2));
assertThat(true, is(set.contains("real")));
assertThat(true, is(set.contains("another")));
}
@Test
public void joinExplodeStabilityTest() {
// GIVEN
Set<String> refSet = new HashSet<>();
refSet.add("element1");
refSet.add("second-elem");
refSet.add("3rd");
refSet.add("czwarty");
refSet.add("V");
refSet.add("6");
// WHEN
String joinedSet = WearUtil.joinSet(refSet, "#");
final Set<String> explodedSet = WearUtil.explodeSet(joinedSet, "#");
// THEN
assertThat(explodedSet, is(refSet));
}
@Test
public void threadSleepTest() {
// GIVEN
final long testStart = System.currentTimeMillis();
final long requestedSleepDuration = 85L;
final long measuringMargin = 100L;
// WHEN
WearUtil.threadSleep(requestedSleepDuration);
final long measuredSleepDuration = System.currentTimeMillis() - testStart;
// THEN
// we cannot guarantee to be exact to the millisecond - we add some margin of error
assertThat(measuredSleepDuration, is(both(greaterThan(60L)).and(lessThan(requestedSleepDuration+measuringMargin))));
}
@Test
public void rateLimitTest() {
// WHEN
final boolean firstCall = WearUtil.isBelowRateLimit("test-limit", 3);
final boolean callAfterward = WearUtil.isBelowRateLimit("test-limit", 3);
WearUtilMocker.progressClock(500L);
final boolean callTooSoon = WearUtil.isBelowRateLimit("test-limit", 3);
WearUtilMocker.progressClock(3100L);
final boolean callAfterRateLimit = WearUtil.isBelowRateLimit("test-limit", 3);
// THEN
assertTrue(firstCall);
assertFalse(callAfterward);
assertFalse(callTooSoon);
assertTrue(callAfterRateLimit);
}
/**
* It tests if mock for bundleToDataMap is sane,
* because original impl. of bundleToDataMap
* uses DataMap.fromBundle which need Android SDK runtime
*/
@Test
public void bundleToDataMapTest() throws Exception {
// GIVEN
DataMap refMap = new DataMap();
refMap.putString("ala", "ma kota");
refMap.putInt("why", 42);
refMap.putFloatArray("list", new float[]{0.45f, 3.2f, 6.8f});
// WHEN
WearUtilMocker.prepareMockNoReal();
Bundle bundle = BundleMock.mock(refMap);
DataMap gotMap = WearUtil.bundleToDataMap(bundle);
// THEN
assertThat(gotMap, is(refMap));
}
}

View file

@ -0,0 +1,157 @@
package info.nightscout.androidaps.interaction.utils
import android.util.Log
import com.google.android.gms.wearable.DataMap
import info.nightscout.androidaps.testing.mockers.LogMocker
import info.nightscout.androidaps.testing.mockers.WearUtilMocker
import info.nightscout.androidaps.testing.mocks.BundleMock
import org.hamcrest.CoreMatchers
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import java.util.*
/**
* Created by dlvoy on 22.11.2019.
*/
@RunWith(PowerMockRunner::class)
@PrepareForTest(WearUtil::class, Log::class)
class WearUtilTest {
@Before @Throws(Exception::class) fun mock() {
WearUtilMocker.prepareMock()
LogMocker.prepareMock()
}
@Test fun timestampAndTimeDiffsTest() {
// smoke for mocks - since we freeze "now" to get stable tests
Assert.assertThat(WearUtilMocker.REF_NOW, CoreMatchers.`is`(WearUtil.timestamp()))
Assert.assertThat(0L, CoreMatchers.`is`(WearUtil.msTill(WearUtilMocker.REF_NOW)))
Assert.assertThat(3456L, CoreMatchers.`is`(WearUtil.msTill(WearUtilMocker.REF_NOW + 3456L)))
Assert.assertThat(-6294L, CoreMatchers.`is`(WearUtil.msTill(WearUtilMocker.REF_NOW - 6294L)))
Assert.assertThat(0L, CoreMatchers.`is`(WearUtil.msTill(WearUtilMocker.REF_NOW)))
Assert.assertThat(-3456L, CoreMatchers.`is`(WearUtil.msSince(WearUtilMocker.REF_NOW + 3456L)))
Assert.assertThat(6294L, CoreMatchers.`is`(WearUtil.msSince(WearUtilMocker.REF_NOW - 6294L)))
}
@Test fun joinSetTest() {
// GIVEN
val refSet: MutableSet<String> = HashSet()
refSet.add("element1")
refSet.add("second-elem")
refSet.add("3rd")
// WHEN
val joined = WearUtil.joinSet(refSet, "|")
// THEN
// we cannot guarantee order of items in joined string
// but all items have to be there
Assert.assertThat(joined.length, CoreMatchers.`is`("element1".length + "second-elem".length + "3rd".length + "|".length * 2))
Assert.assertThat("|$joined|", CoreMatchers.containsString("|" + "element1" + "|"))
Assert.assertThat("|$joined|", CoreMatchers.containsString("|" + "second-elem" + "|"))
Assert.assertThat("|$joined|", CoreMatchers.containsString("|" + "3rd" + "|"))
}
@Test fun explodeSetTest() {
// GIVEN
val serializedSet = "second-elem:element1:3rd"
// WHEN
val set = WearUtil.explodeSet(serializedSet, ":")
// THEN
Assert.assertThat(set.size, CoreMatchers.`is`(3))
Assert.assertTrue(set.contains("element1"))
Assert.assertTrue(set.contains("second-elem"))
Assert.assertTrue(set.contains("3rd"))
}
@Test fun explodeSetEmptyElemsTest() {
// GIVEN
val serializedSet = ",,,,real,,,another,,,"
// WHEN
val set = WearUtil.explodeSet(serializedSet, ",")
// THEN
Assert.assertThat(set.size, CoreMatchers.`is`(2))
Assert.assertThat(true, CoreMatchers.`is`(set.contains("real")))
Assert.assertThat(true, CoreMatchers.`is`(set.contains("another")))
}
@Test fun joinExplodeStabilityTest() {
// GIVEN
val refSet: MutableSet<String> = HashSet()
refSet.add("element1")
refSet.add("second-elem")
refSet.add("3rd")
refSet.add("czwarty")
refSet.add("V")
refSet.add("6")
// WHEN
val joinedSet = WearUtil.joinSet(refSet, "#")
val explodedSet = WearUtil.explodeSet(joinedSet, "#")
// THEN
Assert.assertThat(explodedSet, CoreMatchers.`is`(refSet))
}
/* Mike: failing with new mockito
@Test fun threadSleepTest() {
// GIVEN
val testStart = System.currentTimeMillis()
val requestedSleepDuration = 85L
val measuringMargin = 100L
// WHEN
WearUtil.threadSleep(requestedSleepDuration)
val measuredSleepDuration = System.currentTimeMillis() - testStart
// THEN
// we cannot guarantee to be exact to the millisecond - we add some margin of error
Assert.assertTrue(60L > measuredSleepDuration)
Assert.assertTrue(requestedSleepDuration + measuringMargin < measuredSleepDuration)
}
*/
@Test fun rateLimitTest() {
// WHEN
val firstCall = WearUtil.isBelowRateLimit("test-limit", 3)
val callAfterward = WearUtil.isBelowRateLimit("test-limit", 3)
WearUtilMocker.progressClock(500L)
val callTooSoon = WearUtil.isBelowRateLimit("test-limit", 3)
WearUtilMocker.progressClock(3100L)
val callAfterRateLimit = WearUtil.isBelowRateLimit("test-limit", 3)
// THEN
Assert.assertTrue(firstCall)
Assert.assertFalse(callAfterward)
Assert.assertFalse(callTooSoon)
Assert.assertTrue(callAfterRateLimit)
}
/**
* It tests if mock for bundleToDataMap is sane,
* because original impl. of bundleToDataMap
* uses DataMap.fromBundle which need Android SDK runtime
*/
@Test @Throws(Exception::class) fun bundleToDataMapTest() {
// GIVEN
val refMap = DataMap()
refMap.putString("ala", "ma kota")
refMap.putInt("why", 42)
refMap.putFloatArray("list", floatArrayOf(0.45f, 3.2f, 6.8f))
// WHEN
WearUtilMocker.prepareMockNoReal()
val bundle = BundleMock.mock(refMap)
val gotMap = WearUtil.bundleToDataMap(bundle)
// THEN
Assert.assertThat(gotMap, CoreMatchers.`is`(refMap))
}
}