Springboot单元测试

org.springframework.boot spring-boot-starter-test test

【Springboot单元测试】
package test; import cn.test.Application; import cn.test.bean.User; import cn.test.mapper.UserMapper; import org.junit.Assert; 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 org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) @EnableTransactionManagement //事务回滚,单元测试类不对数据库操作 @Transactional public class JunitT { @Autowired private UserMapper userMapper; @Test public void test1(){ User user=new User(); user.setUsername("huang"); userMapper.insertSelective(user); // 注意使用的tkmybatis,这里想要获得user.getId()必须在实体类的主键id上加注解 //@GeneratedValue(generator = "JDBC")//加上注解 System.out.println(user.getId()); Assert.assertTrue(user.getId().equals("20")); } }


    推荐阅读