【第16期】springboot: IDEA下Junit4单元测
2020-05-06 本文已影响0人
村中一少
一、环境配置
IDEA默认安装了JUnit 插件,没有的话可以参考以下步骤。
JUnit插件安装步骤如下:
File-->settings-->Plguins-->Browse repositories-->输入JUnit-->选择JUnit安装。
二、创建工程
三、创建一个test文件夹用于存放测试类
默认情况下会有test包,注意与main处于同一级别,没有的话自己创建,需要引进依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
四、编写测试类
测试类前加入注解@SpringBootTest(classes = xx.class),xx为工程的启动类,本工程的启动类为StreamSenderApplication。
测试方法前加上注解@Test。
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.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.context.junit4.SpringRunner;
import com.agan.book.stream.ISendService;
import com.agan.book.stream.StreamSenderApplication;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = StreamSenderApplication.class)
public class StreamTests {
@Autowired
private ISendService send;
@Test
public void send() throws InterruptedException {
String msg="test...............";
Message message=MessageBuilder.withPayload(msg.getBytes()).build();
this.send.send().send(message);
}
}
五、设置test目录为Test Resources Root
test目录灰色状态下一般不能直接运行,在此目录上右击鼠标并将此目录标记为Test Resources Root
设置成功后,测试类颜色发生变化
六 右键运行测试类即可