xuxiaowei-com-cn opened a new pull request, #8181:
URL: https://github.com/apache/incubator-seata/pull/8181
Add webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT to ensure the
test gets an independent application context, preventing occasional failures
when running alongside other namingserver tests that may share or interfere
with the cached Spring test context.
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- Please make sure you have read and understood the contributing
guidelines -->
- [ ] I have read the
[CONTRIBUTING.md](https://github.com/apache/incubator-seata/blob/2.x/CONTRIBUTING.md)
guidelines.
- [ ] I have registered the PR
[changes](https://github.com/apache/incubator-seata/tree/2.x/changes).
### Ⅰ. Describe what this PR did
Fix `AuthControllerWithRandomPasswordTest` to prevent occasional test
failures when running alongside other naming server tests.
The test uses `@SpringBootTest(webEnvironment =
SpringBootTest.WebEnvironment.RANDOM_PORT)` with `@AutoConfigureMockMvc` and
relies on `CapturedOutput` to extract an auto-generated random password from
application startup logs. When all naming server tests run together, Spring
Boot's test context caching and lifecycle management can occasionally cause the
test to fail because:
1. Context sharing with other `RANDOM_PORT` tests that specify different
`properties` configurations may result in the expected startup log output not
being captured
2. The `CapturedOutput` extension may not contain the expected password log
line when the context is reused from a previous test run
The fix ensures `AuthControllerWithRandomPasswordTest` gets a fresh,
isolated application context regardless of which other tests ran before it, by
adding `@DirtiesContext` to force Spring to rebuild the context for this test
class.
### Ⅱ. Does this pull request fix one issue?
<!-- If that, add "fixes #xxx" below in the next line, for example, fixes
#97. -->
fixes https://github.com/apache/incubator-seata/issues/8180
### Ⅲ. Why don't you add test cases (unit test/integration test)?
This PR itself fixes an existing test. No new test cases are needed.
### Ⅳ. Describe how to verify it
1. Run the individual test to confirm it still passes:
```
mvn test -pl namingserver -Dtest=AuthControllerWithRandomPasswordTest
```
2. Run the full naming server test suite to confirm the test now passes in
the suite context:
```
mvn test -pl namingserver
```
3. Verify all other naming server tests continue to pass.
### Ⅴ. Special notes for reviews
**Root Cause Analysis:**
The `AuthControllerWithRandomPasswordTest` has the following Spring Boot
test configuration:
- `@SpringBootTest(webEnvironment =
SpringBootTest.WebEnvironment.RANDOM_PORT)`
- `@AutoConfigureMockMvc`
- `@ExtendWith(OutputCaptureExtension.class)`
- No explicit `properties` or `classes`
Other naming server tests have different context configurations:
| Test Class | webEnvironment | Properties |
|---|---|---|
| `NamingControllerLoggerPrintSmokeTest` | `RANDOM_PORT` |
`console.user.password=` (empty) |
| `NamingControllerPropertiesSmokeTest` | `RANDOM_PORT` |
`console.user.username=seata, console.user.password=foo` |
| `AuthControllerWithCustomPropertiesTest` | `MOCK` |
`console.user.username=seata, console.user.password=foo` |
| `WebSecurityConfigTest` | `MOCK` | None |
| `NamingControllerTest` | `MOCK` | None |
Since `AuthControllerWithRandomPasswordTest` relies on
`CapturedOutput.getOut()` containing the auto-generated password log line
(`"Use the auto-generated password: [...]"`) from application startup, using
`@DirtiesContext` ensures a fresh context and clean startup log output for each
test class execution.
**Alternative approaches considered:**
1. **Add `@DirtiesContext`** (chosen) — Simplest fix; ensures context
isolation
2. **Specify explicit properties** — Would make the test configuration
identical to a smoke test, but defeats the purpose of testing random password
generation
3. **Restructure test execution** (e.g., `@Suite` classes) — More invasive,
changes test organization
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]