Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix selection of method from JUnit 4 parameterized test #1018

Merged
merged 1 commit into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

package org.junit.vintage.engine.discovery;

import static org.junit.runner.manipulation.Filter.matchMethodDescription;
import static org.junit.vintage.engine.discovery.RunnerTestDescriptorAwareFilter.adapter;

import java.lang.reflect.Method;

import org.junit.platform.engine.EngineDiscoveryRequest;
import org.junit.platform.engine.discovery.MethodSelector;
import org.junit.runner.Description;
import org.junit.runner.manipulation.Filter;

/**
* @since 4.12
Expand All @@ -36,4 +36,35 @@ private void resolve(MethodSelector selector, TestClassCollector collector) {
collector.addFiltered(testClass, adapter(matchMethodDescription(methodDescription)));
}

/**
* The method {@link Filter#matchMethodDescription(Description)} returns a filter that does not account for
* the case when the description is for a Parametrized runner.
*/
private static Filter matchMethodDescription(final Description desiredDescription) {
return new Filter() {
@Override
public boolean shouldRun(Description description) {
if (description.isTest()) {
return desiredDescription.equals(description) || isMethodWithParameters(description);
}

// explicitly check if any children want to run
for (Description each : description.getChildren()) {
if (shouldRun(each)) {
return true;
}
}
return false;
}

private boolean isMethodWithParameters(Description description) {
return description.getMethodName().startsWith(desiredDescription.getMethodName() + "[");
}

@Override
public String describe() {
return String.format("Method %s", desiredDescription.getDisplayName());
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2015-2017 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.vintage.engine;

import static org.junit.jupiter.api.Assertions.fail;

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 JUnit4ParameterizedTestCase {

private final boolean b;
private final long l;
private final int i;

@Parameterized.Parameters(name = "{0} ---> : Test")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { { 1L, false, 21 }, { 2L, true, 11 }, { 3L, false, 21 }, });
}

public JUnit4ParameterizedTestCase(long l, boolean b, int i) {
this.b = b;
this.l = l;
this.i = i;
}

@Test
public void test1() {
fail("this test should fail");
}

@Test
public void endingIn_test1() {
fail("this test should fail");
}

@Test
public void test1_atTheBeginning() {
fail("this test should fail");
}

@Test
public void test2() {
/* always succeed */
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2015-2017 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.vintage.engine;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;

class JUnit4ParameterizedTests {
private final Launcher launcher = LauncherFactory.create();
private final Map<TestExecutionResult.Status, Integer> callCounts = new HashMap<>();
private final LauncherDiscoveryRequestBuilder requestBuilder = LauncherDiscoveryRequestBuilder.request();

@BeforeEach
void setUpLauncher() {
launcher.registerTestExecutionListeners(new TestListenerMock());
}

@Test
void selectingWholeParameterizedClassRunsTestsWithAllValues() {
requestBuilder.selectors(selectClass(JUnit4ParameterizedTestCase.class));
LauncherDiscoveryRequest discoveryRequest = requestBuilder.build();

launcher.execute(discoveryRequest);

HashMap<TestExecutionResult.Status, Integer> expectedCallCounts = new HashMap<>();
expectedCallCounts.put(TestExecutionResult.Status.SUCCESSFUL, 3);
expectedCallCounts.put(TestExecutionResult.Status.FAILED, 9);
assertEquals(expectedCallCounts, callCounts);
}

@Test
void selectingOneTestFromParameterizedClassRunsWithAllValues() {
requestBuilder.selectors(selectMethod(JUnit4ParameterizedTestCase.class, "test1"));
LauncherDiscoveryRequest discoveryRequest = requestBuilder.build();

launcher.execute(discoveryRequest);

HashMap<TestExecutionResult.Status, Integer> expectedCallCounts = new HashMap<>();
expectedCallCounts.put(TestExecutionResult.Status.FAILED, 3);
assertEquals(expectedCallCounts, callCounts);
}

private class TestListenerMock implements TestExecutionListener {
@Override
public void executionFinished(TestIdentifier identifier, TestExecutionResult result) {
if (identifier.isTest()) {
final TestExecutionResult.Status status = result.getStatus();
callCounts.merge(status, 1, Integer::sum);
}
}
}
}