Skip to content

Commit 1cb5963

Browse files
committed
Add warning on conflicting test frameworks
1 parent 1ac3a04 commit 1cb5963

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,12 @@ data class TestsGenerationReport(
186186
var errors: MutableMap<UtMethod<*>, ErrorsCount> = mutableMapOf()
187187
) {
188188
val classUnderTest: KClass<*>
189-
get() = executables.firstOrNull()?.clazz ?: error("No executables found in test report")
189+
get() = executables.firstOrNull()?.clazz
190+
?: error("No executables found in test report")
190191

191-
// Initial message is generated lazily to avoid evaluation of classUnderTest
192-
var initialMessage: () -> String = { "Unit tests for $classUnderTest were generated successfully." }
192+
// Summary message is generated lazily to avoid evaluation of classUnderTest
193+
var summaryMessage: () -> String = { "Unit tests for $classUnderTest were generated successfully." }
194+
val initialWarnings: MutableList<() -> String> = mutableListOf()
193195

194196
fun addMethodErrors(testCase: UtTestCase, errors: Map<String, Int>) {
195197
this.errors[testCase.method] = errors
@@ -215,8 +217,11 @@ data class TestsGenerationReport(
215217
}
216218

217219
override fun toString(): String = buildString {
218-
appendHtmlLine(initialMessage())
220+
appendHtmlLine(summaryMessage())
219221
appendHtmlLine()
222+
initialWarnings.forEach { appendHtmlLine(it()) }
223+
appendHtmlLine()
224+
220225
val testMethodsStatistic = executables.map { it.countTestMethods() }
221226
val errors = executables.map { it.countErrors() }
222227
val overallTestMethods = testMethodsStatistic.sumBy { it.count }

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/generator/TestGenerator.kt

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -318,17 +318,8 @@ object TestGenerator {
318318
VfsUtil.createDirectories(parent.toString())
319319
resultedReportedPath.toFile().writeText(testsCodeWithTestReport.testsGenerationReport.getFileContent())
320320

321-
if (model.forceMockHappened) {
322-
testsCodeWithTestReport.testsGenerationReport.apply {
323-
initialMessage = {
324-
"""
325-
Unit tests for $classUnderTest were generated partially.<br>
326-
<b>Warning</b>: Some test cases were ignored, because no mocking framework is installed in the project.<br>
327-
Better results could be achieved by <a href="${TestReportUrlOpeningListener.prefix}${TestReportUrlOpeningListener.mockitoSuffix}">installing mocking framework</a>.
328-
""".trimIndent()
329-
}
330-
}
331-
}
321+
processInitialWarnings(testsCodeWithTestReport, model)
322+
332323
val notifyMessage = buildString {
333324
appendHtmlLine(testsCodeWithTestReport.testsGenerationReport.toString())
334325
appendHtmlLine()
@@ -349,6 +340,37 @@ object TestGenerator {
349340
TestsReportNotifier.notify(notifyMessage, model.project, model.testModule)
350341
}
351342

343+
private fun processInitialWarnings(testsCodeWithTestReport: TestsCodeWithTestReport, model: GenerateTestsModel) {
344+
val hasInitialWarnings = model.forceMockHappened || model.hasTestFrameworkConflict
345+
if (!hasInitialWarnings) {
346+
return
347+
}
348+
349+
testsCodeWithTestReport.testsGenerationReport.apply {
350+
summaryMessage = { "Unit tests for $classUnderTest were generated with warnings.<br>" }
351+
352+
if (model.forceMockHappened) {
353+
initialWarnings.add {
354+
"""
355+
<b>Warning</b>: Some test cases were ignored, because no mocking framework is installed in the project.<br>
356+
Better results could be achieved by <a href="${TestReportUrlOpeningListener.prefix}${TestReportUrlOpeningListener.mockitoSuffix}">installing mocking framework</a>.
357+
""".trimIndent()
358+
}
359+
}
360+
if (model.hasTestFrameworkConflict) {
361+
initialWarnings.add {
362+
"""
363+
<b>Warning</b>: There are several test frameworks in the project.
364+
To select run configuration, please refer to the documentation depending on the project build system:
365+
<a href=" https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests">Gradle</a>,
366+
<a href=" https://maven.apache.org/surefire/maven-surefire-plugin/examples/providers.html">Maven</a>
367+
or <a href=" https://www.jetbrains.com/help/idea/run-debug-configuration.html#compound-configs">Idea</a>.
368+
""".trimIndent()
369+
}
370+
}
371+
}
372+
}
373+
352374
@Suppress("unused")
353375
// this method was used in the past, not used in the present but may be used in the future
354376
private fun insertImports(testClass: PsiClass, imports: List<Import>, editor: Editor) {

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsDialogWindow.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
506506
if (frameworkNotInstalled && createTestFrameworkNotificationDialog() == Messages.YES) {
507507
configureTestFramework()
508508
}
509+
510+
model.hasTestFrameworkConflict = TestFramework.allItems.count { it.isInstalled } > 1
509511
}
510512

511513
private fun configureMockFrameworkIfRequired() {

utbot-intellij/src/main/kotlin/org/utbot/intellij/plugin/ui/GenerateTestsModel.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ data class GenerateTestsModel(
2727
var selectedMethods: Set<MemberInfo>?,
2828
var timeout:Long,
2929
var generateWarningsForStaticMocking: Boolean = false,
30-
var forceMockHappened: Boolean = false
30+
var forceMockHappened: Boolean = false,
31+
var hasTestFrameworkConflict: Boolean = false,
3132
) {
3233
var testSourceRoot: VirtualFile? = null
3334
var testPackageName: String? = null

0 commit comments

Comments
 (0)