Skip to content

Commit bc7240e

Browse files
committed
Adding Java config
1 parent 97ca198 commit bc7240e

4 files changed

Lines changed: 54 additions & 23 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.baeldung.spring.data.redis.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
6+
import org.springframework.data.redis.core.RedisTemplate;
7+
8+
@Configuration
9+
public class RedisConfig {
10+
@Bean
11+
JedisConnectionFactory jedisConnectionFactory() {
12+
return new JedisConnectionFactory();
13+
}
14+
15+
@Bean
16+
public RedisTemplate< String, Object> redisTemplate() {
17+
final RedisTemplate< String, Object> template = new RedisTemplate<String, Object>();
18+
template.setConnectionFactory(jedisConnectionFactory());
19+
return template;
20+
}
21+
}

‎spring-data-redis/src/main/java/org/baeldung/spring/data/redis/model/Student.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Student implements Serializable {
77
private static final long serialVersionUID = -1907106213598514113L;
88

99
public enum Gender {
10-
Male, Female
10+
MALE, FEMALE
1111
}
1212

1313
private String id;

‎spring-data-redis/src/main/java/org/baeldung/spring/data/redis/repo/StudentRepositoryImpl.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22

33
import org.baeldung.spring.data.redis.model.Student;
44
import org.springframework.data.redis.core.RedisTemplate;
5+
import org.springframework.stereotype.Repository;
56

67
import java.util.Map;
78

9+
@Repository
810
public class StudentRepositoryImpl implements StudentRepository {
911

1012
private static final String KEY = "Student";
1113

12-
private RedisTemplate<String, Student> redisTemplate;
14+
private RedisTemplate<String, Object> redisTemplate;
1315

14-
public void setRedisTemplate(RedisTemplate<String, Student> redisTemplate) {
16+
public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
1517
this.redisTemplate = redisTemplate;
1618
}
1719

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package org.baeldung.spring.data.redis.repo;
22

3+
import org.baeldung.spring.data.redis.config.RedisConfig;
34
import org.baeldung.spring.data.redis.model.Student;
5+
import org.junit.Before;
46
import org.junit.Test;
57
import org.junit.runner.RunWith;
6-
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.test.context.ContextConfiguration;
89
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
910

@@ -13,46 +14,53 @@
1314
import static org.junit.Assert.assertNull;
1415

1516
@RunWith(SpringJUnit4ClassRunner.class)
16-
@ContextConfiguration(locations = { "classpath:/spring-config.xml" })
17+
//@ContextConfiguration(locations = { "classpath:/spring-config.xml" })
18+
@ContextConfiguration(classes = RedisConfig.class)
1719
public class StudentRepositoryTest {
1820

19-
@Autowired
20-
private StudentRepository studentRepository;
21+
private StudentRepositoryImpl studentRepositoryImpl;
22+
23+
@Before
24+
public void setUp(){
25+
studentRepositoryImpl = new StudentRepositoryImpl();
26+
RedisConfig redisConfig = new RedisConfig();
27+
studentRepositoryImpl.setRedisTemplate(redisConfig.redisTemplate());
28+
}
2129

2230
@Test
2331
public void whenSavingStudent_thenAvailableOnRetrieval() throws Exception {
24-
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.Male, 1);
25-
studentRepository.saveStudent(student);
26-
final Student retrievedStudent = studentRepository.findStudent(student.getId());
32+
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
33+
studentRepositoryImpl.saveStudent(student);
34+
final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId());
2735
assertEquals(student.getId(), retrievedStudent.getId());
2836
}
2937

3038
@Test
3139
public void whenUpdatingStudent_thenAvailableOnRetrieval() throws Exception {
32-
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.Male, 1);
33-
studentRepository.saveStudent(student);
40+
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
41+
studentRepositoryImpl.saveStudent(student);
3442
student.setName("Richard Watson");
35-
studentRepository.saveStudent(student);
36-
final Student retrievedStudent = studentRepository.findStudent(student.getId());
43+
studentRepositoryImpl.saveStudent(student);
44+
final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId());
3745
assertEquals(student.getName(), retrievedStudent.getName());
3846
}
3947

4048
@Test
4149
public void whenSavingStudents_thenAllShouldAvailableOnRetrieval() throws Exception {
42-
final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.Male, 1);
43-
final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.Male, 2);
44-
studentRepository.saveStudent(engStudent);
45-
studentRepository.saveStudent(medStudent);
46-
final Map<Object, Object> retrievedStudent = studentRepository.findAllStudents();
50+
final Student engStudent = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
51+
final Student medStudent = new Student("Med2015001", "Gareth Houston", Student.Gender.MALE, 2);
52+
studentRepositoryImpl.saveStudent(engStudent);
53+
studentRepositoryImpl.saveStudent(medStudent);
54+
final Map<Object, Object> retrievedStudent = studentRepositoryImpl.findAllStudents();
4755
assertEquals(retrievedStudent.size(), 2);
4856
}
4957

5058
@Test
5159
public void whenDeletingStudent_thenNotAvailableOnRetrieval() throws Exception {
52-
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.Male, 1);
53-
studentRepository.saveStudent(student);
54-
studentRepository.deleteStudent(student.getId());
55-
final Student retrievedStudent = studentRepository.findStudent(student.getId());
60+
final Student student = new Student("Eng2015001", "John Doe", Student.Gender.MALE, 1);
61+
studentRepositoryImpl.saveStudent(student);
62+
studentRepositoryImpl.deleteStudent(student.getId());
63+
final Student retrievedStudent = studentRepositoryImpl.findStudent(student.getId());
5664
assertNull(retrievedStudent);
5765
}
5866
}

0 commit comments

Comments
 (0)