當我在 IntelliJ 中運行我的測驗用例套件時,我有一個失敗的 junit 測驗用例。當獨立運行或與類中的其他測驗一起運行時,相同的測驗用例成功。它甚至可以與其他類中的其他測驗一起成功運行。但是,當我運行整個 651 測驗套件時,這 1 個測驗失敗。
此外,當我在 Maven 中運行測驗用例套件(即 - maven clean, install)時,整個套件運行成功。
一種似乎有效的解決方法是在運行配置中將 Fork Mode 設定為“Class”。

但是,fork 配置會導致測驗套件運行速度變慢,我想了解為什么會發生這種情況。失敗的測驗如下...
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes=MyApplication.class)
class AssetRepositoryTest {
....other tests....
@Transactional
@Test @DirtiesContext
void delete_allByAppUser() {
int deleteCount = assetRepository.deleteAllByAppUser(mainTestAppUser);
assertEquals(2, deleteCount);
}
....more tests....
}
當測驗執行時,Springboot 啟動,使用一些資料創建 H2 資料庫,然后當測驗結束時,資料庫被洗掉。該測驗旨在測驗 JPA 存盤庫類。直到最近它一直運行良好,我已經有一段時間沒有改變它了。
當我用整個套件運行這個測驗用例時(套件似乎總是以相同的順序運行),它失敗了。

失敗表明該表不存在......

When I look at the console log, I can see the prior test execute, then spring drops the database. Then this test executes and fails (the db is dropped afterall). And then Spring boots up and JPA creates the database for the next test in the same class. If I didn't know better, I'd say there's a bug with Springboot. It looks like Spring fails to spin up for this single test.
I thought maybe there's an issue with the test class that runs before it, but When I run this test with in a smaller block of tests (same order of execution), including the tests that run before and after it, it runs fine..

And as I said, when I run this standalone, it's fine - spring does it's thing, the database is created, the test executes, and the db is dropped...
任何建議表示贊賞。我無法確定這種奇怪行為的模式或原因。
uj5u.com熱心網友回復:
我的問題似乎與@DirtiesContext 和我的控制器測驗的組合有關。我在對 H2 資料庫進行更改的任何測驗上都使用了@DirtiesContext,因此后續測驗不會受到影響。我最終洗掉了@DirtiesContext,對于需要資料庫資料的每個測驗,我填充了它,然后使用@SQL 注釋(執行階段之前和之后)清理它。不僅我的問題消失了,而且 651 測驗的運行速度要快得多(顯著)。
沒有 @DirtiesContext 的示例控制器測驗如下所示:
@Test
@DisplayName("whitelist remote address - good")
@Sql(scripts={"/sql/remote-addresses.sql"}, executionPhase=Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts={"/sql/delete-all.sql"}, executionPhase=Sql.ExecutionPhase.AFTER_TEST_METHOD)
void whitelistAddress_good() {
//Setup app user for test:
AppUser testUser = TestObjects.getTestUser1();
//Encrypt data for call:
String dataToEncode = testUser.getAppUserId() AppConstants.SPECIAL_DATA_DELIMETER "1"; //row Id as inserted by the SQL...
String dataEncoded = encryptionUtil.encrypt(dataToEncode);
//Setup url variables:
Map<String, String> variables = new HashMap<>();
variables.put("encodedData", dataEncoded);
//Setup headers:
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
//Perform call:
ResponseEntity<String> response = testRestTemplate.exchange(
unirestTestUtil.getHostUrl() AppConstants.AUTHORIZE_ADDRESS "/{encodedData}",
HttpMethod.PATCH,
new HttpEntity<>(headers),
String.class,
variables
);
//Check output status:
System.out.println("jsonResponse = " response.getBody());
assertEquals(HttpStatus.OK.value(), response.getStatusCodeValue());
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/440780.html
上一篇:在IntellijSpringBoot中從psql中的出生日期計算年齡
下一篇:C#-5 類和繼承
