Skip to content

Commit 70709cb

Browse files
onewhltamarinvs19
authored andcommitted
Generate DisplayName annotation only for JUnit 5 #576 (#624)
* Generate DisplayName annotation only for JUnit 5 #576 * Review fixes
1 parent 6ee811e commit 70709cb

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

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