spring boot不启动的情况下进行mongodb和redis访问测试

那年仲夏 提交于 2021-01-18 19:38:34

现在spring boot越来越重,每次要测试个什么东西必须要启动boot然后调用controller或者用boot test写单元测试,这两种我个人觉得都比较麻烦;我这边平常测试mongodb和redis处理都是直接写main代码进行测试,为了使用到spring提供的template类,我们需要自己初始化mongo和redis的客户端连接:

mongodb连接初始化:


import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

import java.util.Arrays;

public class TestMongoDB {
    public static void main(String[] args) {
        //连接信息
        String host = "192.168.1.200";
        int port = 27017;
        String userName = "root";
        String password = "root";
        String databaseName = "mongodb_schema";

        
        MongoCredential credential = MongoCredential.createScramSha1Credential(userName, databaseName, password.toCharArray());
        ServerAddress serverAddress = new ServerAddress(host, port);
        MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(credential));
        SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, databaseName);
        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
        MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, new MongoMappingContext());
        converter.setTypeMapper(new DefaultMongoTypeMapper(null));
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, converter);

        //TODO, mongoTemplate操作

    }

}

redis连接初始化:



import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Slf4j
public class TestRedisClient {
    public static void main(String[] args) {
        //单机模式
        RedisStandaloneConfiguration rsc = new RedisStandaloneConfiguration();
        rsc.setPort(6379);
        rsc.setPassword("redispwd");
        rsc.setHostName("192.168.1.200");

        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer()); //key序列化采用string, value默认用的是java序列化(JdkSerializationRedisSerializer)
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        //单机模式
        LettuceConnectionFactory fac = new LettuceConnectionFactory(rsc);
        fac.afterPropertiesSet();
        redisTemplate.setConnectionFactory(fac);
        redisTemplate.afterPropertiesSet();


        //TODO,测试redistempalte

    }
}

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!