-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgentest.go
79 lines (69 loc) · 2.2 KB
/
gentest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"os"
"path"
"strings"
)
const (
ApplicationTestsTPL = `package {{groupPath}};
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
@AutoConfigureMockMvc
public class ApplicationTests {
@Autowired
private MockMvc mvc;
@Test
public void testCheckHealthController() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/checkhealth/1"))
.andExpect(status().isOk())
.andExpect(content().string(equalTo("{\"code\":200,\"message\":\"ok\",\"data\":{\"id\":1,\"status\":0,\"message\":\"ok\"}}")));
}
}
`
)
func writeTestsFiles(mPath string, group string) {
fpath := path.Join(mPath, "ApplicationTests.java")
var f *os.File
var err error
if isExist(fpath) {
fmt.Printf("[WARN] '%v' already exists. Do you want to overwrite it? [Yes|No] ", fpath)
if askForConfirmation() {
f, err = os.OpenFile(fpath, os.O_RDWR|os.O_TRUNC, 0666)
if err != nil {
fmt.Printf("[WARN] %v\n", err)
return
}
} else {
fmt.Printf("[WARN] Skipped create file '%s'\n", fpath)
return
}
} else {
f, err = os.OpenFile(fpath, os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Printf("[WARN] %v\n", err)
return
}
}
template := ""
template = ApplicationTestsTPL
fileStr := strings.Replace(template, "{{groupPath}}", group, -1)
if _, err := f.WriteString(fileStr); err != nil {
fmt.Printf("[ERRO] Could not write test file to %s\n", fpath)
os.Exit(2)
}
CloseFile(f)
}