Skip to content

Commit

Permalink
Migrate tests to junit5 (#391)
Browse files Browse the repository at this point in the history
* migrate tests to junit5 (except for robo)

* remove dupe

* spotless
  • Loading branch information
breedx-splk authored Oct 18, 2022
1 parent edfd561 commit 507a793
Show file tree
Hide file tree
Showing 38 changed files with 409 additions and 388 deletions.
10 changes: 9 additions & 1 deletion splunk-otel-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ dependencies {
api("io.opentelemetry:opentelemetry-api")
api("com.squareup.okhttp3:okhttp:4.10.0")

testImplementation("junit:junit:4.13.2")
testImplementation("org.mockito:mockito-core:4.8.0")
testImplementation("org.mockito:mockito-junit-jupiter:4.8.0")
testImplementation(platform("org.junit:junit-bom:5.9.1"))
testImplementation("org.junit.jupiter:junit-jupiter-api")
testImplementation("org.junit.jupiter:junit-jupiter-engine")
testImplementation("org.junit.vintage:junit-vintage-engine")
testImplementation("io.opentelemetry:opentelemetry-sdk-testing")
testImplementation("org.robolectric:robolectric:4.9")
testImplementation("androidx.test:core:1.4.0")
Expand All @@ -71,5 +75,9 @@ dependencies {
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.8")
}

tasks.withType<Test> {
useJUnitPlatform()
}

extra["pomName"] = "Splunk Otel Android"
description = "A library for instrumenting Android applications for Splunk RUM"
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,38 @@

package com.splunk.rum;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.app.Activity;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule;
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
import io.opentelemetry.sdk.trace.data.EventData;
import io.opentelemetry.sdk.trace.data.SpanData;
import java.util.List;
import java.util.Optional;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

class ActivityCallbacksTest {
@RegisterExtension final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create();

public class ActivityCallbacksTest {
@Rule public OpenTelemetryRule otelTesting = OpenTelemetryRule.create();
private Tracer tracer;
private VisibleScreenTracker visibleScreenTracker;
private final AppStartupTimer startupTimer = new AppStartupTimer();

@Before
@BeforeEach
public void setup() {
tracer = otelTesting.getOpenTelemetry().getTracer("testTracer");
visibleScreenTracker = mock(VisibleScreenTracker.class);
}

@Test
public void appStartup() {
void appStartup() {
startupTimer.start(tracer);
ActivityCallbacks activityCallbacks =
new ActivityCallbacks(tracer, visibleScreenTracker, startupTimer);
Expand Down Expand Up @@ -94,7 +95,7 @@ public void appStartup() {
}

@Test
public void activityCreation() {
void activityCreation() {
ActivityCallbacks activityCallbacks =
new ActivityCallbacks(tracer, visibleScreenTracker, startupTimer);

Expand Down Expand Up @@ -144,7 +145,7 @@ private void startupAppAndClearSpans(ActivityCallbackTestHarness testHarness) {
}

@Test
public void activityRestart() {
void activityRestart() {
ActivityCallbacks activityCallbacks =
new ActivityCallbacks(tracer, visibleScreenTracker, startupTimer);

Expand Down Expand Up @@ -186,7 +187,7 @@ public void activityRestart() {
}

@Test
public void activityResumed() {
void activityResumed() {
when(visibleScreenTracker.getPreviouslyVisibleScreen()).thenReturn("previousScreen");
ActivityCallbacks activityCallbacks =
new ActivityCallbacks(tracer, visibleScreenTracker, startupTimer);
Expand Down Expand Up @@ -223,7 +224,7 @@ public void activityResumed() {
}

@Test
public void activityDestroyedFromStopped() {
void activityDestroyedFromStopped() {
ActivityCallbacks activityCallbacks =
new ActivityCallbacks(tracer, visibleScreenTracker, startupTimer);

Expand Down Expand Up @@ -259,7 +260,7 @@ public void activityDestroyedFromStopped() {
}

@Test
public void activityDestroyedFromPaused() {
void activityDestroyedFromPaused() {
ActivityCallbacks activityCallbacks =
new ActivityCallbacks(tracer, visibleScreenTracker, startupTimer);

Expand Down Expand Up @@ -315,7 +316,7 @@ public void activityDestroyedFromPaused() {
}

@Test
public void activityStoppedFromRunning() {
void activityStoppedFromRunning() {
ActivityCallbacks activityCallbacks =
new ActivityCallbacks(tracer, visibleScreenTracker, startupTimer);

Expand Down Expand Up @@ -373,6 +374,6 @@ public void activityStoppedFromRunning() {
private void checkEventExists(List<EventData> events, String eventName) {
Optional<EventData> event =
events.stream().filter(e -> e.getName().equals(eventName)).findAny();
assertTrue("Event with name " + eventName + " not found", event.isPresent());
assertTrue(event.isPresent(), "Event with name " + eventName + " not found");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,35 @@

package com.splunk.rum;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.app.Activity;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule;
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
import io.opentelemetry.sdk.trace.data.SpanData;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class ActivityTracerTest {
@Rule public OpenTelemetryRule otelTesting = OpenTelemetryRule.create();
@RegisterExtension final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create();

private Tracer tracer;
private final VisibleScreenTracker visibleScreenTracker = mock(VisibleScreenTracker.class);
private final AppStartupTimer appStartupTimer = new AppStartupTimer();

@Before
@BeforeEach
public void setup() {
tracer = otelTesting.getOpenTelemetry().getTracer("testTracer");
}

@Test
public void restart_nonInitialActivity() {
void restart_nonInitialActivity() {
ActivityTracer trackableTracer =
new ActivityTracer(
mock(Activity.class),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
import static org.mockito.Mockito.when;

import android.os.Handler;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class AnrWatcherTest {
class AnrWatcherTest {

@Test
public void mainThreadDisappearing() {
void mainThreadDisappearing() {
Handler handler = mock(Handler.class);
Thread mainThread = mock(Thread.class);
SplunkRum splunkRum = mock(SplunkRum.class);
Expand All @@ -44,7 +44,7 @@ public void mainThreadDisappearing() {
}

@Test
public void noAnr() {
void noAnr() {
Handler handler = mock(Handler.class);
Thread mainThread = mock(Thread.class);
SplunkRum splunkRum = mock(SplunkRum.class);
Expand All @@ -64,7 +64,7 @@ public void noAnr() {
}

@Test
public void noAnr_temporaryPause() {
void noAnr_temporaryPause() {
Handler handler = mock(Handler.class);
Thread mainThread = mock(Thread.class);
SplunkRum splunkRum = mock(SplunkRum.class);
Expand All @@ -88,7 +88,7 @@ public void noAnr_temporaryPause() {
}

@Test
public void anr_detected() {
void anr_detected() {
Handler handler = mock(Handler.class);
Thread mainThread = mock(Thread.class);
SplunkRum splunkRum = mock(SplunkRum.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@

package com.splunk.rum;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertSame;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule;
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
import io.opentelemetry.sdk.trace.data.SpanData;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

public class AppStartupTimerTest {
@Rule public OpenTelemetryRule otelTesting = OpenTelemetryRule.create();
class AppStartupTimerTest {
@RegisterExtension final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create();
private Tracer tracer;

@Before
public void setup() {
@BeforeEach
void setup() {
tracer = otelTesting.getOpenTelemetry().getTracer("testTracer");
}

@Test
public void start_end() {
void start_end() {
AppStartupTimer appStartupTimer = new AppStartupTimer();
Span startSpan = appStartupTimer.start(tracer);
assertNotNull(startSpan);
Expand All @@ -57,7 +57,7 @@ public void start_end() {
}

@Test
public void multi_end() {
void multi_end() {
AppStartupTimer appStartupTimer = new AppStartupTimer();
appStartupTimer.start(tracer);
appStartupTimer.end();
Expand All @@ -67,7 +67,7 @@ public void multi_end() {
}

@Test
public void multi_start() {
void multi_start() {
AppStartupTimer appStartupTimer = new AppStartupTimer();
appStartupTimer.start(tracer);
assertSame(appStartupTimer.start(tracer), appStartupTimer.start(tracer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,37 @@

package com.splunk.rum;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

import java.time.Clock;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class BandwidthTrackerTest {
class BandwidthTrackerTest {

private Clock clock;
private AtomicLong time;

@Before
public void setup() {
@BeforeEach
void setup() {
clock = mock(Clock.class);
time = new AtomicLong(System.currentTimeMillis());
// Clock moves 5s each time its queried
doAnswer(invocation -> time.addAndGet(5000)).when(clock).millis();
}

@Test
public void testSustainedRateNoData() {
void testSustainedRateNoData() {
BandwidthTracker tracker = new BandwidthTracker(clock);
assertEquals(0.0, tracker.totalSustainedRate(), 0.0);
}

@Test
public void testSustainedRate() {
void testSustainedRate() {
BandwidthTracker tracker = new BandwidthTracker(clock);
tracker.tick(Collections.singletonList(new byte[270000]));
tracker.tick(Collections.singletonList(new byte[200]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import static org.mockito.Mockito.when;

import android.telephony.TelephonyManager;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class CarrierFinderTest {
class CarrierFinderTest {

@Test
public void testSimpleGet() {
void testSimpleGet() {
Carrier expected =
Carrier.builder()
.id(206)
Expand All @@ -49,7 +49,7 @@ public void testSimpleGet() {
}

@Test
public void testMostlyInvalid() {
void testMostlyInvalid() {
Carrier expected = Carrier.builder().build();

TelephonyManager manager = mock(TelephonyManager.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.splunk.rum;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
Expand Down
Loading

0 comments on commit 507a793

Please sign in to comment.