From 8da8ce9af97dbf52084e8ef4aee0d522d4149910 Mon Sep 17 00:00:00 2001 From: Zarina Kurbatova Date: Mon, 15 Aug 2022 17:16:42 +0300 Subject: [PATCH 1/5] Configured unit tests with mockito --- utbot-summary/build.gradle | 2 + .../summary/name/SimpleNameBuilderTest.kt | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt diff --git a/utbot-summary/build.gradle b/utbot-summary/build.gradle index 66eaae3682..3658ce4309 100644 --- a/utbot-summary/build.gradle +++ b/utbot-summary/build.gradle @@ -9,5 +9,7 @@ dependencies { implementation group: 'com.github.javaparser', name: 'javaparser-core', version: '3.22.1' + testImplementation 'org.mockito:mockito-core:4.7.0' + testImplementation("org.junit.jupiter:junit-jupiter:$junit5_version") } diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt new file mode 100644 index 0000000000..b0b9d34375 --- /dev/null +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt @@ -0,0 +1,38 @@ +package org.utbot.summary.name + +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.mockito.Mockito +import org.utbot.framework.plugin.api.UtOverflowFailure +import org.utbot.summary.ast.JimpleToASTMap +import org.utbot.summary.tag.TraceTag +import soot.SootMethod + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class SimpleNameBuilderTest { + private lateinit var traceTag: TraceTag + private lateinit var jimpleToASTMap: JimpleToASTMap + private lateinit var sootToAst: MutableMap + private lateinit var sootMethod: SootMethod + + @BeforeAll + fun setUp() { + traceTag = Mockito.mock(TraceTag::class.java) + Mockito.`when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable())) + + jimpleToASTMap = Mockito.mock(JimpleToASTMap::class.java) + + sootToAst = mutableMapOf() + sootMethod = Mockito.mock(SootMethod::class.java) + Mockito.`when`(sootMethod.name).thenReturn("methodName") + sootToAst[sootMethod] = jimpleToASTMap + } + + @Test + fun `method name starts with test`() { + val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod) + val methodName = simpleNameBuilder.name + assert(methodName.startsWith("test")) + } +} \ No newline at end of file From 68277d4fb2e2a0ce67dc46c583944fdc6bf6ab5f Mon Sep 17 00:00:00 2001 From: Zarina Kurbatova Date: Tue, 16 Aug 2022 17:56:57 +0300 Subject: [PATCH 2/5] Add unit tests for SimpleNameBuilderTest #543 --- .../summary/name/SimpleNameBuilderTest.kt | 47 ++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt index b0b9d34375..7844e530d0 100644 --- a/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt @@ -1,5 +1,6 @@ package org.utbot.summary.name +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance @@ -7,6 +8,7 @@ import org.mockito.Mockito import org.utbot.framework.plugin.api.UtOverflowFailure import org.utbot.summary.ast.JimpleToASTMap import org.utbot.summary.tag.TraceTag +import org.utbot.summary.tag.UniquenessTag import soot.SootMethod @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -19,20 +21,53 @@ class SimpleNameBuilderTest { @BeforeAll fun setUp() { traceTag = Mockito.mock(TraceTag::class.java) - Mockito.`when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable())) - + sootMethod = Mockito.mock(SootMethod::class.java) jimpleToASTMap = Mockito.mock(JimpleToASTMap::class.java) - sootToAst = mutableMapOf() - sootMethod = Mockito.mock(SootMethod::class.java) - Mockito.`when`(sootMethod.name).thenReturn("methodName") + + Mockito.`when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable())) + sootToAst[sootMethod] = jimpleToASTMap } @Test - fun `method name starts with test`() { + fun `method name should start with test`() { + Mockito.`when`(sootMethod.name).thenReturn("methodName") + val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod) val methodName = simpleNameBuilder.name assert(methodName.startsWith("test")) } + + @Test + fun `creates a name pair with a candidate with a bigger index`() { + Mockito.`when`(sootMethod.name).thenReturn("") + + val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod) + val fromCandidateName = DisplayNameCandidate("fromCandidate", UniquenessTag.Unique, 3) + + val toCandidate1 = DisplayNameCandidate("candidate1", UniquenessTag.Common, 2) + val toCandidate2 = DisplayNameCandidate("candidate2", UniquenessTag.Common, 4) + + val candidate = simpleNameBuilder.buildCandidate(fromCandidateName, toCandidate1, toCandidate2) + + val resultPair = Pair(fromCandidateName.name, toCandidate2.name) + assertEquals(candidate, resultPair) + } + + @Test + fun `returns null if candidates are equal`() { + Mockito.`when`(sootMethod.name).thenReturn("") + + val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod) + val fromCandidateName = DisplayNameCandidate("candidate", UniquenessTag.Unique, 0) + + // two equal candidates + val toCandidate1 = DisplayNameCandidate("candidate", UniquenessTag.Common, 1) + val toCandidate2 = DisplayNameCandidate("candidate", UniquenessTag.Common, 1) + + val candidate = simpleNameBuilder.buildCandidate(fromCandidateName, toCandidate1, toCandidate2) + + assertEquals(candidate, null) + } } \ No newline at end of file From a754c4ce9ecdcbecef35f319b31e4932a1c7be59 Mon Sep 17 00:00:00 2001 From: Zarina Kurbatova Date: Wed, 17 Aug 2022 13:33:53 +0300 Subject: [PATCH 3/5] Add unit tests for SimpleClusterCommentBuilderTest.kt and SimpleCommentBuilderTest.kt #543 --- .../SimpleClusterCommentBuilderTest.kt | 57 +++++++++++++++++++ .../comment/SimpleCommentBuilderTest.kt | 57 +++++++++++++++++++ .../summary/name/SimpleNameBuilderTest.kt | 17 +++--- .../org.mockito.plugins.MockMaker | 1 + 4 files changed, 124 insertions(+), 8 deletions(-) create mode 100644 utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleClusterCommentBuilderTest.kt create mode 100644 utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt create mode 100644 utbot-summary/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleClusterCommentBuilderTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleClusterCommentBuilderTest.kt new file mode 100644 index 0000000000..5ddc33077f --- /dev/null +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleClusterCommentBuilderTest.kt @@ -0,0 +1,57 @@ +package org.utbot.summary.comment + +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` +import org.utbot.framework.plugin.api.Step +import org.utbot.framework.plugin.api.UtOverflowFailure +import org.utbot.summary.ast.JimpleToASTMap +import org.utbot.summary.tag.StatementTag +import org.utbot.summary.tag.TraceTag +import soot.SootMethod +import soot.jimple.Stmt +import soot.jimple.internal.JReturnStmt + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class SimpleClusterCommentBuilderTest { + private lateinit var traceTag: TraceTag + private lateinit var jimpleToASTMap: JimpleToASTMap + private lateinit var sootToAst: MutableMap + private lateinit var sootMethod: SootMethod + private lateinit var statementTag: StatementTag + private lateinit var step: Step + private lateinit var statement: Stmt + + @BeforeAll + fun setUp() { + traceTag = mock(TraceTag::class.java) + sootMethod = mock(SootMethod::class.java) + jimpleToASTMap = mock(JimpleToASTMap::class.java) + statementTag = mock(StatementTag::class.java) + step = mock(Step::class.java) + statement = mock(JReturnStmt::class.java) + sootToAst = mutableMapOf() + + `when`(statementTag.step).thenReturn(step) + `when`(step.stmt).thenReturn(statement) + `when`(traceTag.path).thenReturn(listOf(step)) + `when`(traceTag.rootStatementTag).thenReturn(statementTag) + `when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable())) + + sootToAst[sootMethod] = jimpleToASTMap + } + + @Test + fun `throws throwable if execution result is null`() { + val commentBuilder = SimpleClusterCommentBuilder(traceTag, sootToAst) + val comment = commentBuilder.buildString(sootMethod) + val expectedComment = "
\n" +
+                "Test throws Throwable \n" +
+                "
" + Assertions.assertEquals(expectedComment, comment) + } + +} \ No newline at end of file diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt new file mode 100644 index 0000000000..a0bc8575d4 --- /dev/null +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt @@ -0,0 +1,57 @@ +package org.utbot.summary.comment + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` +import org.utbot.framework.plugin.api.Step +import org.utbot.framework.plugin.api.UtOverflowFailure +import org.utbot.summary.ast.JimpleToASTMap +import org.utbot.summary.tag.StatementTag +import org.utbot.summary.tag.TraceTag +import soot.SootMethod +import soot.jimple.Stmt +import soot.jimple.internal.JReturnStmt + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class SimpleCommentBuilderTest { + private lateinit var traceTag: TraceTag + private lateinit var jimpleToASTMap: JimpleToASTMap + private lateinit var sootToAst: MutableMap + private lateinit var sootMethod: SootMethod + private lateinit var statementTag: StatementTag + private lateinit var step: Step + private lateinit var statement: Stmt + + @BeforeAll + fun setUp() { + traceTag = mock(TraceTag::class.java) + sootMethod = mock(SootMethod::class.java) + jimpleToASTMap = mock(JimpleToASTMap::class.java) + statementTag = mock(StatementTag::class.java) + step = mock(Step::class.java) + statement = mock(JReturnStmt::class.java) + sootToAst = mutableMapOf() + + `when`(statementTag.step).thenReturn(step) + `when`(step.stmt).thenReturn(statement) + `when`(traceTag.path).thenReturn(listOf(step)) + `when`(traceTag.rootStatementTag).thenReturn(statementTag) + `when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable())) + + sootToAst[sootMethod] = jimpleToASTMap + } + + @Test + fun `throws throwable if execution result is null`() { + val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst) + val comment = commentBuilder.buildString(sootMethod) + val expectedComment = "
\n" +
+                "Test throws Throwable \n" +
+                "
" + assertEquals(expectedComment, comment) + } + +} \ No newline at end of file diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt index 7844e530d0..fc0acf2a91 100644 --- a/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/name/SimpleNameBuilderTest.kt @@ -4,7 +4,8 @@ import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance -import org.mockito.Mockito +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` import org.utbot.framework.plugin.api.UtOverflowFailure import org.utbot.summary.ast.JimpleToASTMap import org.utbot.summary.tag.TraceTag @@ -20,19 +21,19 @@ class SimpleNameBuilderTest { @BeforeAll fun setUp() { - traceTag = Mockito.mock(TraceTag::class.java) - sootMethod = Mockito.mock(SootMethod::class.java) - jimpleToASTMap = Mockito.mock(JimpleToASTMap::class.java) + traceTag = mock(TraceTag::class.java) + sootMethod = mock(SootMethod::class.java) + jimpleToASTMap = mock(JimpleToASTMap::class.java) sootToAst = mutableMapOf() - Mockito.`when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable())) + `when`(traceTag.result).thenReturn(UtOverflowFailure(Throwable())) sootToAst[sootMethod] = jimpleToASTMap } @Test fun `method name should start with test`() { - Mockito.`when`(sootMethod.name).thenReturn("methodName") + `when`(sootMethod.name).thenReturn("methodName") val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod) val methodName = simpleNameBuilder.name @@ -41,7 +42,7 @@ class SimpleNameBuilderTest { @Test fun `creates a name pair with a candidate with a bigger index`() { - Mockito.`when`(sootMethod.name).thenReturn("") + `when`(sootMethod.name).thenReturn("") val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod) val fromCandidateName = DisplayNameCandidate("fromCandidate", UniquenessTag.Unique, 3) @@ -57,7 +58,7 @@ class SimpleNameBuilderTest { @Test fun `returns null if candidates are equal`() { - Mockito.`when`(sootMethod.name).thenReturn("") + `when`(sootMethod.name).thenReturn("") val simpleNameBuilder = SimpleNameBuilder(traceTag, sootToAst, sootMethod) val fromCandidateName = DisplayNameCandidate("candidate", UniquenessTag.Unique, 0) diff --git a/utbot-summary/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker b/utbot-summary/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker new file mode 100644 index 0000000000..1f0955d450 --- /dev/null +++ b/utbot-summary/src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker @@ -0,0 +1 @@ +mock-maker-inline From 18fca9c738731a6b0dedd435f16a60548da8b116 Mon Sep 17 00:00:00 2001 From: Zarina Kurbatova Date: Wed, 17 Aug 2022 16:26:42 +0300 Subject: [PATCH 4/5] change mockito version, fix broken test, add tests for buildDocStmts --- utbot-summary/build.gradle | 2 +- .../comment/SimpleClusterCommentBuilderTest.kt | 17 +++++++++++------ .../summary/comment/SimpleCommentBuilderTest.kt | 10 ++++++++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/utbot-summary/build.gradle b/utbot-summary/build.gradle index 3658ce4309..0ec442f9f0 100644 --- a/utbot-summary/build.gradle +++ b/utbot-summary/build.gradle @@ -9,7 +9,7 @@ dependencies { implementation group: 'com.github.javaparser', name: 'javaparser-core', version: '3.22.1' - testImplementation 'org.mockito:mockito-core:4.7.0' + testImplementation group: 'org.mockito', name: 'mockito-core', version: mockito_version testImplementation("org.junit.jupiter:junit-jupiter:$junit5_version") } diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleClusterCommentBuilderTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleClusterCommentBuilderTest.kt index 5ddc33077f..fe3c808717 100644 --- a/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleClusterCommentBuilderTest.kt +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleClusterCommentBuilderTest.kt @@ -1,6 +1,6 @@ package org.utbot.summary.comment -import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance @@ -45,13 +45,18 @@ class SimpleClusterCommentBuilderTest { } @Test - fun `throws throwable if execution result is null`() { + fun `builds empty comment if execution result is null`() { val commentBuilder = SimpleClusterCommentBuilder(traceTag, sootToAst) val comment = commentBuilder.buildString(sootMethod) - val expectedComment = "
\n" +
-                "Test throws Throwable \n" +
-                "
" - Assertions.assertEquals(expectedComment, comment) + assertEquals(" ", comment) + } + + @Test + fun `builds empty doc statement if execution result is null`() { + val commentBuilder = SimpleClusterCommentBuilder(traceTag, sootToAst) + val statements = commentBuilder.buildDocStmts(sootMethod) + assertEquals(statements.size, 1) + assertEquals(statements[0].toString(), " ") } } \ No newline at end of file diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt index a0bc8575d4..a91fa7c93e 100644 --- a/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt @@ -54,4 +54,14 @@ class SimpleCommentBuilderTest { assertEquals(expectedComment, comment) } + @Test + fun `builds one doc statement`() { + val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst) + val statements = commentBuilder.buildDocStmts(sootMethod) + val expectedDocStatement = "Test \n" + + "throws Throwable \n" + assertEquals(statements.size, 1) + assertEquals(statements[0].toString(), expectedDocStatement) + } + } \ No newline at end of file From 5f24300bbad56c260154411636877d02611cabf7 Mon Sep 17 00:00:00 2001 From: Zarina Kurbatova Date: Wed, 17 Aug 2022 16:57:15 +0300 Subject: [PATCH 5/5] add tests for invokeDescription method --- .../summary/comment/SimpleCommentBuilder.kt | 2 +- .../summary/comment/SimpleCommentBuilderTest.kt | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt b/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt index 579328fc32..eab1d0d3e6 100644 --- a/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt +++ b/utbot-summary/src/main/kotlin/org/utbot/summary/comment/SimpleCommentBuilder.kt @@ -349,7 +349,7 @@ open class SimpleCommentBuilder( * In case when an enclosing class in nested, we need to replace '$' with '.' * to render the reference. */ - protected fun invokeDescription(className: String, methodName: String, methodParameterTypes: List): String { + fun invokeDescription(className: String, methodName: String, methodParameterTypes: List): String { val prettyClassName: String = className.replace("$", ".") return if (methodParameterTypes.isEmpty()) { diff --git a/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt index a91fa7c93e..39b995ccad 100644 --- a/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt +++ b/utbot-summary/src/test/kotlin/org/utbot/summary/comment/SimpleCommentBuilderTest.kt @@ -64,4 +64,21 @@ class SimpleCommentBuilderTest { assertEquals(statements[0].toString(), expectedDocStatement) } + @Test + fun `builds inline link for method`() { + val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst) + val methodReference = commentBuilder.invokeDescription("org.utbot.ClassName", "methodName", listOf()) + val expectedMethodReference = "{@link org.utbot.ClassName#methodName()}" + assertEquals(methodReference, expectedMethodReference) + } + + @Test + fun `builds inline link for method in nested class`() { + val commentBuilder = SimpleCommentBuilder(traceTag, sootToAst) + val methodReference = + commentBuilder.invokeDescription("org.utbot.ClassName\$NestedClassName", "methodName", listOf()) + val expectedMethodReference = "{@link org.utbot.ClassName.NestedClassName#methodName()}" + assertEquals(methodReference, expectedMethodReference) + } + } \ No newline at end of file