springboot学习day03

73 阅读1分钟

整合junit测试

  • 添加pom.xml
<dependency>  
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-test</artifactId>  
</dependency>

<dependency>  
<groupId>junit</groupId>  
<artifactId>junit</artifactId>  
<scope>test</scope>  
</dependency>
  • test目录下创建测试类

package com.ivan.test;  
@RunWith(SpringRunner.class)  
@SpringBootTest(classes = Main.class)  
public class UserServiceTest {  
    @Autowired  
    private UserService userService;  
    @Test  
    public void testAdd(){  
        userService.add();  
    }  
}

整合redis

  • pom.xml配置
<dependency>  
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-starter-data-redis</artifactId>  
</dependency>
  • 配置连接信息
server:  
    port: 8081  
spring:  
    config:  
        activate:  
            on-profile: dev  
    data:  
        redis:  
            host: 127.0.0.1  
            port: 6379
  • 代码调用
@Autowired  
private RedisTemplate redisTemplate;  
@Test  
public void testSet(){  
    redisTemplate.boundValueOps("name").set("zhangsan");  
}  
@Test  
public void testGet(){  
    Object name= redisTemplate.boundValueOps("name").get();  
    System.out.println(name);  
}