1.SpringBoot开发框架里提供有“spring-test”测试程序依赖只需要导入依赖库即可
1 <dependency> 2 <groupId>junit</groupId> 3 <artifactId>junit</artifactId> 4 <scope>test</scope> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-test</artifactId> 9 <scope>test</scope> 10 </dependency>
2.测试程序,同样要注意注解
1 package com.lion.test; 2 import com.lion.StartBootMain; 3 import com.lion.action.MessageAction; 4 import org.junit.Test; 5 import org.junit.runner.RunWith; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.boot.test.context.SpringBootTest; 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 9 import org.springframework.test.context.web.WebAppConfiguration; 10 @RunWith(SpringJUnit4ClassRunner.class) 11 @WebAppConfiguration 12 @SpringBootTest(classes = StartBootMain.class) 13 public class MessageActionTest { 14 @Autowired 15 private MessageAction messageAction ; 16 @Test 17 public void testEcho() { 18 System.out.println(messageAction.echo()); 19 } 20 }