spring-boot service层junit单元测试

只要加@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = StartupApplication.class)

两个注解就可以引入service进行单元测试了

 

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = StartupApplication.class)
public class OrderBookUserTest {

    @Autowired
    private OrderBookUserService orderBookUserService;
    
    
    @Test
    public void createOrderBook(){
//        System.out.println(userRepository);
        orderBookUserService.createOrderBook(5, 24, 1.5, 300 , 20, 1, OrderBookDirection.BUY);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然,我可以帮你编写一个Spring Boot Starter Data Redis的测试demo。首先,你需要在你的项目中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 接下来,你可以创建一个简单的Redis配置文件,例如`RedisConfig.java`: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; @Configuration public class RedisConfig { @Value("${spring.redis.host}") private String redisHost; @Value("${spring.redis.port}") private int redisPort; @Bean public RedisConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration config = new RedisStandaloneConfiguration(redisHost, redisPort); return new LettuceConnectionFactory(config); } @Bean public RedisTemplate<String, String> redisTemplate() { RedisTemplate<String, String> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory()); return template; } } ``` 这里使用了Lettuce作为Redis的客户端库。 接下来,你可以创建一个简单的Service类来进行Redis操作,例如`RedisService.java`: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class RedisService { private final RedisTemplate<String, String> redisTemplate; @Autowired public RedisService(RedisTemplate<String, String> redisTemplate) { this.redisTemplate = redisTemplate; } public void setValue(String key, String value) { redisTemplate.opsForValue().set(key, value); } public String getValue(String key) { return redisTemplate.opsForValue().get(key); } } ``` 最后,你可以创建一个简单的测试类来验证Redis操作,例如`RedisTest.java`: ```java import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest public class RedisTest { @Autowired private RedisService redisService; @Test public void testRedis() { String key = "myKey"; String value = "myValue"; // 设置值 redisService.setValue(key, value); // 获取值 String retrievedValue = redisService.getValue(key); System.out.println(retrievedValue); // 输出:myValue } } ``` 现在,你可以运行`RedisTest`类来测试Redis操作。 这就是一个简单的Spring Boot Starter Data Redis的测试demo。希望对你有所帮助!如果你有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值