Код: Выделить всё
@Repository
public class RedisService {
private final JedisPooled jedisPooled;
@Autowired
public RedisService(final JedisPooled jedisPooled) {
this.jedisPooled = jedisPooled;
}
public String set(final String key, final String val) {
return jedisPooled.set(key, val);
}
public String get(final String key) {
return jedisPooled.get(key);
}
}
Код: Выделить всё
@Configuration
public class RedisServiceConfig {
@Bean
public RedisService redisService(@Value("${redis.auth.password}") final String redisAuthPassword,
@Value("${redis.endpoint}") final String redisEndpoint,
@Value("${redis.port}") final int redisPort) {
final JedisPooled jedisPooled;
try {
jedisPooled = new JedisPooled(redisEndpoint, redisPort, redisAuthPassword);
} catch (final Exception e) {
throw new JedisConnectionException("Error connecting to Redis");
}
return new RedisService(jedisPooled);
}
}
< /code>
Как я могу проверить обработку исключений в классе конфигурации? Я пробовал что-то подобное, но я не могу заставить его работать: < /p>
public class RedisServiceConfigTest {
@Mock
private JedisPooled mockJedisPooled;
@InjectMocks
private RedisServiceConfig redisServiceConfig;
@Test
public void testRedisServiceBeanCreation() {
when(mockJedisPooled.created()).thenThrow(new JedisConnectionException("Connection error"));
// I need something ^ like this?
final RedisService redisService = redisServiceConfig.redisService("testPassword", "localhost", 6379);
Assertions.assertNotNull(redisService);
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... nfig-class