Skip to content

Commit ca986a5

Browse files
onewhldenis-fokin
authored andcommitted
Generate DisplayName annotation only for JUnit 5 #576 (#624)
* Generate DisplayName annotation only for JUnit 5 #576 * Review fixes
1 parent 02c2101 commit ca986a5

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/CgMethodConstructor.kt

+17-1
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,23 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
16051605
} else {
16061606
setOf(annotation(testFramework.testAnnotationId))
16071607
}
1608-
displayName?.let { testFrameworkManager.addDisplayName(it) }
1608+
1609+
/* Add a short test's description depending on the test framework type:
1610+
DisplayName annotation in case of JUni5, and description argument to Test annotation in case of TestNG.
1611+
*/
1612+
if (displayName != null) {
1613+
when (testFramework) {
1614+
is Junit5 -> {
1615+
displayName.let { testFrameworkManager.addDisplayName(it) }
1616+
}
1617+
is TestNg -> {
1618+
testFrameworkManager.addTestDescription(displayName)
1619+
}
1620+
else -> {
1621+
// nothing
1622+
}
1623+
}
1624+
}
16091625

16101626
val result = currentExecution!!.result
16111627
if (result is UtTimeoutException) {

utbot-framework/src/main/kotlin/org/utbot/framework/codegen/model/constructor/tree/TestFrameworkManager.kt

+30-5
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import org.utbot.framework.codegen.model.constructor.context.CgContextOwner
1515
import org.utbot.framework.codegen.model.constructor.util.CgComponents
1616
import org.utbot.framework.codegen.model.constructor.util.classCgClassId
1717
import org.utbot.framework.codegen.model.constructor.util.importIfNeeded
18-
import org.utbot.framework.codegen.model.tree.CgCommentedAnnotation
1918
import org.utbot.framework.codegen.model.tree.CgEnumConstantAccess
2019
import org.utbot.framework.codegen.model.tree.CgExpression
2120
import org.utbot.framework.codegen.model.tree.CgGetJavaClass
@@ -174,13 +173,39 @@ internal abstract class TestFrameworkManager(val context: CgContext)
174173
}
175174
}
176175

176+
/**
177+
* Supplements TestNG @Test annotation with a description.
178+
* It looks like @Test(description="...")
179+
*
180+
* Should be used only with TestNG.
181+
* @see <a href="https://github.com/UnitTestBot/UTBotJava/issues/576">issue-576 on GitHub</a>
182+
*/
183+
open fun addTestDescription(description: String?) {
184+
if (description == null) return
185+
val testAnnotation =
186+
collectedMethodAnnotations.singleOrNull { it.classId == testFramework.testAnnotationId }
187+
188+
val descriptionArgument = CgNamedAnnotationArgument("description", stringLiteral(description))
189+
if (testAnnotation is CgMultipleArgsAnnotation) {
190+
testAnnotation.arguments += descriptionArgument
191+
} else {
192+
collectedMethodAnnotations += CgMultipleArgsAnnotation(
193+
testFramework.testAnnotationId,
194+
mutableListOf(descriptionArgument)
195+
)
196+
}
197+
}
198+
177199
abstract fun disableTestMethod(reason: String)
178200

179-
// We add a commented JUnit5 DisplayName annotation here by default,
180-
// because other test frameworks do not support such feature.
201+
/**
202+
* Adds @DisplayName annotation.
203+
*
204+
* Should be used only with JUnit 5.
205+
* @see <a href="https://github.com/UnitTestBot/UTBotJava/issues/576">issue-576 on GitHub</a>
206+
*/
181207
open fun addDisplayName(name: String) {
182-
val displayName = CgSingleArgAnnotation(Junit5.displayNameClassId, stringLiteral(name))
183-
collectedMethodAnnotations += CgCommentedAnnotation(displayName)
208+
collectedMethodAnnotations += CgSingleArgAnnotation(Junit5.displayNameClassId, stringLiteral(name))
184209
}
185210

186211
protected fun ClassId.toExceptionClass(): CgExpression =

0 commit comments

Comments
 (0)