Skip to content

Commit

Permalink
Merge pull request #160 from deepueg/unit-test-nav-util
Browse files Browse the repository at this point in the history
Unit tests for NavUtil and Route
  • Loading branch information
deepueg authored Oct 12, 2020
2 parents ff033a6 + 8f3a2dd commit 06819f3
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
8 changes: 8 additions & 0 deletions android/lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,26 @@ android {
}
debug {
multiDexEnabled true
testCoverageEnabled true
}
}
lintOptions {
abortOnError false
}
testOptions {
unitTests.returnDefaultValues = true
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
api ('com.walmartlabs.ern:container-movies-reloaded:1.0.0') {changing=true}
testImplementation 'androidx.test:core:1.3.0'
testImplementation 'androidx.test.ext:junit:1.1.2'
testImplementation 'com.google.truth:truth:1.0.1'
testImplementation 'org.robolectric:robolectric:4.3.1'
androidTestImplementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
androidTestImplementation 'androidx.multidex:multidex:2.0.0'
androidTestImplementation 'androidx.test:core:1.3.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.ern.api.impl.navigation;

import android.os.Bundle;

import com.ernnavigationApi.ern.model.NavigationBar;

import org.json.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import static com.google.common.truth.Truth.assertThat;

@RunWith(RobolectricTestRunner.class)
public class NavUtilTest {

@Test
public void testGetPathReturnsNullForInValidBundle() {
Bundle b = new Bundle();
assertThat(NavUtils.getPath(b)).isNull();
b.putString("someKey", "someValue");
assertThat(NavUtils.getPath(b)).isNull();
b.putString("path", "validPath");
assertThat(NavUtils.getPath(b)).isEqualTo("validPath");
}

@Test
public void testGetPayload() {
Bundle b = new Bundle();
assertThat(NavUtils.getPayload(b)).isNull();
b.putString("jsonPayload", "badJson");
assertThat(NavUtils.getPayload(b)).isNull();
b.putString("jsonPayload", "{}");
assertThat(NavUtils.getPayload(b)).isInstanceOf(JSONObject.class);
}

@Test
public void testGetNavBar() {
Bundle b = new Bundle();
Bundle navBar = new NavigationBar.Builder("NavBar").build().toBundle();
assertThat(NavUtils.getNavBar(b)).isNull();
b.putString("navigationBar", "badJson");
assertThat(NavUtils.getNavBar(b)).isNull();
b.putString("navigationBar", "{}");
assertThat(NavUtils.getNavBar(b)).isNull();
b.putBundle("navigationBar", navBar);
assertThat(NavUtils.getNavBar(b)).isInstanceOf(NavigationBar.class);
navBar.remove("title"); //Remove required parameter
assertThat(NavUtils.getNavBar(b)).isNull();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.ern.api.impl.navigation;

import android.os.Bundle;

import androidx.annotation.NonNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import static com.google.common.truth.Truth.assertThat;

@RunWith(RobolectricTestRunner.class)
public class RouteTest {

@Test
public void testRoute() {
Route route = new Route.Builder(new Bundle()).build();
assertThat(route).isNotNull();
assertThat(route.getArguments().size()).isEqualTo(0);
assertThat(route.getActionId()).isEqualTo(0);
assertThat(route.isCompleted()).isFalse();
}

@Test
public void testRoutingNotifierSuccess() {
RoutingNotifier notifier = new RoutingNotifier() {
@Override
public void routingComplete(@NonNull RoutingResult result) {
assertThat(result.isComplete).isTrue();
assertThat(result.message).isNull();
}
};
Route route = new Route.Builder(new Bundle()).routingNotifier(notifier).build();
route.setResult(true, null);
}

@Test
public void testRoutingNotifierFailure() {
final String FAILURE_MESSAGE = "some message";
RoutingNotifier notifier = new RoutingNotifier() {
@Override
public void routingComplete(@NonNull RoutingResult result) {
assertThat(result.isComplete).isFalse();
assertThat(result.message).isEqualTo(FAILURE_MESSAGE);
}
};
Route route = new Route.Builder(new Bundle()).routingNotifier(notifier).build();
route.setResult(false, FAILURE_MESSAGE);
}

@Test
public void testRoutingNotifierOnNullNotifier() {
Route route = new Route.Builder(new Bundle()).routingNotifier(null).build();
route.setResult(true, null);
assertThat(route.isCompleted());
}

@Test
public void testMultipleSetResult() {
Route route = new Route.Builder(new Bundle()).routingNotifier(null).build();
route.setResult(true, null);
assertThat(route.isCompleted());
boolean isCaught = false;
try {
route.setResult(true, null);
} catch (IllegalStateException e) {
isCaught = true;
}
assertThat(isCaught).isTrue();
}
}

0 comments on commit 06819f3

Please sign in to comment.