Skip to content

Commit 9e88c4c

Browse files
author
Igor Polevoy
committed
#239 Injector should not be set in case of testing
1 parent e0e6c89 commit 9e88c4c

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

activeweb-testing/src/test/java/app/config/AppBootstrap.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import app.services.GreeterModule;
2020
import com.google.inject.Guice;
21+
import com.google.inject.Injector;
2122
import com.google.inject.Module;
2223
import org.javalite.activeweb.AppContext;
2324
import org.javalite.activeweb.Bootstrap;
@@ -26,8 +27,12 @@
2627
* @author Igor Polevoy
2728
*/
2829
public class AppBootstrap extends Bootstrap {
30+
31+
@Override
32+
public void init(AppContext context) {}
33+
2934
@Override
30-
public void init(AppContext context) {
31-
setInjector(Guice.createInjector(new GreeterModule()));
35+
public Injector getInjector() {
36+
return Guice.createInjector(new GreeterModule());
3237
}
3338
}

activeweb/src/main/java/org/javalite/activeweb/Bootstrap.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,30 @@ public void destroy(AppContext context){}
6060
* with mocks for testing.
6161
* </p>
6262
*
63+
* @deprecated This method will be removed in future versions. Instead, override {@link #getInjector()} method
64+
* to produce your injector.
65+
*
6366
* @param injector Injector instance to use for dependency injection.
6467
*/
6568
public void setInjector(Injector injector){
6669
if(!Configuration.isTesting()){
6770
Context.getControllerRegistry().setInjector(injector);
6871
}
6972
}
73+
74+
/**
75+
* Subclasses need to override this method to return instance of Injector to use for dependency injection.
76+
*
77+
* <p></p>
78+
79+
* <strong>
80+
* This method is NOT USED during testing. Each test class will set its own injector
81+
* with mocks for testing.
82+
* </strong>
83+
*
84+
* @return instance of Injector to use to inject dependencies into controllers, filters and tags.
85+
*/
86+
public Injector getInjector(){
87+
return null;
88+
};
7089
}

activeweb/src/main/java/org/javalite/activeweb/RequestDispatcher.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,13 @@ private void initAppConfig(String configClassName, AppContext context, boolean f
128128
try {
129129
Class c = Class.forName(configClassName);
130130
appConfig = (AppConfig) c.newInstance();
131+
appConfig.init(context);
131132
if(appConfig instanceof Bootstrap){
132133
appBootstrap = (Bootstrap) appConfig;
134+
if(!Configuration.isTesting() && appBootstrap.getInjector() != null){
135+
Context.getControllerRegistry().setInjector(appBootstrap.getInjector());
136+
}
133137
}
134-
appConfig.init(context);
135138
appConfig.completeInit();
136139
}
137140
catch (Throwable e) {

0 commit comments

Comments
 (0)