-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
RunnerWithCustomUniqueIdsAndDisplayNames.java
74 lines (59 loc) · 2.04 KB
/
RunnerWithCustomUniqueIdsAndDisplayNames.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Copyright 2015-2020 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 org.junit.vintage.engine.samples.junit4;
import static org.junit.runner.Description.createTestDescription;
import java.io.Serializable;
import java.util.Objects;
import org.junit.jupiter.api.DisplayName;
import org.junit.runner.Description;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
/**
* @since 4.12
*/
public class RunnerWithCustomUniqueIdsAndDisplayNames extends BlockJUnit4ClassRunner {
public RunnerWithCustomUniqueIdsAndDisplayNames(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected String getName() {
DisplayName displayName = getTestClass().getAnnotation(DisplayName.class);
return displayName == null ? super.getName() : displayName.value();
}
@Override
protected Description describeChild(FrameworkMethod method) {
String testName = getTestName(method);
return createTestDescription(getTestClass().getJavaClass().getName(), testName, new CustomUniqueId(testName));
}
private String getTestName(FrameworkMethod method) {
DisplayName displayName = method.getAnnotation(DisplayName.class);
return displayName == null ? testName(method) : displayName.value();
}
private static class CustomUniqueId implements Serializable {
private static final long serialVersionUID = 1L;
private final String testName;
public CustomUniqueId(String testName) {
this.testName = testName;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof CustomUniqueId) {
CustomUniqueId that = (CustomUniqueId) obj;
return Objects.equals(this.testName, that.testName);
}
return false;
}
@Override
public int hashCode() {
return testName.hashCode();
}
}
}