Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can we make class Redis not final #25

Closed
layesuen opened this issue Oct 10, 2014 · 5 comments
Closed

Can we make class Redis not final #25

layesuen opened this issue Oct 10, 2014 · 5 comments

Comments

@layesuen
Copy link

I love the library, great work guys.

Just want see if it's possible to get rid of the final of Redis class. Otherwise, it's impossible to mock it in unit test.

Thanks

@curreli
Copy link
Contributor

curreli commented Oct 13, 2014

Sure, that's feasible.

Out of curiosity, could you explain why you would need to mock it?

@layesuen
Copy link
Author

Int most of the time, in our unit tests, we don't want to actually talk to the actual Redis server. Instead, we talk to the mocked scredis.Redis object which is prepared by mocking libraries like Mockito.

For example,

val mockRedisServer = mock[Redis]
when(mockRedisServer.keys("bat:queue:*")).thenReturn(Future.successful(Set[String]("bat:queue:abc", "bat:queue:jkl")))
when(mockRedisServer.lLen("bat:queue:abc")).thenReturn(Future.successful(3l))
when(mockRedisServer.lLen("bat:queue:jkl")).thenReturn(Future.successful(2l))
when(mockRedisServer.lRange[String]("bat:queue:abc", 0, 2)).thenReturn(Future.successful(List[String](
  """["user1", "http://diary/1", "diary_meal", 28, "http://partner1_callback"]""",
  """["user1", "http://diary/2", "diary_meal", 28, "http://partner1_callback"]""",
  """["user1", "http://diary/3", "diary_meal", 28, "http://partner1_callback"]"""
)))
when(mockRedisServer.lRange[String]("bat:queue:jkl", 0, 1)).thenReturn(Future.successful(List[String](
  """["user1", "http://diary/1", "diary_meal", 29, "http://partner2_callback"]""",
  """["user1", "http://diary/2", "diary_meal", 29, "http://partner2_callback"]"""
)))
when(mockRedisServer.rPopLPush[String](org.mockito.Matchers.eq("agg:queue"), any[String])(any[scredis.serialization.Reader[String]]))
  .thenReturn(Future.successful(None))

then in the unit test, I use this mocked object instead of the real one.

Does that make sense?

Thanks!

@curreli
Copy link
Contributor

curreli commented Oct 14, 2014

Makes sense. I'm going to remove the final modifier for all relevant classes in the next minor release.

@curreli curreli modified the milestone: 2.0.4 Oct 14, 2014
@curreli
Copy link
Contributor

curreli commented Oct 14, 2014

Fixed here. Released under version 2.0.4.

@curreli curreli closed this as completed Oct 14, 2014
@layesuen
Copy link
Author

Wow, that's quick. Now I can get rid of my local overrides 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants