Skip to content

Commit a6cd252

Browse files
committed
refactor(templates): add templated test prompt for Java and Kotlin
This commit introduces a new templated test prompt feature for Java and Kotlin projects. The feature allows developers to automatically generate test code examples based on the structure of their controller, service, or other testable components. The example code is tailored to the specific language and project context, providing a useful starting point for writing tests.
1 parent 400460a commit a6cd252

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

docs/patterns/by-example.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ private fun findExampleCommitMessages(project: Project): String? {
3737
}
3838
```
3939

40+
## 测试代码示例
41+
42+
参考语言实现:`JavaTestContextProvider``KotlinTestContextProvider`
43+
44+
步骤:
45+
46+
1. 获取当前项目的被测试代码。
47+
2. 根据被测试代码寻找模板:`Controller``Service`、默认测试等。
48+
- Kotlin:`ControllerTest.kt`, `ServiceTest.kt`, `Test.kt`
49+
- Java: `ControllerTest.java`, `ServiceTest.java`, `Test.java`
50+
3. 根据模板生成测试代码。
51+
52+
53+
4054
## 文档示例
4155

4256
DOC TODO

java/src/main/kotlin/cc/unitmesh/idea/provider/JavaTestContextProvider.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ open class JavaTestContextProvider : ChatContextProvider {
3939

4040
val language = creationContext.sourceFile?.language?.displayName ?: "Java"
4141

42+
val testPrompt = project.service<TemplatedTestPrompt>()
4243
val finalPrompt = when {
4344
isController(fileName) && isSpringRelated -> {
4445
var testControllerPrompt = prompt + """
4546
|- Use appropriate Spring test annotations such as `@MockBean`, `@Autowired`, `@WebMvcTest`, `@DataJpaTest`, `@AutoConfigureTestDatabase`, `@AutoConfigureMockMvc`, `@SpringBootTest` etc.
4647
|""".trimMargin()
4748

48-
val lookup = project.service<TemplatedTestPrompt>().lookup("ControllerTest.java")
49+
val lookup = testPrompt.lookup("ControllerTest.java")
4950
if (lookup != null) {
5051
testControllerPrompt += "\nHere is the Test code template as example\n```$language\n$lookup\n```\n"
5152
}
@@ -59,7 +60,7 @@ open class JavaTestContextProvider : ChatContextProvider {
5960
|- Assume that the database is empty before each test and create valid entities with consideration for data constraints (jakarta.validation.constraints).
6061
|""".trimMargin()
6162

62-
val lookup = project.service<TemplatedTestPrompt>().lookup("ServiceTest.java")
63+
val lookup = testPrompt.lookup("ServiceTest.java")
6364
if (lookup != null) {
6465
testServicePrompt += "\nHere is the Test code template as example\n```$language\n$lookup\n```\n"
6566
}
@@ -68,7 +69,7 @@ open class JavaTestContextProvider : ChatContextProvider {
6869
}
6970

7071
else -> {
71-
val lookup = project.service<TemplatedTestPrompt>().lookup("Test.java")
72+
val lookup = testPrompt.lookup("Test.java")
7273
if (lookup != null) {
7374
prompt += "\nHere is the Test code template as example\n```$language\n$lookup\n```\n"
7475
}
@@ -89,7 +90,7 @@ open class JavaTestContextProvider : ChatContextProvider {
8990
fileName?.let { MvcUtil.isController(it, langFileSuffix()) } ?: false
9091

9192

92-
val projectJunitCache = mutableMapOf<Project, String>()
93+
private val projectJunitCache = mutableMapOf<Project, String>()
9394

9495
/**
9596
* Returns a string representing the JUnit rule for the given project.

kotlin/src/main/kotlin/cc/unitmesh/kotlin/provider/KotlinTestContextProvider.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ class KotlinTestContextProvider : JavaTestContextProvider() {
3131

3232
val language = creationContext.sourceFile?.language?.displayName ?: "Kotlin"
3333

34+
val testPrompt = project.service<TemplatedTestPrompt>()
3435
val finalPrompt = when {
3536
isController(fileName) && isSpringRelated -> {
3637
var testControllerPrompt = prompt + "\n" + """
3738
|- Use appropriate Spring test annotations such as `@MockBean`, `@Autowired`, `@WebMvcTest`, `@DataJpaTest`, `@AutoConfigureTestDatabase`, `@AutoConfigureMockMvc`, `@SpringBootTest` etc.
3839
|""".trimMargin()
3940

40-
val lookup = project.service<TemplatedTestPrompt>().lookup("ControllerTest.kt")
41+
val lookup = testPrompt.lookup("ControllerTest.kt")
4142
if (lookup != null) {
4243
testControllerPrompt += "\nHere is the Test code template as example\n```$language\n$lookup\n```\n"
4344
}
@@ -52,7 +53,7 @@ class KotlinTestContextProvider : JavaTestContextProvider() {
5253
|""".trimMargin()
5354

5455

55-
val lookup = project.service<TemplatedTestPrompt>().lookup("ServiceTest.kt")
56+
val lookup = testPrompt.lookup("ServiceTest.kt")
5657
if (lookup != null) {
5758
testServicePrompt += "\nHere is the Test code template as example\n```$language\n$lookup\n```\n"
5859
}
@@ -61,7 +62,7 @@ class KotlinTestContextProvider : JavaTestContextProvider() {
6162
}
6263

6364
else -> {
64-
val lookup = project.service<TemplatedTestPrompt>().lookup("Test.kt")
65+
val lookup = testPrompt.lookup("Test.kt")
6566
if (lookup != null) {
6667
prompt += "\n" +
6768
"Here is the Test code template as example\n```$language\n$lookup\n```\n"

0 commit comments

Comments
 (0)