Skip to content

Commit

Permalink
Fix @RequestScoped services in unit tests
Browse files Browse the repository at this point in the history
Fixes RM-13492
  • Loading branch information
admin committed Feb 28, 2019
1 parent 4cb8d6e commit 4aeee67
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions axelor-test/src/main/java/com/axelor/test/GuiceRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
*/
package com.axelor.test;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.servlet.RequestScoped;
import com.google.inject.servlet.RequestScoper;
import com.google.inject.servlet.ServletScopes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;

Expand Down Expand Up @@ -72,9 +78,26 @@ protected List<Module> getModules(Class<?> klass) throws InitializationError {
throw new InitializationError(e);
}
}

modules.add(
new AbstractModule() {
@Override
protected void configure() {
bindScope(RequestScoped.class, ServletScopes.REQUEST);
}
});

return modules;
}

@Override
public void run(RunNotifier notifier) {
final RequestScoper scope = ServletScopes.scopeRequest(Collections.emptyMap());
try (RequestScoper.CloseableScope ignored = scope.open()) {
super.run(notifier);
}
}

@Override
public Object createTest() {
return injector.getInstance(getTestClass().getJavaClass());
Expand Down

0 comments on commit 4aeee67

Please sign in to comment.