番禺做網(wǎng)站多少錢濟(jì)南頭條今日新聞
JUnit測試運(yùn)行器(Test Runner)決定了JUnit如何執(zhí)行測試。JUnit有多個(gè)測試運(yùn)行器,每個(gè)運(yùn)行器都有特定的功能和用途。
1. 默認(rèn)運(yùn)行器
當(dāng)沒有顯式指定運(yùn)行器時(shí),JUnit會(huì)使用默認(rèn)運(yùn)行器,這在JUnit 4和JUnit 5之間有所不同。
JUnit 4 默認(rèn)運(yùn)行器
在JUnit 4中,默認(rèn)運(yùn)行器是BlockJUnit4ClassRunner
。
import org.junit.Test;
import static org.junit.Assert.assertEquals;public class DefaultRunnerTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
JUnit 5 默認(rèn)運(yùn)行器
在JUnit 5中,不需要顯式指定運(yùn)行器,JUnit Jupiter會(huì)自動(dòng)運(yùn)行測試。
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;public class DefaultRunnerTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
2. @RunWith 注解
@RunWith
注解用于指定JUnit 4的測試運(yùn)行器。以下是一些常用的運(yùn)行器:
2.1 SpringRunner
SpringRunner(原名SpringJUnit4ClassRunner
)用于在JUnit 4中運(yùn)行Spring測試,提供Spring應(yīng)用程序上下文的支持。
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;@RunWith(SpringRunner.class)
public class SpringRunnerTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
2.2 Parameterized
Parameterized
運(yùn)行器用于運(yùn)行參數(shù)化測試,即同一個(gè)測試方法可以用不同的參數(shù)多次運(yùn)行。
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;@RunWith(Parameterized.class)
public class ParameterizedTest {private int input1;private int input2;private int expected;public ParameterizedTest(int input1, int input2, int expected) {this.input1 = input1;this.input2 = input2;this.expected = expected;}@Parameterized.Parameterspublic static Collection<Object[]> data() {return Arrays.asList(new Object[][] {{ 1, 2, 3 },{ 2, 3, 5 },{ 3, 5, 8 }});}@Testpublic void testAdd() {assertEquals(expected, input1 + input2);}
}
2.3 Suite
Suite
運(yùn)行器用于運(yùn)行一組測試類。
import org.junit.runner.RunWith;
import org.junit.runners.Suite;@RunWith(Suite.class)
@Suite.SuiteClasses({TestClass1.class,TestClass2.class
})
public class TestSuite {// 空類,僅用于運(yùn)行指定的測試類
}
3. JUnit 5 擴(kuò)展模型
在JUnit 5中,不使用@RunWith
,而是使用@ExtendWith
注解來擴(kuò)展測試功能。
3.1 SpringExtension
SpringExtension
用于在JUnit 5中運(yùn)行Spring測試,提供Spring應(yīng)用程序上下文的支持。
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;@ExtendWith(SpringExtension.class)
public class SpringExtensionTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
3.2 ParameterizedTest
@ParameterizedTest
注解用于運(yùn)行參數(shù)化測試,即同一個(gè)測試方法可以用不同的參數(shù)多次運(yùn)行。
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;public class ParameterizedTestExample {@ParameterizedTest@CsvSource({"1, 2, 3","2, 3, 5","3, 5, 8"})void testAdd(int input1, int input2, int expected) {assertEquals(expected, input1 + input2);}
}
4. 自定義運(yùn)行器和擴(kuò)展
4.1 自定義JUnit 4 運(yùn)行器
可以創(chuàng)建自定義運(yùn)行器來擴(kuò)展JUnit 4的功能。
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;public class CustomRunner extends BlockJUnit4ClassRunner {public CustomRunner(Class<?> klass) throws InitializationError {super(klass);}@Overrideprotected void runChild(org.junit.runners.model.FrameworkMethod method, org.junit.runner.notification.RunNotifier notifier) {System.out.println("Running test: " + method.getName());super.runChild(method, notifier);}
}import org.junit.Test;
import org.junit.runner.RunWith;@RunWith(CustomRunner.class)
public class CustomRunnerTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
4.2 自定義JUnit 5 擴(kuò)展
可以創(chuàng)建自定義擴(kuò)展來擴(kuò)展JUnit 5的功能。
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;public class CustomExtension implements BeforeEachCallback {@Overridepublic void beforeEach(ExtensionContext context) {System.out.println("Before each test: " + context.getDisplayName());}
}import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;@ExtendWith(CustomExtension.class)
public class CustomExtensionTest {@Testpublic void testAdd() {assertEquals(5, 2 + 3);}
}
5. 使用示例:Spring Boot 測試
結(jié)合使用@ExtendWith
和@SpringBootTest
進(jìn)行Spring Boot應(yīng)用程序的集成測試。
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;@ExtendWith(SpringExtension.class)
@SpringBootTest
public class MyApplicationTests {@Autowiredprivate MyService myService;@Testpublic void testAdd() {assertEquals(5, myService.add(2, 3));}
}
總結(jié)
- JUnit 4中,
@RunWith
用于指定測試運(yùn)行器,常用的運(yùn)行器包括SpringRunner
、Parameterized
、Suite
等。 - JUnit 5中,
@ExtendWith
用于擴(kuò)展測試功能,常用的擴(kuò)展包括SpringExtension
、ParameterizedTest
等。 - 可以創(chuàng)建自定義運(yùn)行器(JUnit 4)或擴(kuò)展(JUnit 5)來滿足特定測試需求。
@SpringBootTest
用于Spring Boot應(yīng)用程序的集成測試。
這些工具和技術(shù)使得JUnit能夠靈活地適應(yīng)各種測試需求。