Created
July 5, 2017 05:48
-
-
Save jaycobbcruz/9ccf5e1956eba0e8c43ca00ab169ee20 to your computer and use it in GitHub Desktop.
Guice - JUnit integration example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.inject.AbstractModule; | |
import com.google.inject.Guice; | |
import com.google.inject.Injector; | |
import org.junit.runners.BlockJUnit4ClassRunner; | |
import org.junit.runners.model.InitializationError; | |
public class GuiceJUnitRunner extends BlockJUnit4ClassRunner { | |
public GuiceJUnitRunner(Class<?> klass) throws InitializationError { | |
super(klass); | |
} | |
@Override | |
protected Object createTest() throws Exception { | |
final Object object = super.createTest(); | |
final Injector injector = Guice.createInjector(new AbstractModule() { | |
@Override | |
protected void configure() { | |
bind(PersonDAO.class).to(PersonDAOImpl.class); | |
bind(PersonSearch.class).to(PersonServiceImpl.class); | |
} | |
}); | |
injector.injectMembers(object); | |
return object; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person { | |
private final Integer id; | |
private final String name; | |
public Person(Integer id, String name) { | |
this.id = id; | |
this.name = name; | |
} | |
public Integer getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
@Override | |
public String toString() { | |
return "Person{" + "name='" + name + '\'' + '}'; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
public interface PersonDAO { | |
List<Person> findAll(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
public class PersonDAOImpl implements PersonDAO { | |
public List<Person> findAll() { | |
return Arrays.asList( | |
new Person(1, "John Doe"), | |
new Person(2, "Joe Bloggs") | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
public interface PersonSearch { | |
List<Person> findAll(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.inject.Inject; | |
import java.util.List; | |
public class PersonServiceImpl implements PersonSearch { | |
private final PersonDAO personDAO; | |
@Inject | |
public PersonServiceImpl(PersonDAO personDAO) { | |
this.personDAO = personDAO; | |
} | |
public List<Person> findAll() { | |
return personDAO.findAll(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import javax.inject.Inject; | |
import java.util.List; | |
import static org.junit.Assert.assertTrue; | |
@RunWith(GuiceJUnitRunner.class) | |
public class PersonServiceTest { | |
@Inject private PersonSearch lookupSearch; | |
@Test | |
public void testSearch() { | |
final List<Person> all = lookupSearch.findAll(); | |
assertTrue(all.size() == 2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment