Skip to content

Commit

Permalink
add support for BeforeParam and AfterParam
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-mohedano committed Nov 29, 2024
1 parent f35cbd4 commit 948fe8a
Show file tree
Hide file tree
Showing 12 changed files with 900 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ public JUnit4BeforeAfterInstrumentation() {
public String[] knownMatchingTypes() {
return new String[] {
"org.junit.internal.runners.statements.RunBefores",
"org.junit.internal.runners.statements.RunAfters"
"org.junit.internal.runners.statements.RunAfters",
"org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters$RunBeforeParams",
"org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters$RunAfterParams",
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runners.Parameterized;

public class JUnit4BeforeAfterOperationsTracer {
public static AgentScope startTrace(final Method method) {
Expand All @@ -21,6 +22,10 @@ public static AgentScope startTrace(final Method method) {
span.setTag(Tags.TEST_CALLBACK, "BeforeClass");
} else if (method.isAnnotationPresent(AfterClass.class)) {
span.setTag(Tags.TEST_CALLBACK, "AfterClass");
} else if (method.isAnnotationPresent(Parameterized.BeforeParam.class)) {
span.setTag(Tags.TEST_CALLBACK, "BeforeParam");
} else if (method.isAnnotationPresent(Parameterized.AfterParam.class)) {
span.setTag(Tags.TEST_CALLBACK, "AfterParam");
}
return AgentTracer.activateSpan(span);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import datadog.trace.instrumentation.junit4.TestEventsHandlerHolder
import junit.runner.Version
import org.example.TestFailedAfter
import org.example.TestFailedAfterClass
import org.example.TestFailedAfterParam
import org.example.TestFailedBefore
import org.example.TestFailedBeforeClass
import org.example.TestFailedBeforeParam
import org.example.TestSucceedBeforeAfter
import org.example.TestSucceedBeforeClassAfterClass
import org.example.TestSucceedBeforeParamAfterParam
import org.junit.runner.JUnitCore

@DisableTestTrace(reason = "avoid self-tracing")
Expand All @@ -24,10 +27,13 @@ class JUnit413Test extends CiVisibilityInstrumentationTest {
testcaseName | tests | expectedTracesCount
"test-succeed-before-after" | [TestSucceedBeforeAfter] | 3
"test-succeed-before-class-after-class" | [TestSucceedBeforeClassAfterClass] | 3
"test-succeed-before-param-after-param" | [TestSucceedBeforeParamAfterParam] | 2
"test-failed-before-class" | [TestFailedBeforeClass] | 1
"test-failed-after-class" | [TestFailedAfterClass] | 3
"test-failed-before" | [TestFailedBefore] | 3
"test-failed-after" | [TestFailedAfter] | 3
"test-failed-before-param" | [TestFailedBeforeParam] | 2
"test-failed-after-param" | [TestFailedAfterParam] | 2
}

private void runTests(Collection<Class<?>> tests) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.example;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class TestFailedAfterParam {
private final int num1;
private final int num2;
private final int sum;

public TestFailedAfterParam(final int num1, final int num2, final int sum) {
this.num1 = num1;
this.num2 = num2;
this.sum = sum;
}

@Parameterized.BeforeParam
public static void setup() {}

@Parameterized.AfterParam
public static void tearDown() {
throw new RuntimeException("after param setup failed");
}

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {{0, 0, 0}, {1, 1, 2}});
}

@Test
public void parameterized_test_succeed() {
assertEquals(num1 + num2, sum);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.example;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class TestFailedBeforeParam {
private final int num1;
private final int num2;
private final int sum;

public TestFailedBeforeParam(final int num1, final int num2, final int sum) {
this.num1 = num1;
this.num2 = num2;
this.sum = sum;
}

@Parameterized.BeforeParam
public static void setup() {
throw new RuntimeException("before param setup failed");
}

@Parameterized.AfterParam
public static void tearDown() {}

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {{0, 0, 0}, {1, 1, 2}});
}

@Test
public void parameterized_test_succeed() {
assertEquals(num1 + num2, sum);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.example;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class TestSucceedBeforeParamAfterParam {
private final int num1;
private final int num2;
private final int sum;

public TestSucceedBeforeParamAfterParam(final int num1, final int num2, final int sum) {
this.num1 = num1;
this.num2 = num2;
this.sum = sum;
}

@Parameterized.BeforeParam
public static void setup() {}

@Parameterized.AfterParam
public static void tearDown() {}

@Parameterized.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {{0, 0, 0}, {1, 1, 2}});
}

@Test
public void parameterized_test_succeed() {
assertEquals(num1 + num2, sum);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ ]
Loading

0 comments on commit 948fe8a

Please sign in to comment.