/cstk-language-go:go-add-testfrom cstk-language-go
Generates unit/integration tests for GOB Go microservices using the MockFunc pattern. Reads existing code to match project conventions.
Ships in cstk-language-go. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Add unit/integration tests to a GOB Go microservice following established project patterns.
You generate Go tests for GOB microservices following the MockFunc pattern established in gob-member-service and gob-auth-service.
Parse the user request to determine:
services/ (e.g., gob-process-service)domain, service, handler, repository, consumerIf not specified, ask the user.
Before writing ANY test code, read these files in the target service:
internal/repository/repository.go or similar interface files
internal/service/member_service.go)
internal/domain/*.go
*_test.go files in the target package
internal/dto/dto.go if testing service/handler layer
mocks_test.go doesn't exist)Create mocks_test.go in the same package as the tests. Use the MockFunc pattern:
package service
import (
"context"
"github.com/google/uuid"
"github.com/gob/{service}/internal/domain"
)
// --- MockXxxRepository ---
type MockXxxRepository struct {
FindByIDFunc func(ctx context.Context, id uuid.UUID) (*domain.Xxx, error)
CreateFunc func(ctx context.Context, entity *domain.Xxx) error
UpdateFunc func(ctx context.Context, entity *domain.Xxx) error
DeleteFunc func(ctx context.Context, id uuid.UUID) error
ListFunc func(ctx context.Context, limit, offset int) ([]*domain.Xxx, int, error)
// Add one field per interface method
}
func (m *MockXxxRepository) FindByID(ctx context.Context, id uuid.UUID) (*domain.Xxx, error) {
if m.FindByIDFunc != nil {
return m.FindByIDFunc(ctx, id)
}
return nil, nil
}
func (m *MockXxxRepository) Create(ctx context.Context, entity *domain.Xxx) error {
if m.CreateFunc != nil {
return m.CreateFunc(ctx, entity)
}
return nil
}
// ... implement ALL interface methods with nil-check + safe default
Rules for mocks:
MockXxxRepository struct per repository interfaceFunc suffixnil, nil for pointer returns, nil for error-only returnsmocks_test.go in the SAME package (not a separate mocks directory)_test.go files)Create a setup function that wires the service with all mock dependencies:
func setupXxxService() (*XxxService, *MockAaaRepository, *MockBbbRepository) {
aaaRepo := &MockAaaRepository{}
bbbRepo := &MockBbbRepository{}
// Match the actual constructor signature
service := NewXxxService(aaaRepo, bbbRepo)
// OR if the service uses a Repositories struct:
// repos := &Repositories{aaa: aaaRepo, bbb: bbbRepo}
// service := NewXxxService(repos)
return service, aaaRepo, bbbRepo
}
Rules:
NewXxxService() signaturetestConfig() helper toointernal/domain/*_test.go)Pure unit tests — no mocks needed:
func TestXxx_MethodName(t *testing.T) {
tests := []struct {
name string
// input fields
wantErr error
}{
{
name: "valid case",
wantErr: nil,
},
{
name: "invalid - reason",
wantErr: ErrSpecificError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
entity := &Xxx{/* fields */}
err := entity.MethodName()
if tt.wantErr != nil {
if err != tt.wantErr {
t.Errorf("MethodName() error = %v, want %v", err, tt.wantErr)
}
return
}
if err != nil {
t.Errorf("MethodName() unexpected error: %v", err)
}
})
}
}
internal/service/*_test.go)func TestXxxService_Create(t *testing.T) {
service, xxxRepo, _ := setupXxxService()
ctx := context.Background()
t.Run("success", func(t *testing.T) {
xxxRepo.CreateFunc = func(ctx context.Context, entity *domain.Xxx) error {
return nil
}
req := &dto.CreateXxxRequest{
Name: "Test",
}
result, err := service.Create(ctx, req)
if err != nil {
t.Fatalf("Create() error = %v", err)
}
if result.Name != "Test" {
t.Errorf("Create() Name = %v, want %v", result.Name, "Test")
}
})
t.Run("validation error - empty name", func(t *testing.T) {
req := &dto.CreateXxxRequest{
Name: "",
}
_, err := service.Create(ctx, req)
if err == nil {
t.Error("Create() expected validation error")
}
})
t.Run("repository error", func(t *testing.T) {
xxxRepo.CreateFunc = func(ctx context.Context, entity *domain.Xxx) error {
return errors.New("db connection failed")
}
req := &dto.CreateXxxRequest{
Name: "Test",
}
_, err := service.Create(ctx, req)
if err == nil {
t.Error("Create() expected error when repo fails")
}
})
}
internal/handler/*_test.go)func TestXxxHandler_Create(t *testing.T) {
app := fiber.New()
mockService := &MockXxxService{}
handler := NewXxxHandler(mockService)
app.Post("/xxx", handler.Create)
t.Run("success - 201", func(t *testing.T) {
mockService.CreateFunc = func(ctx context.Context, req *dto.CreateXxxRequest) (*dto.XxxResponse, error) {
return &dto.XxxResponse{ID: uuid.New(), Name: req.Name}, nil
}
body := `{"name": "Test"}`
req := httptest.NewRequest("POST", "/xxx", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
resp, err := app.Test(req)
if err != nil {
t.Fatalf("app.Test() error = %v", err)
}
if resp.StatusCode != fiber.StatusCreated {
t.Errorf("status = %d, want %d", resp.StatusCode, fiber.StatusCreated)
}
})
t.Run("bad request - invalid JSON", func(t *testing.T) {
req := httptest.NewRequest("POST", "/xxx", strings.NewReader("{invalid"))
req.Header.Set("Content-Type", "application/json")
resp, err := app.Test(req)
if err != nil {
t.Fatalf("app.Test() error = %v", err)
}
if resp.StatusCode != fiber.StatusBadRequest {
t.Errorf("status = %d, want %d", resp.StatusCode, fiber.StatusBadRequest)
}
})
}
internal/messaging/*_test.go)func TestProcessHandler_HandleEvent(t *testing.T) {
mockService := &MockXxxService{}
handler := NewProcessHandler(mockService)
t.Run("process.created event", func(t *testing.T) {
var called bool
mockService.HandleProcessCreatedFunc = func(ctx context.Context, event *domain.ProcessEvent) error {
called = true
return nil
}
payload := []byte(`{"process_id": "` + uuid.New().String() + `", "action": "created"}`)
err := handler.Handle(context.Background(), payload)
if err != nil {
t.Fatalf("Handle() error = %v", err)
}
if !called {
t.Error("HandleProcessCreated was not called")
}
})
}
For EVERY method tested, always include these scenarios:
| Rule | Value |
|---|---|
| Test framework | Standard testing package only (NO testify) |
| Mock library | Hand-written MockFunc pattern (NO mockgen, gomock) |
| Test file location | Same package as source (_test suffix) |
| Mock file | mocks_test.go per package |
| Naming | Test{Type}_{Method} (e.g., TestMemberService_Create) |
| Structure | Table-driven with t.Run() subtests |
| Assertions | if got != want { t.Errorf(...) } |
| Fatal vs Error | t.Fatalf for setup failures, t.Errorf for assertion failures |
| Context | Always use context.Background() |
| UUIDs | Use uuid.New() for test IDs |
| Time | Use time.Now() or fixed time.Date() for deterministic tests |
github.com/stretchr/testifymockgen or any code generatormocks/ directoryreflect.DeepEqual — compare fields individuallyTestMain unless writing integration tests with DBAfter generating tests, run:
cd services/{service-name} && go test ./internal/{layer}/... -v -count=1
If compilation fails, fix immediately. Common issues:
services/gob-member-service/internal/service/ — full MockFunc pattern, setup helpers, table-driven testsservices/gob-auth-service/internal/service/ — in-memory store mocks, complex scenario testsservices/gob-election-service/internal/domain/ — pure domain validation testsWhat SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub jotjunior/cstk --plugin cstk-language-go7 skills in this plugin
/commitCommit staged and unstaged changes with a well-crafted conventional commit message.
Handles submodule updates, multi-service changes, and follows project conventions.
Triggers: "/commit", "commit", "commitar", "fazer commit", "commit changes".
/go-add-consumerAdd a RabbitMQ event consumer to an existing GOB Go microservice/go-add-entityAdd a full CRUD vertical slice (domain, DTO, repository, service, handler, migration, factory wiring) to an existing GOB Go microservice/go-add-migrationCreate properly named PostgreSQL migration files for GOB microservices/go-review-prReview all changes in the current branch against GOB project conventions before opening a PR.
Diff-aware: only analyzes what changed, not the entire codebase.
Triggers: "review pr", "revisar pr", "pre-pr check", "review branch", "revisar branch",
"checar antes do pr", "quality gate".
/go-review-serviceAudit a GOB Go microservice against all project conventions and patternsOther plugins' skills for the same job