Created
September 28, 2012 06:09
-
-
Save virasak/3798194 to your computer and use it in GitHub Desktop.
Simple Guice & JUnit4 Integration: Use the unit test itself as a module to provide member injection. Use @provides methods to build a module from unit test 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
import com.google.inject.Inject; | |
public class AuthenService { | |
private final UserDAO userDAO; | |
@Inject | |
public AuthenService(UserDAO userDAO) { | |
this.userDAO = userDAO; | |
} | |
public User login(String username, String password) throws AuthenticationException { | |
User user = userDAO.findUser(username); | |
if (user == null) { | |
throw new UserNotFoundException(); | |
} | |
// TODO: Hash password before compare | |
if (!user.getPassword().equals(password)) { | |
throw new PasswordDontMatchException(); | |
} | |
if (!user.isActive()) { | |
throw new NotActiveException(); | |
} | |
return user; | |
} | |
} |
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.Inject; | |
import com.google.inject.Provides; | |
import com.google.inject.Singleton; | |
import junit.framework.Assert; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import static org.easymock.EasyMock.*; | |
@RunWith(GuiceJUnit4Runner.class) | |
public class EasyMockTest { | |
@Inject private AuthenService authenService; | |
@Inject private UserDAO userDAO; | |
@Provides @Singleton | |
public UserDAO mockUserDAO() { | |
return createStrictMock(UserDAO.class); | |
} | |
@Test | |
public void successLogin() { | |
User virasak = new User(); | |
virasak.setUsername("virasak"); | |
virasak.setPassword("123456"); | |
virasak.setActive(true); | |
expect(userDAO.findUser(eq(virasak.getUsername()))).andReturn(virasak); | |
replay(userDAO); | |
try { | |
User loginUser = authenService.login(virasak.getUsername(), virasak.getPassword()); | |
Assert.assertEquals(loginUser, virasak); | |
verify(userDAO); | |
} catch (AuthenticationException e) {} | |
} | |
} |
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.Guice; | |
import com.google.inject.Module; | |
import com.google.inject.internal.ProviderMethodsModule; | |
import org.junit.runners.BlockJUnit4ClassRunner; | |
import org.junit.runners.model.InitializationError; | |
public class GuiceJUnit4Runner extends BlockJUnit4ClassRunner { | |
public GuiceJUnit4Runner(Class<?> klass) throws InitializationError { | |
super(klass); | |
} | |
@Override | |
public Object createTest() throws Exception { | |
Object object = super.createTest(); | |
Module module = ProviderMethodsModule.forObject(object); | |
Guice.createInjector(module).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
package com.googlecode.guicejunit4; | |
import com.google.inject.Inject; | |
import com.google.inject.Provides; | |
import com.google.inject.name.Named; | |
import static junit.framework.Assert.*; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
@RunWith(GuiceJUnit4Runner.class) | |
public class SimpleInjectTest { | |
@Inject | |
private SimpleService simpleService; | |
@Provides @Named("greeter") | |
String greeter() { | |
return "Hello World"; | |
} | |
@Test | |
public void testInject() { | |
assertNotNull(simpleService); | |
} | |
@Test | |
public void testInjectDependencies() { | |
assertEquals(simpleService.getGreeter(), "Hello World"); | |
} | |
} |
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
package com.googlecode.guicejunit4; | |
import com.google.inject.Inject; | |
import com.google.inject.name.Named; | |
public class SimpleService { | |
@Inject @Named("greeter") | |
private String greeter; | |
public String getGreeter() { | |
return greeter; | |
} | |
} |
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 User { | |
private long userId; | |
private String username; | |
private String password; | |
private boolean active; | |
public long getUserId() { | |
return userId; | |
} | |
public void setUserId(long userId) { | |
this.userId = userId; | |
} | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public boolean isActive() { | |
return active; | |
} | |
public void setActive(boolean active) { | |
this.active = active; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
} |
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 interface UserDAO { | |
User findUser(String userName); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment