You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: java/src/main/kotlin/cc/unitmesh/idea/provider/JavaTestContextProvider.kt
+5-4Lines changed: 5 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -39,13 +39,14 @@ open class JavaTestContextProvider : ChatContextProvider {
39
39
40
40
val language = creationContext.sourceFile?.language?.displayName ?:"Java"
41
41
42
+
val testPrompt = project.service<TemplatedTestPrompt>()
42
43
val finalPrompt =when {
43
44
isController(fileName) && isSpringRelated -> {
44
45
var testControllerPrompt = prompt +"""
45
46
|- Use appropriate Spring test annotations such as `@MockBean`, `@Autowired`, `@WebMvcTest`, `@DataJpaTest`, `@AutoConfigureTestDatabase`, `@AutoConfigureMockMvc`, `@SpringBootTest` etc.
46
47
|""".trimMargin()
47
48
48
-
val lookup =project.service<TemplatedTestPrompt>().lookup("ControllerTest.java")
49
+
val lookup =testPrompt.lookup("ControllerTest.java")
49
50
if (lookup !=null) {
50
51
testControllerPrompt +="\nHere is the Test code template as example\n```$language\n$lookup\n```\n"
51
52
}
@@ -59,7 +60,7 @@ open class JavaTestContextProvider : ChatContextProvider {
59
60
|- Assume that the database is empty before each test and create valid entities with consideration for data constraints (jakarta.validation.constraints).
60
61
|""".trimMargin()
61
62
62
-
val lookup =project.service<TemplatedTestPrompt>().lookup("ServiceTest.java")
63
+
val lookup =testPrompt.lookup("ServiceTest.java")
63
64
if (lookup !=null) {
64
65
testServicePrompt +="\nHere is the Test code template as example\n```$language\n$lookup\n```\n"
65
66
}
@@ -68,7 +69,7 @@ open class JavaTestContextProvider : ChatContextProvider {
68
69
}
69
70
70
71
else-> {
71
-
val lookup =project.service<TemplatedTestPrompt>().lookup("Test.java")
72
+
val lookup =testPrompt.lookup("Test.java")
72
73
if (lookup !=null) {
73
74
prompt +="\nHere is the Test code template as example\n```$language\n$lookup\n```\n"
74
75
}
@@ -89,7 +90,7 @@ open class JavaTestContextProvider : ChatContextProvider {
Copy file name to clipboardExpand all lines: kotlin/src/main/kotlin/cc/unitmesh/kotlin/provider/KotlinTestContextProvider.kt
+4-3Lines changed: 4 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -31,13 +31,14 @@ class KotlinTestContextProvider : JavaTestContextProvider() {
31
31
32
32
val language = creationContext.sourceFile?.language?.displayName ?:"Kotlin"
33
33
34
+
val testPrompt = project.service<TemplatedTestPrompt>()
34
35
val finalPrompt =when {
35
36
isController(fileName) && isSpringRelated -> {
36
37
var testControllerPrompt = prompt +"\n"+"""
37
38
|- Use appropriate Spring test annotations such as `@MockBean`, `@Autowired`, `@WebMvcTest`, `@DataJpaTest`, `@AutoConfigureTestDatabase`, `@AutoConfigureMockMvc`, `@SpringBootTest` etc.
38
39
|""".trimMargin()
39
40
40
-
val lookup =project.service<TemplatedTestPrompt>().lookup("ControllerTest.kt")
41
+
val lookup =testPrompt.lookup("ControllerTest.kt")
41
42
if (lookup !=null) {
42
43
testControllerPrompt +="\nHere is the Test code template as example\n```$language\n$lookup\n```\n"
43
44
}
@@ -52,7 +53,7 @@ class KotlinTestContextProvider : JavaTestContextProvider() {
52
53
|""".trimMargin()
53
54
54
55
55
-
val lookup =project.service<TemplatedTestPrompt>().lookup("ServiceTest.kt")
56
+
val lookup =testPrompt.lookup("ServiceTest.kt")
56
57
if (lookup !=null) {
57
58
testServicePrompt +="\nHere is the Test code template as example\n```$language\n$lookup\n```\n"
58
59
}
@@ -61,7 +62,7 @@ class KotlinTestContextProvider : JavaTestContextProvider() {
61
62
}
62
63
63
64
else-> {
64
-
val lookup =project.service<TemplatedTestPrompt>().lookup("Test.kt")
65
+
val lookup =testPrompt.lookup("Test.kt")
65
66
if (lookup !=null) {
66
67
prompt +="\n"+
67
68
"Here is the Test code template as example\n```$language\n$lookup\n```\n"
0 commit comments