Java RedisTemplate mock仅在测试类中工作
现在,当我为类B中的方法编写测试时,即someMethod()Java RedisTemplate mock仅在测试类中工作,java,spring-boot,junit,redis,microservices,Java,Spring Boot,Junit,Redis,Microservices,现在,当我为类B中的方法编写测试时,即someMethod() @RunWith(SpringJUnit4ClassRunner.class) @春靴测试 @ActiveProfiles(“测试”) ClassBTest(){ @注射模拟 B类 @嘲弄 @限定符(“模板”) 再发现模板再发现模板; @嘲弄 哈希操作哈希操作; @试验 someMethodTest(){ Mockito.when(redisTemplate.opsForHash()).thenReturn(hashOperatio
@RunWith(SpringJUnit4ClassRunner.class)
@春靴测试
@ActiveProfiles(“测试”)
ClassBTest(){
@注射模拟
B类
@嘲弄
@限定符(“模板”)
再发现模板再发现模板;
@嘲弄
哈希操作哈希操作;
@试验
someMethodTest(){
Mockito.when(redisTemplate.opsForHash()).thenReturn(hashOperations);
Mockito.doReturn(“aa”).when(hashOperations).get(Mockito.any(),Mockito.any());
somethod();
//断言
}
}
我观察到redis模板模拟是在测试类中生成的,而不是在类A和类B中生成的
类内测试
B类内部
为什么模拟在B类和A类中不起作用?您没有将模拟注入到正在测试的服务中。可以在要测试的类之前使用
@injectmock
完成此操作。您可以尝试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@ActiveProfiles("test")
ClassBTest(){
@InjectMocks
Class B
@Mock
@Qualifier("redisTemplate")
RedisTemplate<String, Object> redisTemplate;
@Mock
HashOperations<String, Object, Object> hashOpertaions;
@Test
someMethodTest(){
Mockito.when(redisTemplate.opsForHash()).thenReturn(hashOpertaions);
Mockito.doReturn("aa").when(hashOpertaions).get(Mockito.any(), Mockito.any());
someMethod();
//assertions
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@春靴测试
@ActiveProfiles(“测试”)
ClassBTest(){
@嘲弄
再发现模板再发现模板;
@嘲弄
哈希操作哈希操作;
@注射模拟
bObjectOfB;//这将把创建的模拟注入到类中
@试验
someMethodTest(){
Mockito.when(redisTemplate.opsForHash()).thenReturn(hashOperations);
Mockito.doReturn(“aa”).when(hashOperations).get(Mockito.any(),Mockito.any());
objectOfB.someMethod();
//断言
}
}
我的问题现在解决了。这对我有用
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@ActiveProfiles("test")
ClassBTest(){
@Mock
RedisTemplate<String, Object> redisTemplate;
@Mock
HashOperations<String, Object, Object> hashOpertaions;
@InjectMocks
B objectOfB; // this would inject the created mocks into the class
@Test
someMethodTest(){
Mockito.when(redisTemplate.opsForHash()).thenReturn(hashOpertaions);
Mockito.doReturn("aa").when(hashOpertaions).get(Mockito.any(), Mockito.any());
objectOfB.someMethod();
//assertions
}
}
@MockBean
@限定符(“模板”)
再发现模板再发现模板;
@蚕豆
哈希操作哈希操作;
@蚕豆
RedisKeyValueAdapter RedisKeyValueAdapter;
我正在这样做,在写问题时错过了它
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@ActiveProfiles("test")
ClassBTest(){
@InjectMocks
Class B
@Mock
@Qualifier("redisTemplate")
RedisTemplate<String, Object> redisTemplate;
@Mock
HashOperations<String, Object, Object> hashOpertaions;
@Test
someMethodTest(){
Mockito.when(redisTemplate.opsForHash()).thenReturn(hashOpertaions);
Mockito.doReturn("aa").when(hashOpertaions).get(Mockito.any(), Mockito.any());
someMethod();
//assertions
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@ActiveProfiles("test")
ClassBTest(){
@Mock
RedisTemplate<String, Object> redisTemplate;
@Mock
HashOperations<String, Object, Object> hashOpertaions;
@InjectMocks
B objectOfB; // this would inject the created mocks into the class
@Test
someMethodTest(){
Mockito.when(redisTemplate.opsForHash()).thenReturn(hashOpertaions);
Mockito.doReturn("aa").when(hashOpertaions).get(Mockito.any(), Mockito.any());
objectOfB.someMethod();
//assertions
}
}
@MockBean
@Qualifier("redisTemplate")
RedisTemplate<String, Object> redisTemplate;
@MockBean
HashOperations<String, Object, Object> hashOpertaions;
@MockBean
RedisKeyValueAdapter redisKeyValueAdapter;