第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果使用 Spring 和 Junit 4,如何編寫內(nèi)部測試類?或者我還能如何構(gòu)建我的測試?

如果使用 Spring 和 Junit 4,如何編寫內(nèi)部測試類?或者我還能如何構(gòu)建我的測試?

幕布斯6054654 2023-04-26 15:56:56
我有一個測試類,將來會測試不同的測試條件,我想讓它更有條理并且只適合一個文件。我找到了這個解決方案,但它不適合我,因為我無法將 aSpringRunner.class和Enclosed.class組合成一個@RunWith。我不能使用Nitor Creation's Nested Runner- 它帶來了同樣的問題。我用Spring 5和Junit 4.12。好吧......我的問題是,如何將我的測試結(jié)合在幾個內(nèi)部/嵌套類中與一個根類中?UPD:一句話 - 我無法升級到Junit 5.
查看完整描述

1 回答

?
鴻蒙傳說

TA貢獻(xiàn)1865條經(jīng)驗 獲得超7個贊

JUnit 4

如果您需要堅持使用 JUnit 4,可以使用第三方插件來提供支持。

將依賴項添加到您的項目中,并HeirachalContextRunner像這樣使用:


@RunWith(HierarchicalContextRunner.class)

public class NestedTest {


? ? @ClassRule

? ? public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();


? ? @Rule

? ? public final SpringMethodRule? springMethodRule = new SpringMethodRule();


? ? @Before

? ? public void setup() {

? ? ? ? // General test-suite setup

? ? }


? ? public class NestedClass {


? ? ? ? @Test

? ? ? ? public void testSomething() {

? ? ? ? ? ? // Test

? ? ? ? }


? ? ? ? public class AnotherNestedClass {


? ? ? ? ? ? @Test

? ? ? ? ? ? public void testSomethingElse() {

? ? ? ? ? ? ? ? // Test

? ? ? ? ? ? }

? ? ? ? }

? ? }

}

請注意,我們不需要在這里指定 Spring 的運行器。相反,我們使用規(guī)則來應(yīng)用 Spring 測試框架,從Spring 4.2.


JUnit 5


可以說,一個更具前瞻性的解決方案是升級到JUnit 5. 然后您可以使用注釋直接構(gòu)建測試用例@Nested。見下文:


@SpringBootTest

@ExtendWith(SpringExtension.class)

class MyNestedTest {


? ? @BeforeAll

? ? void setup() {

? ? ? ? // General test-suite setup

? ? }


? ? @Nested

? ? @DisplayName("parentTestSuite")

? ? class NestedClass {


? ? ? ? @Test

? ? ? ? void testSomething() {

? ? ? ? ? ? // Test

? ? ? ? }


? ? ? ? @Nested

? ? ? ? @DisplayName("childTestSuite")

? ? ? ? class AnotherNestedClass {


? ? ? ? ? ? @Test

? ? ? ? ? ? void testSomethingElse() {

? ? ? ? ? ? ? ? // Test

? ? ? ? ? ? }


? ? ? ? }

? ? }

}

請注意,@RunWith已替換為@ExtendWithin?JUnit 5。如果您選擇遷移到 JUnit 5,您可能會發(fā)現(xiàn)閱讀Baeldung 的 JUnit 5 遷移指南很有用。



查看完整回答
反對 回復(fù) 2023-04-26
  • 1 回答
  • 0 關(guān)注
  • 341 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號