diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/Domain.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/Domain.kt index 86c0768626..98579b5de9 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/Domain.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/domain/Domain.kt @@ -409,11 +409,6 @@ object Junit4 : TestFramework(id = "JUnit4",displayName = "JUnit 4") { ) } - val enclosedClassId = BuiltinClassId( - canonicalName = "org.junit.experimental.runners.Enclosed", - simpleName = "Enclosed" - ) - override val nestedClassesShouldBeStatic = true override val argListClassId: ClassId diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/TestFrameworkManager.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/TestFrameworkManager.kt index 33100a890d..6db42e130c 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/TestFrameworkManager.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/services/framework/TestFrameworkManager.kt @@ -88,8 +88,6 @@ abstract class TestFrameworkManager(val context: CgContext) abstract val annotationForNestedClasses: CgAnnotation? - abstract val annotationForOuterClasses: CgAnnotation? - /** * Determines whether appearance of expected exception in test method breaks current test execution or not. */ @@ -261,9 +259,6 @@ internal class TestNgManager(context: CgContext) : TestFrameworkManager(context) override val annotationForNestedClasses: CgAnnotation? get() = null - override val annotationForOuterClasses: CgAnnotation? - get() = null - override val isExpectedExceptionExecutionBreaking: Boolean = false override val timeoutArgumentName: String = "timeOut" @@ -409,20 +404,6 @@ internal class Junit4Manager(context: CgContext) : TestFrameworkManager(context) override val annotationForNestedClasses: CgAnnotation? get() = null - override val annotationForOuterClasses: CgAnnotation - get() { - require(testFramework is Junit4) { "According to settings, JUnit4 was expected, but got: $testFramework" } - return statementConstructor.annotation( - testFramework.runWithAnnotationClassId, - testFramework.enclosedClassId.let { - when (codegenLanguage) { - CodegenLanguage.JAVA -> CgGetJavaClass(it) - CodegenLanguage.KOTLIN -> CgGetKotlinClass(it) - } - } - ) - } - override val isExpectedExceptionExecutionBreaking: Boolean = true override fun expectException(exception: ClassId, block: () -> Unit) { @@ -481,13 +462,9 @@ internal class Junit5Manager(context: CgContext) : TestFrameworkManager(context) override val annotationForNestedClasses: CgAnnotation get() { require(testFramework is Junit5) { "According to settings, JUnit5 was expected, but got: $testFramework" } - return statementConstructor.annotation(testFramework.nestedTestClassAnnotationId) } - override val annotationForOuterClasses: CgAnnotation? - get() = null - override val isExpectedExceptionExecutionBreaking: Boolean = false private val assertThrows: BuiltinMethodId diff --git a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgTestClassConstructor.kt b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgTestClassConstructor.kt index f7e20b737f..09467b3f38 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgTestClassConstructor.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/framework/codegen/tree/CgTestClassConstructor.kt @@ -1,7 +1,10 @@ package org.utbot.framework.codegen.tree import org.utbot.framework.UtSettings +import org.utbot.framework.codegen.domain.Junit4 +import org.utbot.framework.codegen.domain.Junit5 import org.utbot.framework.codegen.domain.ParametrizedTestSource +import org.utbot.framework.codegen.domain.TestNg import org.utbot.framework.codegen.domain.builtin.TestClassUtilMethodProvider import org.utbot.framework.codegen.domain.context.CgContext import org.utbot.framework.codegen.domain.context.CgContextOwner @@ -77,23 +80,30 @@ open class CgTestClassConstructor(val context: CgContext) : currentTestClassContext.collectedTestClassAnnotations += it } } - if (testClassModel.nestedClasses.isNotEmpty()) { - testFrameworkManager.annotationForOuterClasses?.let { - currentTestClassContext.collectedTestClassAnnotations += it - } - } body = buildClassBody(currentTestClass) { + val notYetConstructedTestSets = testClassModel.methodTestSets.toMutableList() + for (nestedClass in testClassModel.nestedClasses) { - nestedClassRegions += CgRealNestedClassesRegion( - "Tests for ${nestedClass.classUnderTest.simpleName}", - listOf( - withNestedClassScope(nestedClass) { constructTestClass(nestedClass) } - ) - ) + // It is not possible to run tests for both outer and inner class in JUnit4 at once, + // so we locate all test methods in outer test class for JUnit4. + // see https://stackoverflow.com/questions/69770700/how-to-run-tests-from-outer-class-and-nested-inner-classes-simultaneously-in-jun + // or https://stackoverflow.com/questions/28230277/test-cases-in-inner-class-and-outer-class-with-junit4 + when (testFramework) { + Junit4 -> { + notYetConstructedTestSets += collectTestSetsFromInnerClasses(nestedClass) + } + Junit5, + TestNg -> { + nestedClassRegions += CgRealNestedClassesRegion( + "Tests for ${nestedClass.classUnderTest.simpleName}", + listOf(withNestedClassScope(nestedClass) { constructTestClass(nestedClass) }) + ) + } + } } - for (testSet in testClassModel.methodTestSets) { + for (testSet in notYetConstructedTestSets) { updateCurrentExecutable(testSet.executableId) val currentMethodUnderTestRegions = constructTestSet(testSet) ?: continue val executableUnderTestCluster = CgMethodsCluster( @@ -161,6 +171,15 @@ open class CgTestClassConstructor(val context: CgContext) : return if (regions.any()) regions else null } + private fun collectTestSetsFromInnerClasses(model: TestClassModel): List { + val testSets = model.methodTestSets.toMutableList() + for (nestedClass in model.nestedClasses) { + testSets += collectTestSetsFromInnerClasses(nestedClass) + } + + return testSets + } + private fun processFailure(testSet: CgMethodTestSet, failure: Throwable) { codeGenerationErrors .getOrPut(testSet) { mutableMapOf() } diff --git a/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonTestFrameworkManager.kt b/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonTestFrameworkManager.kt index 9e60783841..2ad3a2b881 100644 --- a/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonTestFrameworkManager.kt +++ b/utbot-python/src/main/kotlin/org/utbot/python/framework/codegen/model/constructor/tree/PythonTestFrameworkManager.kt @@ -53,8 +53,6 @@ internal class PytestManager(context: CgContext) : TestFrameworkManager(context) override val dataProviderMethodsHolder: TestClassContext get() = TODO() override val annotationForNestedClasses: CgAnnotation get() = TODO("Not yet implemented") - override val annotationForOuterClasses: CgAnnotation - get() = TODO("Not yet implemented") override fun assertEquals(expected: CgValue, actual: CgValue) { +CgPythonAssertEquals( @@ -86,8 +84,6 @@ internal class UnittestManager(context: CgContext) : TestFrameworkManager(contex get() = TODO() override val annotationForNestedClasses: CgAnnotation get() = TODO("Not yet implemented") - override val annotationForOuterClasses: CgAnnotation - get() = TODO("Not yet implemented") override fun expectException(exception: ClassId, block: () -> Unit) { require(testFramework is Unittest) { "According to settings, Unittest was expected, but got: $testFramework" }