<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # `SpringRunner`教程 > 原文: [http://zetcode.com/spring/springrunner/](http://zetcode.com/spring/springrunner/) `SpringRunner`教程展示了如何使用`SpringRunner`測試 Spring 應用。 Spring 是流行的 Java 應用框架。 在本教程中,我們使用 Spring 5 版本。 ## `SpringRunner` `SpringRunner`是`SpringJUnit4ClassRunner`的別名,該別名將`JUnit`測試庫與 Spring TestContext Framework 結合在一起。 我們將其與`@RunWith(SpringRunner.class)`一起使用。 使用`SpringRunner`,我們可以實現基于 JUnit 4 的標準單元測試和集成測試。 Spring TestContext Framework 提供了通用的,注解驅動的單元和集成測試支持,這些支持與使用中的測試框架(JUnit,TestNG)無關。 ## `SpringRunner`示例 在以下應用中,我們使用`SprigRunner`測試一個簡單的服務。 該應用是一個 Spring 獨立控制臺應用。 該應用包含兩個屬性文件:一個文件用于生產應用,另一個文件用于測試。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ ├───config │ │ │ AppConfig.java │ │ └───service │ │ HelloService.java │ └───resources │ application.properties │ logback.xml └───test ├───java │ └───com │ └───zetcode │ └───service │ HelloServiceTest.java └───resources appTest.properties ``` 這是項目結構。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>springrunnerex</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> <spring-version>5.1.3.RELEASE</spring-version> </properties> <dependencies> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>1.3</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.6.0</version> <configuration> <mainClass>com.zetcode.Application</mainClass> </configuration> </plugin> </plugins> </build> </project> ``` 這是 Maven 構建文件。 我們具有以下依賴項:`logback-classic`用于記錄日志,`spring-context`和`spring-core`是基本的 Spring 依賴項,`spring-test`用于測試,`hamcrest-all`包含 Hamcrest 匹配庫的所有模塊,而`JUnit`是用于單元測試的庫。 `exec-maven-plugin`幫助執行系統和 Java 程序。 `resources/logback.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <configuration> <logger name="org.springframework" level="ERROR"/> <logger name="com.zetcode" level="INFO"/> <appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <Pattern>%d{HH:mm:ss.SSS} %blue(%-5level) %magenta(%logger{36}) - %msg %n </Pattern> </encoder> </appender> <root> <level value="INFO" /> <appender-ref ref="consoleAppender" /> </root> </configuration> ``` `logback.xml`是 Logback 日志庫的配置文件。 `resources/application.properties` ```java app.message=Hello there! ``` `application.properties`包含一個消息屬性,由`HelloMessage`服務顯示。 `com/zetcode/AppConfig.java` ```java package com.zetcode.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration @ComponentScan(basePackages = "com.zetcode") @PropertySource("application.properties") public class AppConfig { } ``` `AppConfig`配置組件掃描并從提供的文件中加載屬性。 `com/zetcode/servide/HelloService.java` ```java package com.zetcode.service; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @Service public class HelloService { @Value("${app.message}") private String message; public String sayHello() { return message; } } ``` `HelloService`返回從`application.properties`文件檢索到的消息。 `com/zetcode/Application.java` ```java package com.zetcode; import com.zetcode.config.AppConfig; import com.zetcode.service.HelloService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.stereotype.Component; @Component public class Application { private static final Logger logger = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { var ctx = new AnnotationConfigApplicationContext(AppConfig.class); var app = ctx.getBean(Application.class); app.run(); ctx.close(); } @Autowired private HelloService helloService; private void run() { logger.info("Calling hello service"); logger.info(helloService.sayHello()); } } ``` 應用使用`HelloService`將消息打印到控制臺。 ```java $ mvn -q exec:java 17:50:54.118 INFO com.zetcode.Application - Calling hello service 17:50:54.118 INFO com.zetcode.Application - Hello there! ``` 我們運行該應用。 `resources/appTest.properties` ```java app.message=Testing hello message ``` `appTest.properties`專用于測試。 `com/zetcode/service/HelloServiceTest.java` ```java package com.zetcode.service; import com.zetcode.config.AppConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; @RunWith(SpringRunner.class) @ContextConfiguration(classes={HelloService.class}) @TestPropertySource("/appTest.properties") public class HelloServiceTest { @Value("${app.message}") private String message; @Autowired private HelloService helloService; @Test public void testHelloMessage() { var message = helloService.sayHello(); assertThat(message, equalTo(message)); } } ``` `HelloServiceTest`用于測試`HelloService`類。 ```java @RunWith(SpringRunner.class) @ContextConfiguration(classes={HelloService.class}) @TestPropertySource("/appTest.properties") public class HelloServiceTest { ``` 測試類用`@RunWith(SpringRunner.class)`注解。 `@ContextConfiguration`定義了類級別的元數據,用于確定如何加載和配置用于集成測試的應用上下文。 此外,我們還提供了`@TestPropertySource`自定義測試屬性文件。 ```java @Value("${app.message}") private String message; ``` 我們從`appTest.properties`文件注入消息。 ```java @Autowired private HelloService helloService; ``` 我們注入`HelloMessage`服務類。 這是要測試的類。 ```java @Test public void testHelloMessage() { var message = helloService.sayHello(); assertThat(message, equalTo(message)); } ``` 我們測試來自`service`方法的消息是否等于注入的字符串值。 ```java $ mvn -q test ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.zetcode.service.HelloServiceTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.489 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ``` 我們運行測試。 在本教程中,我們展示了如何使用`SpringRunner`在 Spring 應用中創建測試。 [Spring MockMvc 教程](/spring/mockmvc/), [Spring `@PropertySource`教程](/spring/propertysource/), [Java 教程](/lang/java/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看