Skip to content

Commit

Permalink
Handle classes with empty names (e.g. anonymous classes)
Browse files Browse the repository at this point in the history
Fixes #87.
  • Loading branch information
marcphilipp committed Jan 2, 2024
1 parent 0d4adf6 commit 72d4f66
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ class ClassDescriptor extends AbstractTestDescriptor {
ExecutionStrategy executionStrategy = new IncludeMethodsExecutionStrategy();

ClassDescriptor(UniqueId uniqueId, Class<?> testClass, Set<TestTag> tags) {
super(uniqueId, testClass.getSimpleName(), ClassSource.from(testClass));
super(uniqueId, determineDisplayName(testClass), ClassSource.from(testClass));
this.testClass = testClass;
this.tags = tags;
}

private static String determineDisplayName(Class<?> testClass) {
String simpleName = testClass.getSimpleName();
return simpleName.isEmpty() ? testClass.getName() : simpleName;
}

@Override
public String getLegacyReportingName() {
return testClass.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Map;
import java.util.regex.Pattern;

import example.basics.AnonymousClassTestCase;
import example.basics.DryRunTestCase;
import example.basics.IgnoredTestCase;
import example.basics.InheritedClassLevelOnlyAnnotationTestCase;
Expand Down Expand Up @@ -314,6 +315,17 @@ void discoversNestedTestClasses() {
assertThat(classDescriptor.getChildren()).hasSize(1);
}

@Test
void ignoresAnonymousClasses() {
var selectedTestClass = AnonymousClassTestCase.class.getName() + "$1";
var request = request().selectors(selectClass(selectedTestClass)).build();

var rootDescriptor = testEngine.discover(request, engineId);

assertThat(rootDescriptor.getUniqueId()).isEqualTo(engineId);
assertThat(rootDescriptor.getChildren()).isEmpty();
}

interface InterfaceTestCase {
}

Expand Down
26 changes: 26 additions & 0 deletions src/testFixtures/java/example/basics/AnonymousClassTestCase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2021 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 v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package example.basics;

import org.testng.annotations.Test;

public class AnonymousClassTestCase {

@Test
public void test() {
new AnonymousClassTestCase() {
@SuppressWarnings("unused")
@Test
public void method() {
}
};
}
}

0 comments on commit 72d4f66

Please sign in to comment.