Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,12 @@ data class TestsGenerationReport(
var errors: MutableMap<UtMethod<*>, ErrorsCount> = mutableMapOf()
) {
val classUnderTest: KClass<*>
get() = executables.firstOrNull()?.clazz ?: error("No executables found in test report")
get() = executables.firstOrNull()?.clazz
?: error("No executables found in test report")

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

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

override fun toString(): String = buildString {
appendHtmlLine(initialMessage())
appendHtmlLine(summaryMessage())
appendHtmlLine()
initialWarnings.forEach { appendHtmlLine(it()) }
appendHtmlLine()

val testMethodsStatistic = executables.map { it.countTestMethods() }
val errors = executables.map { it.countErrors() }
val overallTestMethods = testMethodsStatistic.sumBy { it.count }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,8 @@ object TestGenerator {
VfsUtil.createDirectories(parent.toString())
resultedReportedPath.toFile().writeText(testsCodeWithTestReport.testsGenerationReport.getFileContent())

if (model.forceMockHappened) {
testsCodeWithTestReport.testsGenerationReport.apply {
initialMessage = {
"""
Unit tests for $classUnderTest were generated partially.<br>
<b>Warning</b>: Some test cases were ignored, because no mocking framework is installed in the project.<br>
Better results could be achieved by <a href="${TestReportUrlOpeningListener.prefix}${TestReportUrlOpeningListener.mockitoSuffix}">installing mocking framework</a>.
""".trimIndent()
}
}
}
processInitialWarnings(testsCodeWithTestReport, model)

val notifyMessage = buildString {
appendHtmlLine(testsCodeWithTestReport.testsGenerationReport.toString())
appendHtmlLine()
Expand All @@ -349,6 +340,37 @@ object TestGenerator {
TestsReportNotifier.notify(notifyMessage, model.project, model.testModule)
}

private fun processInitialWarnings(testsCodeWithTestReport: TestsCodeWithTestReport, model: GenerateTestsModel) {
val hasInitialWarnings = model.forceMockHappened || model.hasTestFrameworkConflict
if (!hasInitialWarnings) {
return
}

testsCodeWithTestReport.testsGenerationReport.apply {
summaryMessage = { "Unit tests for $classUnderTest were generated with warnings.<br>" }

if (model.forceMockHappened) {
initialWarnings.add {
"""
<b>Warning</b>: Some test cases were ignored, because no mocking framework is installed in the project.<br>
Better results could be achieved by <a href="${TestReportUrlOpeningListener.prefix}${TestReportUrlOpeningListener.mockitoSuffix}">installing mocking framework</a>.
""".trimIndent()
}
}
if (model.hasTestFrameworkConflict) {
initialWarnings.add {
"""
<b>Warning</b>: There are several test frameworks in the project.
To select run configuration, please refer to the documentation depending on the project build system:
<a href=" https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests">Gradle</a>,
<a href=" https://maven.apache.org/surefire/maven-surefire-plugin/examples/providers.html">Maven</a>
or <a href=" https://www.jetbrains.com/help/idea/run-debug-configuration.html#compound-configs">Idea</a>.
""".trimIndent()
}
}
}
}

@Suppress("unused")
// this method was used in the past, not used in the present but may be used in the future
private fun insertImports(testClass: PsiClass, imports: List<Import>, editor: Editor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,8 @@ class GenerateTestsDialogWindow(val model: GenerateTestsModel) : DialogWrapper(m
if (frameworkNotInstalled && createTestFrameworkNotificationDialog() == Messages.YES) {
configureTestFramework()
}

model.hasTestFrameworkConflict = TestFramework.allItems.count { it.isInstalled } > 1
}

private fun configureMockFrameworkIfRequired() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ data class GenerateTestsModel(
var selectedMethods: Set<MemberInfo>?,
var timeout:Long,
var generateWarningsForStaticMocking: Boolean = false,
var forceMockHappened: Boolean = false
var forceMockHappened: Boolean = false,
var hasTestFrameworkConflict: Boolean = false,
) {
var testSourceRoot: VirtualFile? = null
var testPackageName: String? = null
Expand Down