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].
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.

--
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to 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