Skip to content

Add the field testPrivateMethods in utbot-gradle #483

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 4 commits into from
Jul 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class GenerateTestsAndSarifReportFacade(

private fun generateTestCases(targetClass: TargetClassWrapper, workingDirectory: Path): List<UtTestCase> =
TestCaseGenerator.generate(
targetClass.targetMethods(),
targetClass.targetMethods,
sarifProperties.mockStrategy,
sarifProperties.classesToMockAlways,
sarifProperties.generationTimeout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ interface SarifExtensionProvider {
*/
val markGeneratedTestsDirectoryAsTestSourcesRoot: Boolean

/**
* Generate tests for private methods or not.
*/
val testPrivateMethods: Boolean

val testFramework: TestFramework

val mockFramework: MockFramework
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.utbot.framework.plugin.sarif

import org.utbot.framework.plugin.api.UtMethod
import org.utbot.framework.plugin.api.util.executableId
import java.io.File
import kotlin.reflect.KCallable
import kotlin.reflect.KClass
Expand All @@ -14,13 +15,23 @@ data class TargetClassWrapper(
val classUnderTest: KClass<*>,
val sourceCodeFile: File,
val testsCodeFile: File,
val sarifReportFile: File
val sarifReportFile: File,
val testPrivateMethods: Boolean = false
) {
/**
* Returns the methods of the class [classUnderTest] declared by the user.
*/
fun targetMethods() =
classUnderTest.java.declaredMethods.map {
val targetMethods: List<UtMethod<*>> = run {
val allDeclaredMethods = classUnderTest.java.declaredMethods
val neededDeclaredMethods = if (testPrivateMethods) {
allDeclaredMethods.toList()
} else {
allDeclaredMethods.filter {
!it.executableId.isPrivate
}
}
neededDeclaredMethods.map {
UtMethod(it.kotlinFunction as KCallable<*>, classUnderTest)
}
}
}
7 changes: 7 additions & 0 deletions utbot-gradle/docs/utbot-gradle.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ __Groovy:__
generatedTestsRelativeRoot = 'build/generated/test'
sarifReportsRelativeRoot = 'build/generated/sarif'
markGeneratedTestsDirectoryAsTestSourcesRoot = true
testPrivateMethods = false
testFramework = 'junit5'
mockFramework = 'mockito'
generationTimeout = 60000L
Expand All @@ -58,6 +59,7 @@ __Kotlin DSL:__
generatedTestsRelativeRoot.set("build/generated/test")
sarifReportsRelativeRoot.set("build/generated/sarif")
markGeneratedTestsDirectoryAsTestSourcesRoot.set(true)
testPrivateMethods.set(false)
testFramework.set("junit5")
mockFramework.set("mockito")
generationTimeout.set(60000L)
Expand All @@ -78,6 +80,7 @@ generateTestsAndSarifReport
-PprojectRoot='C:/.../SomeDirectory'
-PgeneratedTestsRelativeRoot='build/generated/test'
-PsarifReportsRelativeRoot='build/generated/sarif'
-PtestPrivateMethods='false'
-PtestFramework=junit5
-PmockFramework=mockito
-PgenerationTimeout=60000
Expand Down Expand Up @@ -112,6 +115,10 @@ generateTestsAndSarifReport
- Mark the directory with generated tests as `test sources root` or not.
- By default, `true` is used.

- `testPrivateMethods`&ndash;
- Generate tests for private methods or not.
- By default, `false` is used.

- `testFramework` &ndash;
- The name of the test framework to be used.
- Can be one of:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ abstract class SarifGradleExtension {
@get:Input
abstract val markGeneratedTestsDirectoryAsTestSourcesRoot: Property<Boolean>

/**
* Generate tests for private methods or not.
*/
@get:Input
abstract val testPrivateMethods: Property<Boolean>

/**
* Can be one of: 'junit4', 'junit5', 'testng'.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ class SarifGradleExtensionProvider(
get() = extension.markGeneratedTestsDirectoryAsTestSourcesRoot.orNull
?: true

override val testPrivateMethods: Boolean
get() = taskParameters["testPrivateMethods"]?.let { it == "true"}
?: extension.testPrivateMethods.orNull
?: false

override val testFramework: TestFramework
get() = (taskParameters["testFramework"] ?: extension.testFramework.orNull)
?.let(::testFrameworkParse)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class SourceSetWrapper(
classUnderTest,
sourceCodeFile,
createTestsCodeFile(classFqn),
createSarifReportFile(classFqn)
createSarifReportFile(classFqn),
parentProject.sarifProperties.testPrivateMethods
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,43 @@ class SarifGradleExtensionProviderTest {
}
}

@Nested
@DisplayName("testPrivateMethods")
inner class TestPrivateMethodsTest {
@Test
fun `should be false by default`() {
setTestPrivateMethodsInExtension(null)
assertEquals(false, extensionProvider.testPrivateMethods)
}

@Test
fun `should be provided from the extension`() {
setTestPrivateMethodsInExtension(true)
assertEquals(true, extensionProvider.testPrivateMethods)
}

@Test
fun `should be provided from the task parameters`() {
setTestPrivateMethodsInTaskParameters(true)
assertEquals(true, extensionProvider.testPrivateMethods)
}

@Test
fun `should be provided from the task parameters, not from the extension`() {
setTestPrivateMethodsInTaskParameters(false)
setTestPrivateMethodsInExtension(true)
assertEquals(false, extensionProvider.testPrivateMethods)
}

private fun setTestPrivateMethodsInExtension(value: Boolean?) {
Mockito.`when`(extensionMock.testPrivateMethods).thenReturn(createBooleanProperty(value))
}

private fun setTestPrivateMethodsInTaskParameters(value: Boolean) {
extensionProvider.taskParameters = mapOf("testPrivateMethods" to "$value")
}
}

@Nested
@DisplayName("testFramework")
inner class TestFrameworkTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SourceSetWrapperTest {
Mockito.`when`(sarifPropertiesMock.codegenLanguage).thenReturn(CodegenLanguage.JAVA)
Mockito.`when`(sarifPropertiesMock.generatedTestsRelativeRoot).thenReturn("test")
Mockito.`when`(sarifPropertiesMock.sarifReportsRelativeRoot).thenReturn("sarif")
Mockito.`when`(sarifPropertiesMock.testPrivateMethods).thenReturn(true)

val gradleProject = GradleProjectWrapper(project, sarifPropertiesMock)
val sourceSetWrapper = SourceSetWrapper(project.mainSourceSet, gradleProject)
Expand All @@ -47,6 +48,7 @@ class SourceSetWrapperTest {
Mockito.`when`(sarifPropertiesMock.codegenLanguage).thenReturn(CodegenLanguage.JAVA)
Mockito.`when`(sarifPropertiesMock.generatedTestsRelativeRoot).thenReturn("test")
Mockito.`when`(sarifPropertiesMock.sarifReportsRelativeRoot).thenReturn("sarif")
Mockito.`when`(sarifPropertiesMock.testPrivateMethods).thenReturn(true)

val gradleProject = GradleProjectWrapper(project, sarifPropertiesMock)
val sourceSetWrapper = SourceSetWrapper(project.mainSourceSet, gradleProject)
Expand All @@ -66,6 +68,7 @@ class SourceSetWrapperTest {

Mockito.`when`(sarifPropertiesMock.generatedTestsRelativeRoot).thenReturn("test")
Mockito.`when`(sarifPropertiesMock.sarifReportsRelativeRoot).thenReturn("sarif")
Mockito.`when`(sarifPropertiesMock.testPrivateMethods).thenReturn(true)

val gradleProject = GradleProjectWrapper(project, sarifPropertiesMock)
val sourceSetWrapper = SourceSetWrapper(project.mainSourceSet, gradleProject)
Expand All @@ -79,6 +82,7 @@ class SourceSetWrapperTest {

Mockito.`when`(sarifPropertiesMock.generatedTestsRelativeRoot).thenReturn("test")
Mockito.`when`(sarifPropertiesMock.sarifReportsRelativeRoot).thenReturn("sarif")
Mockito.`when`(sarifPropertiesMock.testPrivateMethods).thenReturn(true)

val gradleProject = GradleProjectWrapper(project, sarifPropertiesMock)
val sourceSetWrapper = SourceSetWrapper(project.mainSourceSet, gradleProject)
Expand Down
5 changes: 5 additions & 0 deletions utbot-maven/docs/utbot-maven.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ For example, the following configuration may be used:
<generatedTestsRelativeRoot>target/generated/test</generatedTestsRelativeRoot>
<sarifReportsRelativeRoot>target/generated/sarif</sarifReportsRelativeRoot>
<markGeneratedTestsDirectoryAsTestSourcesRoot>true</markGeneratedTestsDirectoryAsTestSourcesRoot>
<testPrivateMethods>false</testPrivateMethods>
<testFramework>junit5</testFramework>
<mockFramework>mockito</mockFramework>
<generationTimeout>60000L</generationTimeout>
Expand Down Expand Up @@ -79,6 +80,10 @@ For example, the following configuration may be used:
- Mark the directory with generated tests as `test sources root` or not.
- By default, `true` is used.

- `testPrivateMethods`&ndash;
- Generate tests for private methods or not.
- By default, `false` is used.

- `testFramework` &ndash;
- The name of the test framework to be used.
- Can be one of:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class GenerateTestsAndSarifReportMojo : AbstractMojo() {
@Parameter(defaultValue = "true")
internal var markGeneratedTestsDirectoryAsTestSourcesRoot: Boolean = true

/**
* Generate tests for private methods or not.
*/
@Parameter(defaultValue = "false")
internal var testPrivateMethods: Boolean = false

/**
* Can be one of: 'junit4', 'junit5', 'testng'.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class SarifMavenConfigurationProvider(
override val markGeneratedTestsDirectoryAsTestSourcesRoot: Boolean
get() = generateTestsAndSarifReportMojo.markGeneratedTestsDirectoryAsTestSourcesRoot

override val testPrivateMethods: Boolean
get() = generateTestsAndSarifReportMojo.testPrivateMethods

override val testFramework: TestFramework
get() = testFrameworkParse(generateTestsAndSarifReportMojo.testFramework)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ class MavenProjectWrapper(
classUnderTest,
sourceCodeFile,
createTestsCodeFile(classFqn),
createSarifReportFile(classFqn)
createSarifReportFile(classFqn),
sarifProperties.testPrivateMethods
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,21 @@ class SarifMavenConfigurationProviderTest : AbstractMojoTestCase() {
Assertions.assertEquals(true, configurationProvider.markGeneratedTestsDirectoryAsTestSourcesRoot)
}

@Test
fun `testPrivateMethods should be provided from the configuration`() {
Assertions.assertEquals(true, configurationProvider.testPrivateMethods)
}

@Test
fun `testFramework should be provided from the configuration`() {
Assertions.assertEquals(Junit5, configurationProvider.testFramework)
}


@Test
fun `mockFramework should be provided from the configuration`() {
Assertions.assertEquals(MockFramework.MOCKITO, configurationProvider.mockFramework)
}


@Test
fun `generationTimeout should be provided from the configuration`() {
Assertions.assertEquals(10000, configurationProvider.generationTimeout)
Expand Down
1 change: 1 addition & 0 deletions utbot-maven/src/test/resources/project-to-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<generatedTestsRelativeRoot>target/generated/test</generatedTestsRelativeRoot>
<sarifReportsRelativeRoot>target/generated/sarif</sarifReportsRelativeRoot>
<markGeneratedTestsDirectoryAsTestSourcesRoot>true</markGeneratedTestsDirectoryAsTestSourcesRoot>
<testPrivateMethods>true</testPrivateMethods>
<testFramework>junit5</testFramework>
<mockFramework>mockito</mockFramework>
<generationTimeout>10000</generationTimeout>
Expand Down