Skip to content

Added ForceStaticMockListener to run "configure mockito-inline" action. #438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 7, 2022
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
2 changes: 1 addition & 1 deletion utbot-framework/src/main/kotlin/org/utbot/engine/Mocks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class Mocker(
fun shouldMock(
type: RefType,
mockInfo: UtMockInfo,
): Boolean = checkIfShouldMock(type, mockInfo).also { if (it) mockListenerController?.onShouldMock(strategy) }
): Boolean = checkIfShouldMock(type, mockInfo).also { if (it) mockListenerController?.onShouldMock(strategy, mockInfo) }

private fun checkIfShouldMock(
type: RefType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.utbot.engine.util.mockListeners

import kotlinx.coroutines.CancellationException

/**
* Exception used in [org.utbot.engine.util.mockListeners.ForceMockListener].
*/
class ForceMockCancellationException: CancellationException("Forced mocks without Mockito")

/**
* Exception used in [org.utbot.engine.util.mockListeners.ForceStaticMockListener].
*/
class ForceStaticMockCancellationException: CancellationException("Forced static mocks without Mockito-inline")
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.utbot.engine.util.mockListeners
import org.utbot.engine.EngineController
import org.utbot.engine.MockStrategy
import org.utbot.engine.util.mockListeners.exceptions.ForceMockCancellationException
import org.utbot.engine.UtMockInfo

/**
* Listener for mocker events in [org.utbot.engine.UtBotSymbolicEngine].
Expand All @@ -13,7 +13,7 @@ class ForceMockListener: MockListener {
var forceMockHappened = false
private set

override fun onShouldMock(controller: EngineController, strategy: MockStrategy) {
override fun onShouldMock(controller: EngineController, strategy: MockStrategy, mockInfo: UtMockInfo) {
// If force mocking happened -- сancel engine job
controller.job?.cancel(ForceMockCancellationException())
forceMockHappened = true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.utbot.engine.util.mockListeners

import org.utbot.engine.EngineController
import org.utbot.engine.MockStrategy
import org.utbot.engine.UtMockInfo
import org.utbot.engine.UtNewInstanceMockInfo
import org.utbot.engine.UtStaticMethodMockInfo
import org.utbot.engine.UtStaticObjectMockInfo

/**
* Listener for mocker events in [org.utbot.engine.UtBotSymbolicEngine].
* If forced static mock happened, cancels the engine job.
*
* Supposed to be created only if Mockito inline is not installed.
*/
class ForceStaticMockListener: MockListener {
var forceStaticMockHappened = false
private set

override fun onShouldMock(controller: EngineController, strategy: MockStrategy, mockInfo: UtMockInfo) {
if (mockInfo is UtNewInstanceMockInfo
|| mockInfo is UtStaticMethodMockInfo
|| mockInfo is UtStaticObjectMockInfo) {
// If force static mocking happened -- сancel engine job
controller.job?.cancel(ForceStaticMockCancellationException())
forceStaticMockHappened = true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package org.utbot.engine.util.mockListeners

import org.utbot.engine.EngineController
import org.utbot.engine.MockStrategy
import org.utbot.engine.UtMockInfo

/**
* Listener that can be attached using [MockListenerController] to mocker in [org.utbot.engine.UtBotSymbolicEngine].
*/
interface MockListener {
fun onShouldMock(controller: EngineController, strategy: MockStrategy)
fun onShouldMock(controller: EngineController, strategy: MockStrategy, mockInfo: UtMockInfo)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.utbot.engine.util.mockListeners

import org.utbot.engine.EngineController
import org.utbot.engine.MockStrategy
import org.utbot.engine.UtMockInfo

/**
* Controller that allows to attach listeners to mocker in [org.utbot.engine.UtBotSymbolicEngine].
Expand All @@ -13,7 +14,7 @@ class MockListenerController(private val controller: EngineController) {
listeners += listener
}

fun onShouldMock(strategy: MockStrategy) {
listeners.map { it.onShouldMock(controller, strategy) }
fun onShouldMock(strategy: MockStrategy, mockInfo: UtMockInfo) {
listeners.map { it.onShouldMock(controller, strategy, mockInfo) }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ object UtBotTestCaseGenerator : TestCaseGenerator {
private val logger = KotlinLogging.logger {}
private val timeoutLogger = KotlinLogging.logger(logger.name + ".timeout")

lateinit var configureEngine: (UtBotSymbolicEngine) -> Unit
lateinit var engineActions: MutableList<(UtBotSymbolicEngine) -> Unit>
lateinit var isCanceled: () -> Boolean

//properties to save time on soot initialization
Expand All @@ -76,19 +76,19 @@ object UtBotTestCaseGenerator : TestCaseGenerator {
buildDir,
classpath,
dependencyPaths,
configureEngine = {},
engineActions = mutableListOf(),
isCanceled
)

fun init(
buildDir: Path,
classpath: String?,
dependencyPaths: String,
configureEngine: (UtBotSymbolicEngine) -> Unit,
engineActions: MutableList<(UtBotSymbolicEngine) -> Unit>,
isCanceled: () -> Boolean
) {
this.isCanceled = isCanceled
this.configureEngine = configureEngine
this.engineActions = engineActions
if (isCanceled()) return

checkFrameworkDependencies(dependencyPaths)
Expand Down Expand Up @@ -293,7 +293,9 @@ object UtBotTestCaseGenerator : TestCaseGenerator {
mockStrategy,
chosenClassesToMockAlways,
executionTimeEstimator
).apply(configureEngine)
)

engineActions.map { engine.apply(it) }

generate(engine).collect {
when (it) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ class CodeGenerator(
buildDir: String,
classpath: String,
pluginJarsPath: String,
configureEngine: (UtBotSymbolicEngine) -> Unit = {},
engineActions: MutableList<(UtBotSymbolicEngine) -> Unit> = mutableListOf(),
isCanceled: () -> Boolean,
) {
init {
UtSettings.testMinimizationStrategyType = TestSelectionStrategyType.COVERAGE_STRATEGY
}

val generator = (project.service<Settings>().testCasesGenerator as UtBotTestCaseGenerator).apply {
init(Paths.get(buildDir), classpath, pluginJarsPath, configureEngine, isCanceled)
init(Paths.get(buildDir), classpath, pluginJarsPath, engineActions, isCanceled)
}

private val settingsState = project.service<Settings>().state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,11 @@ object TestGenerator {
""".trimIndent()
appendHtmlLine(savedFileMessage)
}
TestsReportNotifier.notify(notifyMessage, model.project, model.testModule)
TestsReportNotifier.notify(notifyMessage)
}

private fun processInitialWarnings(testsCodeWithTestReport: TestsCodeWithTestReport, model: GenerateTestsModel) {
val hasInitialWarnings = model.forceMockHappened || model.hasTestFrameworkConflict
val hasInitialWarnings = model.forceMockHappened || model.forceStaticMockHappened || model.hasTestFrameworkConflict
if (!hasInitialWarnings) {
return
}
Expand All @@ -425,6 +425,14 @@ object TestGenerator {
""".trimIndent()
}
}
if (model.forceStaticMockHappened) {
initialWarnings.add {
"""
<b>Warning</b>: Some test cases were ignored, because mockito-inline is not installed in the project.<br>
Better results could be achieved by <a href="${TestReportUrlOpeningListener.prefix}${TestReportUrlOpeningListener.mockitoInlineSuffix}">configuring mockito-inline</a>.
""".trimIndent()
}
}
if (model.hasTestFrameworkConflict) {
initialWarnings.add {
"""
Expand Down

This file was deleted.

Loading