I am an Android developer and trying to learn Dagger2. I have gone through some tutorials and got some basic understanding. I developed a basic java app using Dagger2. And below is my app code.
Logger.java: Logger is simple java class which will print logs with some tag.
public class Logger {
private String tag;
#Inject
public Logger(String tag) {
this.tag = tag;
}
public void log(String msg) {
System.out.println(tag + "::" + msg);
}
}
InvertNumber.java: It will invert the passed number and prints log using Logger
public class InvertNumber {
private Logger logger;
#Inject
public InvertNumber(Logger logger) {
this.logger = logger;
}
public void invert(int i) {
logger.log("Inverted value is " + (i * -1));
}
}
Now I added Dagger2 depency classes (Module and component) like below
#Module
public class NumberModule {
private String tag;
public NumberModule(String tag) {
this.tag = tag;
}
#Provides
Logger provideLogger(){
Logger logger = new Logger(tag);
return logger;
}
#Provides
InvertNumber provideTempManager(Logger logger){
return new InvertNumber(logger);
}
}
#Component(modules = NumberModule.class)
public interface NumberComponent {
InvertNumber getInvertNumber();
}
Now below is my main method.
public static void main(String[] args) {
NumberComponent numberComponent = DaggerNumberComponent.builder()
.numberModule(new NumberModule("MyMain"))
.build();
InvertNumber invertNumber = numberComponent.getInvertNumber();
invertNumber.invert(10);
}
To print logs in console, I have to provide tag to the logger. For this I am creating instance of NumberModule class and passing to NumberComponent builder.
Now my questions are:
Is this correct way to pass tag using NumberModule instance
If it is correct, according to DI concept, it is not encouraged to
use new operator to create objects (new NumberModule())
If the above code is wrong, what is the correct way?
Your way of solving problem is right, but there is one more using #Component.Buidler. (please, note that in your case #Inject annotation in Logger and InvertNumber constructor not working - you call them by hand).
Rewrite dagger stuff like that
#Module
public class NumberModule {
#Inject
public NumberModule() {}
#Provides
Logger provideLogger(#Named("logger_tag") String tag){
Logger logger = new Logger(tag);
return logger;
}
#Provides
InvertNumber provideTempManager(Logger logger){
return new InvertNumber(logger);
}
}
#Component(modules = NumberModule.class)
public interface NumberComponent {
InvertNumber getInvertNumber();
#Component.Builder
interface Builder {
#BindsInstance
Builder loggerTag(#Named("logger_tag") String tag);
NumberComponent build();
}
}
and use it
NumberComponent numberComponent = DaggerNumberComponent.builder()
.loggerTag("MyMain")
.build();
To allow dagger create Logger and InvertNumber for you (not manually calling their constructors), you need interfaces for each of them.
Related
I have an object to be injected that is defined as:
public class EnvironmentModule extends AbstractModule {
#Override
protected void configure() {
}
#Provides
#Singleton
private String getObject(final Client client) {
...
}
}
Client is an enum defined as :
#NoArgsConstructor(force = true)
public enum Client {
private String name;
Client(final String name) {
this.name = name;
}
public static Client identifyClient(final String clientName) {
}
}
This gives me an error-
Could not find a suitable constructor in Client. Classes must have either one (and only one) constructor annotated with #Inject or a zero-argument constructor that is not private
at Client.class(Client.java:5)
at EnvironmentModule.getObject(EnvironmentModule.java:35)
Please help. What has to be done.
The reason this is happening is because in your module you do not declare an instance of Client to be injected in the scope of the module, so it tries to create one with an empty constructor. This does not work because your enum has two constructors, and guice requires a single empty constructor. The solution to this to create a singleton of your client. I assume the code you omitted in Client looks like
public enum Client {
//I assume it works like this
NORMAL_CLIENT("whatever");
private String name;
Client(final String name) {
this.name = name;
}
public static Client identifyClient(final String clientName) {
return Arrays.stream(Client.values())
.filter(client -> clientName.equals(client.name))
.findAny()
//This is dangerous, throw an error if its not found
.get();
}
}
So we need to create a singleton in the environment module for the client. this would look like
public class EnvironmentModule extends AbstractModule {
#Override
protected void configure() {
super.configure();
}
#Provides
#Singleton
private Client getClient() {
return Client.identifyClient("whatever");
}
#Provides
#Singleton
private String doWhatever(final Client client) {
System.out.println("found client " + client);
return "cool it works";
}
}
invoking the module through
public class Main {
public static void main(String[] args) {
final var envInjector = Guice.createInjector(new EnvironmentModule());
final var client = envInjector.getInstance(Client.class);
final var doWhateverString = envInjector.getInstance(String.class);
System.out.println(doWhateverString);
System.out.println("found instance " + client);
}
}
we can see
found client NORMAL_CLIENT
cool it works
found instance NORMAL_CLIENT
I'm New in Mocking.
I've a service I'm trying to call is let say name A, I need to test someMethod.
#Service
public class A {
private Logger logger = LoggerFactory.getLogger(getClass());
private final CoreXReader coreXReader;
#Autowired
B b;
#Autowired
C c;
#Async
public void someMethod(Config config) throws Exception {
pushConfig(config);
}
private void pushConfig(Config config) throws Exception {
String url = config.getBaseurl() + config.getId();
ABCRestClient restClient = new ABCRestClient(url);
String jobJson = restClient.callRestMethod(HttpMethod.GET, "");
}
}
sample of ABCRestClient
public class ABCRestClient {
private Logger logger = LoggerFactory.getLogger(getClass());
private String url;
public ABCRestClient(String url) {
this.url = url;
}
public String callRestMethod(HttpMethod method, String payload) throws Exception {
someresponse="example response";
return someresponse;
}
}
I'm trying to test by creating mockSpy but it still Calling its 'callRestMethod'
#RunWith(SpringRunner.class)
#SpringBootTest // (webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Test {
#Autowired
private A a;
private Logger logger = LoggerFactory.getLogger(getClass());
#Before
public void prepareMockDataForService() throws Exception {
ABCRestClient apiClient = new ABCRestClient(config.getBaseurl() + config.getId() );
ABCRestClient apiClientSpy=Mockito.spy(apiClient);
doReturn(getCallResponse()).when(apiClientSpy).callRestMethod(HttpMethod.GET, "");
}
#Test
public void TestPushConfig() throws Exception {
a.someMethod(StubDataGenerator.getConfig());
}
private String getCallResponse() {
return "{"msg":"sample response"}";
}
}
i'm not sure what I'm doing wrong here why its calling the actual callRestMethod as i already create a spy .
I tried using this too Mockito.doReturn(getCallResponse()).when(apiClientSpy.callRestMethod(HttpMethod.GET, ""))
Also, is there any difference in these two statement if I use it Mockito.doReturn() or directly doReturn()? In my case both seems behaving same.
Before I tried with this as well when().thenReturn(); but I read somewhere that use when().thenReturn() when you actually want to make call. Please correct if my understanding is wrong.
You can try mock instead of spy:
#RunWith(SpringRunner.class)
#SpringBootTest // (webEnvironment=
SpringBootTest.WebEnvironment.RANDOM_PORT)
public class Test {
#Autowired
private A a;
private Logger logger = LoggerFactory.getLogger(getClass());
#Before
public void prepareMockDataForService() throws Exception {
ABCRestClient apiClientSpy=Mockito.mock(ABCRestClient.class);
doReturn(getCallResponse()).when(apiClientSpy).callRestMethod(HttpMethod.GET, "");
}
#Test
public void TestPushConfig() throws Exception {
a.someMethod(StubDataGenerator.getConfig());
}
private String getCallResponse() {
return "{"msg":"sample response"}";
}
}
I have a class annotated with #Component which is use to initialze application.yml config properties. Service classe is using configuration property. But sometime my Service class instance created before the Configuration class and I get null property value in service class, Its random not specific pattern.
Configuration Initializer class..
#Component
public class ConfigInitializer implements InitializingBean {
private static final Logger log = LoggerFactory.getLogger(ConfigInitializer.class);
#Autowired
ProxyConfig proxyConfig;
/*#PostConstruct
public void postConstruct(){
setProperties();
}
*/
#Override
public void afterPropertiesSet() {
setProperties();
}
private void setSystemProperties(){
log.debug("Setting properties...");
Properties props = new Properties();
props.put("PROXY_URL", proxyConfig.getProxyUrl());
props.put("PROXY_PORT", proxyConfig.getProxyPort());
System.getProperties().putAll(props);
}
}
#Component
#ConfigurationProperties(prefix = "proxy-config")
public static class ProxyConfig {
private String proxyUrl;
private String proxyPort;
public String getProxyUrl() {
return proxyUrl;
}
public void setProxyUrl(String proxyUrl) {
this.proxyUrl = proxyUrl;
}
public String getProxyPort() {
return proxyPort;
}
public void setProxyPort(String proxyPort) {
this.proxyPort = proxyPort;
}
}
Service Class..
#Service("receiverService")
public class ReceiverService {
private static final Logger logger = LoggerFactory.getLogger(ReceiverService.class);
private ExecutorService executorService = Executors.newSingleThreadExecutor();
#Autowired
public ReceiverService() {
initClient();
}
private void initClient() {
Future future = executorService.submit(new Callable(){
public Object call() throws Exception {
String value = System.getProperty("PROXY_URL"); **//Here I am getting null**
logger.info("Values : " + value);
}
});
System.out.println("future.get() = " + future.get());
}
}
Above Service class get null values String value = System.getProperty("PROXY_URL")
When I use #DependsOn annotation on Service class, it works fine.
In my little knowledge, I know Spring does not have specific order of bean creation.
I want to know If I use #Configuration instead of #Component on ConfigInitializer class like below, Will spring initialize ConfigInitializer
class before other beans ?.
#Configuration
public class ConfigInitializer implements InitializingBean {
//code here
}
I have to add custom method method to my logging system. Let's say I have in several places line of code like:
private static Logger LOGGER = LoggerFactory.getLogger(MyJavaClass.class);
Logging is handling by slf4j. How can I extend my logger with adding new(!) method like public void error(SomeTypeObject obj) { /implementation/ }
The goal is to not change existing code. How can I force LoggerFactory to return my own Logger implementation extended with mentioned method?
I followed answers for this queston: stackoverflow.com/questions/2653855/implement-custom-logger-with-slf4j
So, I've made my LoggerFactory, StaticLoggerBinder and MyLoggerAdapter.
StaticLoggerBinder
public class StaticLoggerBinder implements LoggerFactoryBinder {
private static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
public static final StaticLoggerBinder getSingleton() {
return SINGLETON;
}
public static String REQUESTED_API_VERSION = "1.6.99";
private static final String loggerFactoryClassStr = MyLoggerFactory.class.getName();
private final ILoggerFactory loggerFactory;
private StaticLoggerBinder() {
loggerFactory = new MyLoggerFactory();
}
public ILoggerFactory getLoggerFactory() {
return loggerFactory;
}
public String getLoggerFactoryClassStr() {
return loggerFactoryClassStr;
}
}
Logger Factory
public class MyLoggerFactory implements ILoggerFactory {
ConcurrentMap<String, Logger> loggerMap;
public MyLoggerFactory() {
loggerMap = new ConcurrentHashMap<String, Logger>();
}
public Logger getLogger(String name) {
Logger simpleLogger = loggerMap.get(name);
if (simpleLogger != null) {
return simpleLogger;
} else {
Logger newInstance = new MyLoggerAdapter(name);
Logger oldInstance = loggerMap.putIfAbsent(name, newInstance);
return oldInstance == null ? newInstance : oldInstance;
}
}
void reset() {
loggerMap.clear();
}
}
MyLogger
public class MyLoggerAdapter implements Logger {
//methods from Logger interface
//and my additional method
public void error(Exception ex) {
//do smthng;
}
}
Now, in MyJavaClass i have a field
private static Logger LOGGER = LoggerFactory.getLogger(MyJavaClass.class);
The problem is, when i try to LOGGER.error(myExceptionObject) the method is not visible. I am missing something. I would be very grateful for help.
In your MyJavaClass you should use your MyLoggerFactory:
private static MyLoggerAdapter LOGGER = MyLoggerFactory.getLogger();
instead of
private static Logger LOGGER = LoggerFactory.getLogger(MyJavaClass.class);
In this example logger can get so:
private static MyLoggerAdapter log = (MyLoggerAdapter) StaticLoggerBinder.getSingleton().getLoggerFactory.getLogger(MyController.class.getName());
I have a little problem. I think this is typical question. However, I can't find good example. My application is using Jersey. And I want to test controller by client as test. Controller has private field - StudentService. When I debug test I see, that field is null. This leads to error. And I need to inject this field. I tried this:
My Controller
#Path("/student")
#Component
public class StudentResourse {
#Autowired
private StrudentService service; // this field Spring does not set
#Path("/getStudent/{id}")
#GET
#Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Student getStudent(#PathParam("id") long id) {
return service.get(id);
}
}
My JUnit test class:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = "classpath:config.xml")
#TestExecutionListeners({ DbUnitTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class })
public class StudentResourseTest extends JerseyTest {
private static final String PACKAGE_NAME = "com.example.servlet";
private static final String FILE_DATASET = "/data.xml";
#Autowired
private StudentService service; // this field is setted by Spring, but I do not need this field for test
public StudentResourseTest() {
super(new WebAppDescriptor.Builder(PACKAGE_NAME).build());
}
#Override
protected TestContainerFactory getTestContainerFactory() {
return new HTTPContainerFactory();
}
#Override
protected AppDescriptor configure() {
return new WebAppDescriptor.Builder("restful.server.resource")
.contextParam("contextConfigLocation",
"classpath:/config.xml").contextPath("/")
.servletClass(SpringServlet.class)
.contextListenerClass(ContextLoaderListener.class)
.requestListenerClass(RequestContextListener.class).build();
}
#Test
#DatabaseSetup(FILE_DATASET)
public void test() throws UnsupportedEncodingException {
ClientResponse response = resource().path("student").path("getStudent")
.path("100500").accept(MediaType.APPLICATION_XML)
.get(ClientResponse.class);
Student student = (Student) response.getEntity(Student.class);
} }
I guees, that problem is in test class. Because, when I run my application not in test, I can directly request students and everything working fine. But when I test classes, internal field of Controller does not setted. How to fix this bug? Thanks for your answers.
This is in my config.xml
<context:component-scan base-package="com.example" />
<bean id="StudentResourse" class="com.example.servlet.StudentResourse">
<property name="service" ref="studentService" />
</bean>
<bean id="service" class="com.example.service.StudentServiceImpl" />
One issue may be that you're trying to configure your test application in constructor and in configure() method. Use one or another but not both because in this case your configure() method is not invoked and hence you may not be using SpringServlet and everything that is defined in this method.
Reference: https://github.com/jiunjiunma/spring-jersey-test and http://geek.riffpie.com/unit-testing-restful-jersey-services-glued-together-with-spring/
Idea is to get a hold of the application context inside jersey by using ApplicationContextAware interface. There after we can grab the exact bean already created by spring, in your case, StudentService. Below example shows a mocked version of the dependency, SampleService, used to test the resource layer apis.
Resource class delegating the processing to a service layer
#Component
#Path("/sample")
public class SampleResource {
#Autowired
private SampleService sampleService;
#GET
#Produces(MediaType.APPLICATION_JSON)
#Path ("/{id}")
public Sample getSample(#PathParam("id") int id) {
Sample sample = sampleService.getSample(id);
if (sample == null) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return sample;
}
}
Service layer encapsulating business logic
#Service
public class SampleService {
private static final Map<Integer, Sample> samples = new HashMap<>();
static {
samples.put(1, new Sample(1, "sample1"));
samples.put(2, new Sample(2, "sample2"));
}
public Sample getSample(int id) {
return samples.get(id);
}
}
Unit test for the above resource
public class SampleResourceTest extends SpringContextAwareJerseyTest {
private SampleService mockSampleService;
// create mock object for our test
#Bean
static public SampleService sampleService() {
return Mockito.mock(SampleService.class);
}
/**
* Create our own resource here so only the test resource is loaded. If
* we use #ComponentScan, the whole package will be scanned and more
* resources may be loaded (which is usually NOT what we want in a test).
*/
#Bean
static public SampleResource sampleResource() {
return new SampleResource();
}
// get the mock objects from the internal servlet context, because
// the app context may get recreated for each test so we have to set
// it before each run
#Before
public void setupMocks() {
mockSampleService = getContext().getBean(SampleService.class);
}
#Test
public void testMock() {
Assert.assertNotNull(mockSampleService);
}
#Test
public void testGetSample() {
// see how the mock object hijack the sample service, now id 3 is valid
Sample sample3 = new Sample(3, "sample3");
Mockito.when(mockSampleService.getSample(3)).thenReturn(sample3);
expect().statusCode(200).get(SERVLET_PATH + "/sample/3");
String jsonStr = get(SERVLET_PATH + "/sample/3").asString();
Assert.assertNotNull(jsonStr);
}
}
SpringContextAwareJerseyTest
#Configuration
public class SpringContextAwareJerseyTest extends JerseyTest {
protected static String SERVLET_PATH = "/api";
final private static ThreadLocal<ApplicationContext> context =
new ThreadLocal<>();
protected String getResourceLocation() {
return "example.rest";
}
protected String getContextConfigLocation() {
return getClass().getName();
}
static private String getContextHolderConfigLocation() {
return SpringContextAwareJerseyTest.class.getName();
}
protected WebAppDescriptor configure() {
String contextConfigLocation = getContextConfigLocation() + " " +
getContextHolderConfigLocation();
Map<String, String> initParams = new HashMap<>();
initParams.put("com.sun.jersey.config.property.packages",
getResourceLocation());
initParams.put("com.sun.jersey.api.json.POJOMappingFeature", "true");
return new WebAppDescriptor.Builder(initParams)
.servletClass(SpringServlet.class)
.contextParam(
"contextClass",
"org.springframework.web.context.support.AnnotationConfigWebApplicationContext")
.contextParam("contextConfigLocation", contextConfigLocation)
.servletPath(SERVLET_PATH) // if not specified, it set to root resource
.contextListenerClass(ContextLoaderListener.class)
.requestListenerClass(RequestContextListener.class)
.build();
}
protected final ApplicationContext getContext() {
return context.get();
}
#Bean
public static ContextHolder contextHolder() {
return new ContextHolder();
}
private static class ContextHolder implements ApplicationContextAware {
#Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
context.set(applicationContext);
}
}
}
Using the above with jersey 1.8