|
18 | 18 | Package gstest provides unit testing utilities for dependency injection in Go-Spring framework. |
19 | 19 |
|
20 | 20 | Key Features: |
21 | | - - Non-critical Dependency Tolerance - Gracefully ignores non-test-target injection failures |
22 | | - via warning logs (enabled by gs.ForceAutowireIsNullable) |
23 | | - - Type-Safe Mocking - Offers MockFor/With methods for compile-time verified mock registration |
24 | | - - Context Lifecycle Management - TestMain automates application context bootstrap/teardown |
25 | | - - Smart Injection Helpers - Provides Get/Wire utilities to simplify test case composition |
| 21 | + - Test environment configuration: jobs and servers are disabled, and the "test" profile is automatically activated |
| 22 | + - Autowire failure tolerance: non-critical autowiring errors are tolerated so that missing beans do not break tests |
| 23 | + - Type-safe mocking: compile-time checked MockFor/With methods for registering mock beans |
| 24 | + - Context lifecycle management: TestMain starts and stops the Go-Spring context automatically |
| 25 | + - Injection helpers: Get[T](t) and Wire(t, obj) simplify bean retrieval and dependency injection |
26 | 26 |
|
27 | 27 | Usage Pattern: |
28 | 28 |
|
29 | | - // Register mocks in init function (executes before TestMain) |
| 29 | + // Step 1: Register your mock beans before tests run |
| 30 | + // by calling `MockFor[T]().With(obj)` inside an `init()` function. |
30 | 31 | func init() { |
31 | 32 | gstest.MockFor[*Dao]().With(&MockDao{}) |
32 | 33 | } |
33 | 34 |
|
| 35 | + // Step 2: Implement TestMain and invoke `gstest.TestMain(m, opts...)` |
| 36 | + // to bootstrap the application context, execute all tests, and then shut it down. |
| 37 | + // You can supply `BeforeRun` and `AfterRun` hooks to run code immediately before or after your test suite. |
34 | 38 | func TestMain(m *testing.M) { |
35 | 39 | gstest.TestMain(m) |
36 | 40 | } |
37 | 41 |
|
| 42 | + // Step 3: Write your test cases and use Get[T](t) or Wire(t, obj) to retrieve beans and inject dependencies. |
38 | 43 | func TestService(t *testing.T) { |
39 | 44 | // Retrieve autowired test target |
40 | 45 | service := gstest.Get[*Service](t) |
|
0 commit comments