阿超
>
在SpringBoot单元测试中添加参数
我的肩上是风,风上是闪烁的星群。——北岛《结局或开始·献给遇罗克》
当时是解决一个issue
而去查阅的
https://github.com/baomidou/mybatis-plus/issues/4417
最后代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| package com.ruben.mybatisplusissue;
import javax.annotation.Resource;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.SpringApplication; import org.springframework.boot.test.context.SpringBootContextLoader; import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.test.context.BootstrapWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextLoader; import org.springframework.test.context.junit.jupiter.SpringExtension;
import com.baomidou.mybatisplus.core.toolkit.AES; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.ruben.mybatisplusissue.mapper.UserMapper; import com.zaxxer.hikari.HikariDataSource;
@ContextConfiguration(classes = MybatisPlusIssueApplication.class) @ExtendWith(SpringExtension.class) @BootstrapWith(MybatisPlusIssueApplicationTests.Bootstrapper.class) class MybatisPlusIssueApplicationTests {
static class Bootstrapper extends SpringBootTestContextBootstrapper {
static class ArgumentSupplyingContextLoader extends SpringBootContextLoader {
@Override protected SpringApplication getSpringApplication() { return new SpringApplication() { @Override public ConfigurableApplicationContext run(String... args) { return super.run("--mpw.key=de8accd4774af9eb"); } }; } }
@Override protected Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass) { return ArgumentSupplyingContextLoader.class; } }
@Resource private UserMapper userMapper; @Resource private HikariDataSource hikariDataSource;
@Test void testRestart() { userMapper.selectList(Wrappers.lambdaQuery()); String randomKey = AES.generateRandomKey(); String url = AES.encrypt(hikariDataSource.getJdbcUrl(), randomKey); String username = AES.encrypt(hikariDataSource.getUsername(), randomKey); String password = AES.encrypt(hikariDataSource.getPassword(), randomKey); System.out.println("randomKey:[ --mpw.key=" + randomKey + " ]"); System.out.println("url:[ mpw:" + url + " ]"); System.out.println("username:[ mpw:" + username + " ]"); System.out.println("password:[ mpw:" + password + " ]"); } }
|
这里--mpw.key=de8accd4774af9eb
即为添加的参数