1. Configure Module to Enable the Dependency Injection Create SampleModule.java which extends AbstractModule. Abstract Module have configure () method which we need to override. Inside the configure() method need to define the Binding between the Interface and Corresponding Implementation as shown in the below example.
public class SampleModule extends AbstractModule { // Bind interface with Implementation in Configure Method. @Override protected void configure() { // Bind Clinet to RESTFul WebService //bind(Client.class).to(RESTClient.class); // Bind Client to JMS Service. bind(Client.class).to(JMSClient.class); bind(JMSService.class).to(JMSServiceImpl.class); bind(RESTService.class).to(RESTServiceImpl.class); } }
2. Interfaces and Implementation As shown in the above point we need to Create the Interface and Corresponding Implementation. For Example, JSMClient and RESTClient are the type of Client which can access the Service ( JMSService / RESTService). Client.java
package com.code.java.desk.api; public interface Client { String accessService(); } @Singleton public class JMSClient implements Client { // Instance Variable Injection. @Inject private JMSService jmsService; // Constructor Based Injection @Inject public JMSClient(JMSService jmsService) { super(); this.jmsService = jmsService; } // Setter Method Injection. @Inject public void setJMSService(JMSService service){ this.jmsService = service; } @Override public String accessService() { System.out.println("Access Client...."); jmsService.execute(); System.out.println("Exit Client Code..."); } }
3. Services and Implementation Below defined as JMSService and JMSServiceImpl.
package com.code.java.desk.service; public interface JMSService { void execute(); } @Singleton public class JMSServiceImpl implements JMSService{ @Override public void execute() { System.out.println("Executing JMS Service"); } }
4. MainApplication / User Interface
public class MainApplication { @Inject Client client; public String processRequest(){ System.out.println("Inside the Main Application..."); client.accessService(); System.out.println("Exit Main Applicaion..."); } }
5. Entry Point of Application
public class TestMainApplication { public static void main(String[] args) { Injector injector = Guice.createInjector(new SampleModule()); MainApplication mainApp = injector.getInstance(MainApplication.class); mainApp.processRequest(); } }
6. How to Write JUnit test cases for Google Guice To Write the test case for functionalities using Guice, we need to Create the instance of Injector using the Module. We can Install the SampleModule which we had created. Need to Bind the necessary services, interface and implementation and install the existing module( If Necessary). Following is the example to show case –
public class MainApplicationTest { private Injector injector; @Before public void setUp() throws Exception { injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { install(new SampleModule()); bind(JMSService.class).to(JMSServiceImpl.class); bind(Client.class).to(JMSClient.class); } }); } @After public void destroy() throws Exception { injector = null; } @Test public void testProcessRequest() { MainApplication mainApp = injector.getInstance(MainApplication.class); Assert.assertEquals("Executing JMS Service",mainApp.processRequest()); } }
Soumitra Pathak - Linkedin, Facebook
He is a freelance writer, loves to explore latest features in Java technology.
No comments:
Post a Comment