@Christian:

Thanks for your answer, I ended up with the following helper method:

    public static void set(Object object, String fieldName, Object 
fieldValue) {
        Field field = null;
        Class<?> clazz = object.getClass();
        
        while(field == null) {
            try {
                field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                field.set(object, fieldValue);
                break;
            } catch (Exception ex) {
                if (null != clazz.getSuperclass()) {
                    clazz = clazz.getSuperclass();
                } else {
                    throw new RuntimeException(ex);
                }
            }
        }
    }

@Simone:
Is it correct the maven repositories for the logging project are not 
working (yet)? I tried including it in my maven pom.xml but it can't fetch 
the dependency


Op vrijdag 25 januari 2013 18:01:12 UTC+1 schreef Christian Gruber het 
volgende:
>
> Make a method like this
>
> public static void set(Object object, String fieldName, Object fieldValue) {
>   Class<?> objectClass = object.getClass();
>   String fieldName = (String) fieldValues[i];
>   try {
>     Field field = findField(objectClass, fieldName);
>     field.setAccessible(true);
>     field.set(object, fieldValue);
>   } catch (Exception ex) {
>     throw new RuntimeException(String.format("Unable to set field '%s' of %s 
> to %s: %s",
>         fieldName, object, fieldValue, ex.getMessage()), ex);
>   }
> }
>
> … and use it in your test infrastructure to:
>
>   Utils.set(testInstance, "logger", mock(Logger));
>
> or something like that.
>
> Christian.
>
> On 23 Jan 2013, at 5:54, Bram wrote:
>
> Im working on implementing Guice in some existing code. I use log4j2 as
> logger in that code, so I implemented an injector using this example:
> http://code.google.com/p/google-guice/wiki/CustomInjections
>
> I'm injecting my logger in the following way:
>
> clas Test {
> @InjectLogger
> private Logger logger;
> }
>
> This works perfectly, however all of my unit tests break because of logging
> statements in the code and no logger injected (thus: nullpointer
> exception). Ofcourse I understand why this is, but I dont know the best way
> to solve this.
>
> I can obviously add a setter for the logger that I call in my unit test
> set-up, but is that realy a proper solution?
> ------------------------------
>
> 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]<javascript:>
> .
> To unsubscribe from this group, send email to 
> [email protected] <javascript:>.
> Visit this group at http://groups.google.com/group/google-guice?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
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].
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to