单元测试的pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
在test包下面创建单元测试类:
单元测试类
测试代码
import com.pump.demopro.DemoProApplication;
import com.pump.demopro.rabbitmqtest.producer.Sender;
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.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.Date;
/**
* @ProjectName: demo-pro
* @Package: com.pump.rabbitmqtest
* @ClassName: RabbitTests
* @Author: pump
* @Description:
* @Date: 2023-11-02 9:30:50
* @Version: 1.0
*/
@ActiveProfiles("dev")
@RunWith(value= SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoProApplication.class)
public class RabbitTests {
@Autowired
private Sender sender;
@Test
public void sendTest() throws Exception {
while(true){
String msg = new Date().toString();
sender.send(msg);
Thread.sleep(1000);
}
}
}
- 指定单元测试使用的配置文件:@ActiveProfiles("dev")
- 指定单元测试的启动类:@SpringBootTest(classes = DemoProApplication.class)
- 指定单元测试的方法:@Test
网友评论