我如何注释我的 JUnit 测试,以便它 运行 我的 Spring 启动应用程序 运行 的方式?
How do I annotate my JUnit test so it will run the way my Spring Boot app runs?
我有一个 Spring 引导 Web 应用程序,我通过 运行 启动这个 class ...
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Web 应用程序有一个 JSP/HTML 前端,由 Spring MVC 控制器提供服务,它与服务对话,DAO 使用 Hibernate 与 read/write 实体对话 MySQL 数据库.
所有组件和服务都已实例化,@Autowired 和 Web 应用程序 运行 没问题。
现在,我想构建 JUnit 测试并测试服务或 DAO 中的一些功能。
我开始编写如下所示的 JUnit 测试,但我很快就卡住了,不知道如何实例化所有 @Autowired 组件和 classes。
public class MySQLTests {
@Test
public void test000() {
assertEquals("Here is a test for addition", 10, (7+3));
}
@Autowired
UserService userService = null;
@Test
public void test001() {
userService.doSomething("abc123");
// ...
}
}
我基本上希望 Web 应用程序启动并 运行,然后让 JUnit 测试 运行 这些服务中的方法。
我需要一些入门帮助...是否有某种 JUnit 等效于 @SpringBootApplication 注释,我可以在我的 JUnit class 中使用?
回答我自己的问题...我是这样工作的...
对测试 class 进行了注释:
@RunWith(SpringRunner.class)
@SpringBootTest
测试 class 必须在 @Controller class 之上的包中...所以我的测试 class 在 com.projectname.*
控制器是 com.projectname.controller.*
工作代码如下所示...
package com.projectname;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.projectname.controller.WebController;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test1 {
@Autowired
private WebController controller;
@Test
public void contextLoads() throws Exception {
assertNotNull("WebController should not be null", controller);
}
}
我有一个 Spring 引导 Web 应用程序,我通过 运行 启动这个 class ...
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Web 应用程序有一个 JSP/HTML 前端,由 Spring MVC 控制器提供服务,它与服务对话,DAO 使用 Hibernate 与 read/write 实体对话 MySQL 数据库.
所有组件和服务都已实例化,@Autowired 和 Web 应用程序 运行 没问题。
现在,我想构建 JUnit 测试并测试服务或 DAO 中的一些功能。
我开始编写如下所示的 JUnit 测试,但我很快就卡住了,不知道如何实例化所有 @Autowired 组件和 classes。
public class MySQLTests {
@Test
public void test000() {
assertEquals("Here is a test for addition", 10, (7+3));
}
@Autowired
UserService userService = null;
@Test
public void test001() {
userService.doSomething("abc123");
// ...
}
}
我基本上希望 Web 应用程序启动并 运行,然后让 JUnit 测试 运行 这些服务中的方法。
我需要一些入门帮助...是否有某种 JUnit 等效于 @SpringBootApplication 注释,我可以在我的 JUnit class 中使用?
回答我自己的问题...我是这样工作的...
对测试 class 进行了注释:
@RunWith(SpringRunner.class)
@SpringBootTest
测试 class 必须在 @Controller class 之上的包中...所以我的测试 class 在
com.projectname.*
控制器是com.projectname.controller.*
工作代码如下所示...
package com.projectname;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.projectname.controller.WebController;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test1 {
@Autowired
private WebController controller;
@Test
public void contextLoads() throws Exception {
assertNotNull("WebController should not be null", controller);
}
}