how to access sessionscoped bean across tests
cuneyt Nov 17, 2012 10:58 PMHave been struggling with a test case where a service is dependent on having a session scoped bean being in the session fails;
The class producing @SessionScoped User:
public class LoginService {
private User user;
public void login(String name) {
if ("userA".equals(name)) {
user = new User(name, "permissionA");
} else if ("userB".equals(name)) {
user = new User(name, "permissionB");
} else {
user = new User("anonymous", "");
}
}
@Producer
@LoggedIn
@SessionScoped
public User getLoggedIn() {
return this.user;
}
}
The service using the @LoggedIn User:
public class MediaService {
@Inject
@LoggedIn
private User user;
public void updateImage(){
if("permissionA".equals(user.getPermission())){
System.out.println("user can update image");
}
else {
System.out.println("user can not update image");
}
}
}
And the test:
@RunWith(Arquillian.class)
public class ServiceTest {
@Deployment public static WebArchive deployment() {
return ShrinkWrap .create(WebArchive.class, "test.war")
.addClass(LoggedIn.class)
.addClass(LoginService.class)
.addClass(MediaService.class)
.addClass(User.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Inject private LoginService lservice;
@Inject private MediaService mservice;
@Test
public void testLogin() {
lservice.login("userA");
}
@Test
public void testUpdateImage(){
mservice.updateImage();
}
}
Testing with the arquillian remote container, first test (testLogin) passes and the testUpdateImage fails with null User in session.
WELD-000052 Cannot return null from a non-dependent producer method:
[method] @Produces @LoggedIn @SessionScoped public org.arg.service.LoginService.getLoggedIn()
Thanks