Springboot--Junit
Posted 周无极
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot--Junit相关的知识,希望对你有一定的参考价值。
Springboot--Junit
package com.bjsxt.dao; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl { public void saveUser(){ System.out.println("insert into users....."); } }
package com.bjsxt.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.bjsxt.dao.UserDaoImpl; @Service public class UserServiceImpl { @Autowired private UserDaoImpl userDaoImpl; public void addUser(){ this.userDaoImpl.saveUser(); } }
package com.bjsxt; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
package com.bjsxt.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.test.context.junit4.SpringJUnit4ClassRunner; import com.bjsxt.App; import com.bjsxt.service.UserServiceImpl; /** * SpringBoot测试类 *@RunWith:启动器 *SpringJUnit4ClassRunner.class:让junit与spring环境进行整合 * *@SpringBootTest(classes={App.class}) 1,当前类为springBoot的测试类 *@SpringBootTest(classes={App.class}) 2,加载SpringBoot启动类。启动springBoot * *junit与spring整合 @Contextconfiguartion("classpath:applicationContext.xml") */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes={App.class}) public class UserServiceTest { @Autowired private UserServiceImpl userServiceImpl; @Test public void testAddUser(){ this.userServiceImpl.addUser(); } }
<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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> </parent> <groupId>com.bjsxt</groupId> <artifactId>19-spring-boot-test</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 添加junit环境的jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies> </project>
以上是关于Springboot--Junit的主要内容,如果未能解决你的问题,请参考以下文章