Skip to content

Commit 06e2be3

Browse files
authored
style: ban org.junit.Assert and org.junit.jupiter.api.Assertions (#5507)
Fluent assertions through AssertJ result in more useful test output
1 parent 9fc070a commit 06e2be3

File tree

30 files changed

+302
-267
lines changed

30 files changed

+302
-267
lines changed

detekt-rules/src/software/aws/toolkits/gradle/detekt/rules/BannedImportsRule.kt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class BannedImportsRule : Rule() {
1717
override fun visitImportList(importList: KtImportList) {
1818
super.visitImportList(importList)
1919
importList.imports.forEach { element ->
20-
val importedFqName = element.importedFqName?.asString()
20+
val importedFqName = element.importedFqName?.asString() ?: return
2121
if (importedFqName == "org.assertj.core.api.Assertions") {
2222
report(
2323
CodeSmell(
@@ -28,7 +28,7 @@ class BannedImportsRule : Rule() {
2828
)
2929
}
3030

31-
if (importedFqName?.startsWith("org.hamcrest") == true) {
31+
if (importedFqName.startsWith("org.hamcrest")) {
3232
report(
3333
CodeSmell(
3434
issue,
@@ -38,9 +38,7 @@ class BannedImportsRule : Rule() {
3838
)
3939
}
4040

41-
if (importedFqName?.startsWith("kotlin.test.assert") == true &&
42-
importedFqName.startsWith("kotlin.test.assertNotNull") == false
43-
) {
41+
if (importedFqName.startsWith("kotlin.test.assert") && !importedFqName.startsWith("kotlin.test.assertNotNull")) {
4442
report(
4543
CodeSmell(
4644
issue,
@@ -50,7 +48,17 @@ class BannedImportsRule : Rule() {
5048
)
5149
}
5250

53-
if (importedFqName?.startsWith("org.gradle.internal.impldep") == true) {
51+
if (importedFqName.startsWith("org.junit.jupiter.api.Assertions") || importedFqName.startsWith("org.junit.Assert")) {
52+
report(
53+
CodeSmell(
54+
issue,
55+
Entity.from(element),
56+
message = "Use AssertJ instead of JUnit assertions"
57+
)
58+
)
59+
}
60+
61+
if (importedFqName.startsWith("org.gradle.internal.impldep")) {
5462
report(
5563
CodeSmell(
5664
issue,
@@ -60,7 +68,7 @@ class BannedImportsRule : Rule() {
6068
)
6169
}
6270

63-
if (importedFqName?.contains("kotlinx.coroutines.Dispatchers") == true) {
71+
if (importedFqName.contains("kotlinx.coroutines.Dispatchers")) {
6472
report(
6573
CodeSmell(
6674
issue,

plugins/amazonq/chat/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonqFeatureDev/FeatureDevSessionContextTest.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import com.intellij.testFramework.RuleChain
5-
import org.junit.Assert.assertEquals
5+
import org.assertj.core.api.Assertions.assertThat
66
import org.junit.Before
77
import org.junit.Rule
88
import org.junit.Test
@@ -85,7 +85,11 @@ class FeatureDevSessionContextTest : FeatureDevTestBase(HeavyJavaCodeInsightTest
8585
fun addressablePathOf(path: String) = path.removePrefix(base).removePrefix("/")
8686

8787
fileCases.forEach {
88-
assertEquals(it.shouldInclude, zippedFiles.contains(addressablePathOf(it.path)))
88+
if (it.shouldInclude) {
89+
assertThat(zippedFiles).contains(addressablePathOf(it.path))
90+
} else {
91+
assertThat(zippedFiles).doesNotContain(addressablePathOf(it.path))
92+
}
8993
}
9094
}
9195

plugins/amazonq/chat/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonqFeatureDev/storage/ChatSessionStorageTest.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33

44
package software.aws.toolkits.jetbrains.services.amazonqFeatureDev.storage
55

6-
import org.junit.Assert.assertEquals
6+
import org.assertj.core.api.Assertions.assertThat
77
import org.junit.Before
88
import org.junit.Test
99
import org.mockito.kotlin.mock
1010
import org.mockito.kotlin.whenever
1111
import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.FeatureDevTestBase
1212
import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.session.Session
13-
import kotlin.test.assertNotNull
1413

1514
class ChatSessionStorageTest : FeatureDevTestBase() {
1615

@@ -27,9 +26,9 @@ class ChatSessionStorageTest : FeatureDevTestBase() {
2726
@Test
2827
fun `check getSession for NewSession`() {
2928
val testSession = chatSessionStorage.getSession("tabId", project)
30-
assertNotNull(testSession)
31-
assertEquals(testSession.tabID, "tabId")
32-
assertEquals(testSession.project, project)
29+
assertThat(testSession).isNotNull()
30+
assertThat(testSession.tabID).isEqualTo("tabId")
31+
assertThat(testSession.project).isEqualTo(project)
3332
}
3433

3534
@Test
@@ -39,7 +38,7 @@ class ChatSessionStorageTest : FeatureDevTestBase() {
3938

4039
val expectedSession = chatSessionStorage.getSession(mockSession.tabID, mockSession.project)
4140
val actualSession = chatSessionStorage.getSession("tab1", project)
42-
assertNotNull(actualSession)
43-
assertEquals(expectedSession, actualSession)
41+
assertThat(actualSession).isNotNull()
42+
assertThat(actualSession).isEqualTo(expectedSession)
4443
}
4544
}

plugins/amazonq/codetransform/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codemodernizer/CodeTransformChatTest.kt

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@
33

44
package software.aws.toolkits.jetbrains.services.codemodernizer
55

6-
import org.junit.Assert.assertEquals
7-
import org.junit.Assert.assertNotNull
8-
import org.junit.Assert.assertNull
9-
import org.junit.Assert.fail
6+
import org.assertj.core.api.Assertions.assertThat
107
import org.junit.Test
118
import software.aws.toolkits.jetbrains.services.codemodernizer.constants.buildTransformResultChatContent
129
import software.aws.toolkits.jetbrains.services.codemodernizer.messages.CodeTransformButtonId
1310
import software.aws.toolkits.jetbrains.services.codemodernizer.model.CodeModernizerJobCompletedResult
1411
import software.aws.toolkits.jetbrains.services.codemodernizer.model.JobId
12+
import software.aws.toolkits.jetbrains.utils.satisfiesKt
1513
import software.aws.toolkits.resources.message
1614

1715
class CodeTransformChatTest {
@@ -20,41 +18,49 @@ class CodeTransformChatTest {
2018
fun `test that transform result chat item includes view build log button and message when pre-build fails`() {
2119
val result = CodeModernizerJobCompletedResult.JobFailedInitialBuild(JobId("dummy-job-id-123"), "Build failed in Java 8 sandbox", true)
2220
val chatItem = buildTransformResultChatContent(result)
23-
assertEquals(chatItem.message, message("codemodernizer.chat.message.result.fail_initial_build"))
24-
assertNotNull(chatItem.buttons)
25-
assertEquals(chatItem.buttons?.size ?: fail("buttons is null"), 1)
26-
assertEquals(chatItem.buttons?.get(0)?.id ?: fail("buttons is null"), CodeTransformButtonId.ViewBuildLog.id)
21+
assertThat(chatItem.message).isEqualTo(message("codemodernizer.chat.message.result.fail_initial_build"))
22+
assertThat(chatItem.buttons)
23+
.singleElement()
24+
.satisfiesKt {
25+
assertThat(it.id).isEqualTo(CodeTransformButtonId.ViewBuildLog.id)
26+
}
2727
}
2828

2929
@Test
3030
fun `test that transform result chat item includes view summary button and view diff button with correct label when job fully succeeded with 5 patch files`() {
3131
val result = CodeModernizerJobCompletedResult.JobCompletedSuccessfully(JobId("dummy-job-id-123"))
3232
val chatItem = buildTransformResultChatContent(result, 5)
33-
assertEquals(chatItem.message, message("codemodernizer.chat.message.result.success.multiple_diffs"))
34-
assertNotNull(chatItem.buttons)
35-
assertEquals(chatItem.buttons?.size ?: fail("buttons is null"), 2)
36-
assertEquals(chatItem.buttons?.get(0)?.id ?: fail("buttons is null"), CodeTransformButtonId.ViewDiff.id)
37-
assertEquals(chatItem.buttons?.get(0)?.text ?: fail("buttons is null"), "View diff 1/5")
38-
assertEquals(chatItem.buttons?.get(1)?.id ?: fail("buttons is null"), CodeTransformButtonId.ViewSummary.id)
33+
assertThat(chatItem.message).isEqualTo(message("codemodernizer.chat.message.result.success.multiple_diffs"))
34+
assertThat(chatItem.buttons)
35+
.hasSize(2)
36+
.satisfiesKt { buttons ->
37+
assertThat(buttons[0].id).isEqualTo(CodeTransformButtonId.ViewDiff.id)
38+
assertThat(buttons[0].text).isEqualTo("View diff 1/5")
39+
assertThat(buttons[1].id).isEqualTo(CodeTransformButtonId.ViewSummary.id)
40+
}
3941
}
4042

4143
@Test
4244
fun `test that transform result chat item includes view summary button and view diff button with correct label when job partially succeeded with 1 patch file`() {
4345
val result = CodeModernizerJobCompletedResult.JobPartiallySucceeded(JobId("dummy-job-id-123"))
4446
val chatItem = buildTransformResultChatContent(result, 1)
45-
assertEquals(chatItem.message, message("codemodernizer.chat.message.result.partially_success"))
46-
assertNotNull(chatItem.buttons)
47-
assertEquals(chatItem.buttons?.size ?: fail("buttons is null"), 2)
48-
assertEquals(chatItem.buttons?.get(0)?.id ?: fail("buttons is null"), CodeTransformButtonId.ViewDiff.id)
49-
assertEquals(chatItem.buttons?.get(0)?.text ?: fail("buttons is null"), "View diff")
50-
assertEquals(chatItem.buttons?.get(1)?.id ?: fail("buttons is null"), CodeTransformButtonId.ViewSummary.id)
47+
assertThat(chatItem.message).isEqualTo(message("codemodernizer.chat.message.result.partially_success"))
48+
49+
assertThat(chatItem.buttons)
50+
.hasSize(2)
51+
.satisfiesKt { buttons ->
52+
assertThat(buttons[0].id).isEqualTo(CodeTransformButtonId.ViewDiff.id)
53+
assertThat(buttons[0].text).isEqualTo("View diff")
54+
assertThat(buttons[1].id).isEqualTo(CodeTransformButtonId.ViewSummary.id)
55+
}
5156
}
5257

5358
@Test
5459
fun `test that transform result chat item does not include any buttons when job failed with known reason`() {
5560
val result = CodeModernizerJobCompletedResult.JobFailed(JobId("dummy-job-id-123"), message("codemodernizer.file.invalid_pom_version"))
5661
val chatItem = buildTransformResultChatContent(result)
57-
assertEquals(chatItem.message, message("codemodernizer.chat.message.result.fail_with_known_reason", result.failureReason))
58-
assertNull(chatItem.buttons)
62+
63+
assertThat(chatItem.message).isEqualTo(message("codemodernizer.chat.message.result.fail_with_known_reason", result.failureReason))
64+
assertThat(chatItem.buttons).isNull()
5965
}
6066
}

plugins/amazonq/codetransform/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codemodernizer/CodeTransformHilDownloadArtifactTest.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ package software.aws.toolkits.jetbrains.services.codemodernizer
55

66
import com.intellij.testFramework.assertEqualsToFile
77
import com.intellij.util.io.delete
8-
import org.junit.Assert.assertEquals
8+
import org.assertj.core.api.Assertions.assertThat
99
import org.junit.Before
1010
import org.junit.Test
1111
import software.aws.toolkits.jetbrains.services.codemodernizer.model.CodeTransformHilDownloadArtifact
1212
import software.aws.toolkits.jetbrains.utils.rules.HeavyJavaCodeInsightTestFixtureRule
13+
import software.aws.toolkits.jetbrains.utils.satisfiesKt
1314
import kotlin.io.path.createTempDirectory
1415

1516
class CodeTransformHilDownloadArtifactTest : CodeWhispererCodeModernizerTestBase(HeavyJavaCodeInsightTestFixtureRule()) {
@@ -25,10 +26,12 @@ class CodeTransformHilDownloadArtifactTest : CodeWhispererCodeModernizerTestBase
2526
val hilDownloadArtifact = CodeTransformHilDownloadArtifact.create(testZipFilePath, outputFolder)
2627

2728
// verify manifest file values
28-
assertEquals(hilDownloadArtifact.manifest.pomArtifactId, "lombok")
29-
assertEquals(hilDownloadArtifact.manifest.pomGroupId, "org.projectlombok")
30-
assertEquals(hilDownloadArtifact.manifest.sourcePomVersion, "0.11.4")
31-
assertEquals(hilDownloadArtifact.manifest.pomArtifactId, "lombok")
29+
assertThat(hilDownloadArtifact.manifest).satisfiesKt {
30+
assertThat(it.pomArtifactId).isEqualTo("lombok")
31+
assertThat(it.pomGroupId).isEqualTo("org.projectlombok")
32+
assertThat(it.sourcePomVersion).isEqualTo("0.11.4")
33+
assertThat(it.pomArtifactId).isEqualTo("lombok")
34+
}
3235

3336
// verify pom file
3437
val testDownloadPomFile = "humanInTheLoop/pom.xml".toResourceFile().toPath()

plugins/amazonq/codetransform/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codemodernizer/CodeTransformTelemetryTest.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
package software.aws.toolkits.jetbrains.services.codemodernizer
55

66
import com.intellij.testFramework.LightVirtualFile
7-
import org.junit.Assert.assertEquals
8-
import org.junit.Assert.assertNotEquals
7+
import org.assertj.core.api.Assertions.assertThat
98
import org.junit.Before
109
import org.junit.Test
1110
import org.mockito.kotlin.doReturn
@@ -25,14 +24,16 @@ class CodeTransformTelemetryTest : CodeWhispererCodeModernizerTestBase(HeavyJava
2524
fun `SessionId updated on prepareForNewJobSubmission invoked`() {
2625
val originalSessionId = CodeTransformTelemetryState.instance.getSessionId()
2726
telemetryManagerSpy.prepareForNewJobSubmission()
28-
assertNotEquals(originalSessionId, CodeTransformTelemetryState.instance.getSessionId())
27+
28+
assertThat(originalSessionId).isNotEqualTo(CodeTransformTelemetryState.instance.getSessionId())
2929
}
3030

3131
@Test
3232
fun `ProjectId is reproducible`() {
3333
val projectId1 = telemetryManagerSpy.getProjectHash(validJDK8CustomerSelection)
3434
val projectId2 = telemetryManagerSpy.getProjectHash(validJDK8CustomerSelection)
35-
assertEquals(projectId1, projectId2)
35+
36+
assertThat(projectId1).isEqualTo(projectId2)
3637
}
3738

3839
@Test
@@ -42,6 +43,7 @@ class CodeTransformTelemetryTest : CodeWhispererCodeModernizerTestBase(HeavyJava
4243
doReturn(Path("/anotherpath/pom.xml")).whenever(emptyPomFileSpy2).toNioPath()
4344
val newCustomerSelection = validJDK8CustomerSelection.copy(configurationFile = emptyPomFileSpy2)
4445
val projectId2 = telemetryManagerSpy.getProjectHash(newCustomerSelection)
45-
assertNotEquals(projectId1, projectId2)
46+
47+
assertThat(projectId1).isNotEqualTo(projectId2)
4648
}
4749
}

0 commit comments

Comments
 (0)