You can mock concrete classes with Mockito.

If you really want to inject an interface even if you know which
implementation you need, you can use binding annotations.

I don't really see the point of specifying an IFileReader if you know you
need a caching or HTTP-aware version. Similarly, binding annotations are
often a weaker and more difficult to understand version of what the
standard type system gives you.

Moandji
On Dec 16, 2012 3:05 PM, "Dirk Nimerem" <[email protected]> wrote:

> Hello Moandji,
>
> I want to test the CachedFileReader later with a simple test and a mock a
> IFileReader like this:
>
> IFileReader mockreader = MockCreater.createMock(IFileReader filereader);
> CachedFileReader objecttotest = new CachedFileReader(filereader);
>
> Is it a good idea to inject the FileReader directly into the
> CachedFileReader? My CachedFileReader class then directly depends on a
> concrete class not on an interface. I imagine I could create another
> HttpFileReader class. Then my CachedFileReader should use the
> HttpFileReader within the constructor via guice not the "normal" FileReader
> class. Is there a solution for this problem?
>
> But this solution is at least much better than my current workaround,
> thank you.
>
> Dirk
>
> Am Sonntag, 16. Dezember 2012 12:27:48 UTC+1 schrieb Moandji Ezana:
>>
>> Simply inject FileReader into CachedFileReader instead of IFileReader.
>>
>> Moandji
>> On Dec 16, 2012 1:11 PM, "Dirk Nimerem" <[email protected]> wrote:
>>
>>> Hello,
>>>
>>> i defined the following interface:
>>>
>>> @ImplementedBy(FileSystem.**class)
>>> public interface IFileSystem {
>>> public String getFileContent(String filepath) throws IOException;
>>> public long getLastModified(String filepath) throws IOException;
>>> }
>>>
>>> and implementing class:
>>>
>>> @Singleton
>>> public class FileReader implements IFileReader {
>>> @Override
>>> public String getFileContent(String filepath) throws IOException {
>>> return FileUtils.readFileToString(new File(filepath));
>>> }
>>>
>>> @Override
>>> public String getLastModified(String filepath) throws IOException {
>>> return (new File(filepath)).lastModified()**;
>>> }
>>> }
>>>
>>> This should encapsulate these file operations on the harddisk. And I'm
>>> also able to mock this interface while testing my classes.
>>> Now i would like to provide a CachedFileReader implementing which should
>>> cache any file *(I keep the example as small as possible without take
>>> note on file changes or anything else)*:
>>>
>>> @Singleton
>>> public class CachedFileReader implements IFileReader {
>>> private final HashMap<String, String> cache = new HashMap<String,
>>> String> ();
>>> private final IFileReader filereader;
>>>
>>> @Override
>>> public String getFileContent(String filepath) throws IOException {
>>> String key = filepath + String.valueOf(**getLastModified(filepath)); //
>>> I know this is a crap solution, its just for this example
>>>
>>> if (!(cache.containsKey(filepath)**) {
>>> cache.put(filepath, filereader.getFileContent(file**path));
>>> }
>>> return cache.get(filepath);
>>> }
>>>
>>> @Override
>>> public String getLastModified(String filepath) throws IOException {
>>> return filereader.getLastModified(**filepath);
>>> }
>>>
>>> @Inject
>>> public CachedFileReader(IFileReader filereader) {
>>> this.filereader = filereader;
>>> }
>>>  }
>>>
>>> I haven't inherited from FileReader because I want later be able to test
>>> the CachedFileReader itself with a IFileReader mock object passed to the
>>> constructor.
>>> Also I changed the @ImplementedBy Annotation of my IFileReader interface
>>> to CachedFileReader. So every class which needs a IFileReader now gets
>>> the CachedFileReader instance.
>>>
>>> But how can I tell Google Juice tell that it should inject the normal
>>> FileReader to the constructor of the CachedFileReader instead of itself? Is
>>> there an annotation for that? I have read about multibindings but it didn't
>>> helped me. Or is there a better solution for my concern?
>>>
>>> Thank you in advance,
>>> Dirk
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "google-guice" group.
>>> To view this discussion on the web visit https://groups.google.com/d/**
>>> msg/google-guice/-/**Q0mkVSYi3RsJ<https://groups.google.com/d/msg/google-guice/-/Q0mkVSYi3RsJ>
>>> .
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to google-guice...@**
>>> googlegroups.com.
>>> For more options, visit this group at http://groups.google.com/**
>>> group/google-guice?hl=en<http://groups.google.com/group/google-guice?hl=en>
>>> .
>>>
>>  --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-guice/-/dNFiHY_eurwJ.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to