Skip to content

Commit 45e687e

Browse files
authored
Generate DisplayName annotation only for JUnit 5 #576 (#624)
* Generate DisplayName annotation only for JUnit 5 #576 * Review fixes
1 parent de28e4d commit 45e687e

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
@@ -1602,7 +1602,23 @@ internal class CgMethodConstructor(val context: CgContext) : CgContextOwner by c
16021602
} else {
16031603
setOf(annotation(testFramework.testAnnotationId))
16041604
}
1605-
displayName?.let { testFrameworkManager.addDisplayName(it) }
1605+
1606+
/* Add a short test's description depending on the test framework type:
1607+
DisplayName annotation in case of JUni5, and description argument to Test annotation in case of TestNG.
1608+
*/
1609+
if (displayName != null) {
1610+
when (testFramework) {
1611+
is Junit5 -> {
1612+
displayName.let { testFrameworkManager.addDisplayName(it) }
1613+
}
1614+
is TestNg -> {
1615+
testFrameworkManager.addTestDescription(displayName)
1616+
}
1617+
else -> {
1618+
// nothing
1619+
}
1620+
}
1621+
}
16061622

16071623
val result = currentExecution!!.result
16081624
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)