PageRenderTime 147ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/testlib/test/com/google/inject/testing/fieldbinder/BoundFieldModuleTest.java

https://gitlab.com/metamorphiccode/guice
Java | 753 lines | 587 code | 148 blank | 18 comment | 0 complexity | 1021ffe48116073e106daad088f94a08 MD5 | raw file
  1. /*
  2. * Copyright (C) 2014 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.inject.testing.fieldbinder;
  17. import static com.google.inject.Asserts.assertContains;
  18. import static java.lang.annotation.RetentionPolicy.RUNTIME;
  19. import com.google.inject.BindingAnnotation;
  20. import com.google.inject.ConfigurationException;
  21. import com.google.inject.CreationException;
  22. import com.google.inject.Guice;
  23. import com.google.inject.Inject;
  24. import com.google.inject.Injector;
  25. import com.google.inject.Key;
  26. import com.google.inject.Provider;
  27. import com.google.inject.ProvisionException;
  28. import com.google.inject.name.Named;
  29. import com.google.inject.name.Names;
  30. import com.google.inject.util.Providers;
  31. import junit.framework.TestCase;
  32. import java.lang.annotation.Retention;
  33. import java.util.Arrays;
  34. import java.util.List;
  35. import javax.inject.Qualifier;
  36. /** Unit tests for {@link BoundFieldModule}. */
  37. public class BoundFieldModuleTest extends TestCase {
  38. public void testBindingNothing() {
  39. Object instance = new Object() {};
  40. BoundFieldModule module = BoundFieldModule.of(instance);
  41. Guice.createInjector(module);
  42. // If we didn't throw an exception, we succeeded.
  43. }
  44. public void testBindingOnePrivate() {
  45. final Integer testValue = 1024;
  46. Object instance = new Object() {
  47. @Bind private Integer anInt = testValue;
  48. };
  49. BoundFieldModule module = BoundFieldModule.of(instance);
  50. Injector injector = Guice.createInjector(module);
  51. assertEquals(testValue, injector.getInstance(Integer.class));
  52. }
  53. public void testBindingOnePublic() {
  54. final Integer testValue = 1024;
  55. Object instance = new Object() {
  56. @Bind public Integer anInt = testValue;
  57. };
  58. BoundFieldModule module = BoundFieldModule.of(instance);
  59. Injector injector = Guice.createInjector(module);
  60. assertEquals(testValue, injector.getInstance(Integer.class));
  61. }
  62. private static class FieldBindableClass {
  63. @Bind Integer anInt;
  64. FieldBindableClass(Integer anInt) {
  65. this.anInt = anInt;
  66. }
  67. }
  68. private static class FieldBindableSubclass extends FieldBindableClass {
  69. FieldBindableSubclass(Integer anInt) {
  70. super(anInt);
  71. }
  72. }
  73. public void testSuperTypeBinding() {
  74. FieldBindableSubclass instance = new FieldBindableSubclass(1024);
  75. BoundFieldModule module = BoundFieldModule.of(instance);
  76. Injector injector = Guice.createInjector(module);
  77. assertEquals(instance.anInt, injector.getInstance(Integer.class));
  78. }
  79. public void testBindingTwo() {
  80. final Integer testValue = 1024;
  81. final String testString = "Hello World!";
  82. Object instance = new Object() {
  83. @Bind private Integer anInt = testValue;
  84. @Bind private String aString = testString;
  85. };
  86. BoundFieldModule module = BoundFieldModule.of(instance);
  87. Injector injector = Guice.createInjector(module);
  88. assertEquals(testValue, injector.getInstance(Integer.class));
  89. assertEquals(testString, injector.getInstance(String.class));
  90. }
  91. public void testBindingSuperType() {
  92. final Integer testValue = 1024;
  93. Object instance = new Object() {
  94. @Bind(to = Number.class) private Integer anInt = testValue;
  95. };
  96. BoundFieldModule module = BoundFieldModule.of(instance);
  97. Injector injector = Guice.createInjector(module);
  98. assertEquals(testValue, injector.getInstance(Number.class));
  99. }
  100. public void testBindingSuperTypeAccessSubType() {
  101. final Integer testValue = 1024;
  102. Object instance = new Object() {
  103. @Bind(to = Number.class) private Integer anInt = testValue;
  104. };
  105. BoundFieldModule module = BoundFieldModule.of(instance);
  106. Injector injector = Guice.createInjector(module);
  107. try {
  108. injector.getInstance(Integer.class);
  109. fail();
  110. } catch (ConfigurationException e) {
  111. assertContains(
  112. e.getMessage(),
  113. "Could not find a suitable constructor in java.lang.Integer");
  114. }
  115. }
  116. public void testBindingIncorrectTypeProviderFails() {
  117. final Integer testValue = 1024;
  118. Object instance = new Object() {
  119. @Bind(to = String.class) private Provider<Integer> anIntProvider = new Provider<Integer>() {
  120. @Override public Integer get() {
  121. return testValue;
  122. }
  123. };
  124. };
  125. BoundFieldModule module = BoundFieldModule.of(instance);
  126. try {
  127. Guice.createInjector(module);
  128. fail();
  129. } catch (CreationException e) {
  130. assertContains(
  131. e.getMessage(),
  132. "Requested binding type \"java.lang.String\" is not "
  133. + "assignable from field binding type \"java.lang.Integer\"");
  134. }
  135. }
  136. @BindingAnnotation
  137. @Retention(RUNTIME)
  138. private static @interface SomeBindingAnnotation {}
  139. public void testBindingWithBindingAnnotation() {
  140. final Integer testValue1 = 1024, testValue2 = 2048;
  141. Object instance = new Object() {
  142. @Bind private Integer anInt = testValue1;
  143. @Bind
  144. @SomeBindingAnnotation
  145. private Integer anotherInt = testValue2;
  146. };
  147. BoundFieldModule module = BoundFieldModule.of(instance);
  148. Injector injector = Guice.createInjector(module);
  149. assertEquals(testValue1, injector.getInstance(Integer.class));
  150. assertEquals(
  151. testValue2,
  152. injector.getInstance(Key.get(Integer.class, SomeBindingAnnotation.class)));
  153. }
  154. @Qualifier
  155. @Retention(RUNTIME)
  156. private static @interface SomeQualifier {}
  157. public void testBindingWithQualifier() {
  158. final Integer testValue1 = 1024, testValue2 = 2048;
  159. Object instance = new Object() {
  160. @Bind private Integer anInt = testValue1;
  161. @Bind
  162. @SomeQualifier
  163. private Integer anotherInt = testValue2;
  164. };
  165. BoundFieldModule module = BoundFieldModule.of(instance);
  166. Injector injector = Guice.createInjector(module);
  167. assertEquals(testValue1, injector.getInstance(Integer.class));
  168. assertEquals(
  169. testValue2,
  170. injector.getInstance(Key.get(Integer.class, SomeQualifier.class)));
  171. }
  172. public void testCanReuseBindingAnnotationsWithDifferentValues() {
  173. final Integer testValue1 = 1024, testValue2 = 2048;
  174. final String name1 = "foo", name2 = "bar";
  175. Object instance = new Object() {
  176. @Bind
  177. @Named(name1)
  178. private Integer anInt = testValue1;
  179. @Bind
  180. @Named(name2)
  181. private Integer anotherInt = testValue2;
  182. };
  183. BoundFieldModule module = BoundFieldModule.of(instance);
  184. Injector injector = Guice.createInjector(module);
  185. assertEquals(
  186. testValue1,
  187. injector.getInstance(Key.get(Integer.class, Names.named(name1))));
  188. assertEquals(
  189. testValue2,
  190. injector.getInstance(Key.get(Integer.class, Names.named(name2))));
  191. }
  192. public void testBindingWithValuedBindingAnnotation() {
  193. final Integer testValue1 = 1024, testValue2 = 2048;
  194. final String name = "foo";
  195. Object instance = new Object() {
  196. @Bind private Integer anInt = testValue1;
  197. @Bind
  198. @Named(name)
  199. private Integer anotherInt = testValue2;
  200. };
  201. BoundFieldModule module = BoundFieldModule.of(instance);
  202. Injector injector = Guice.createInjector(module);
  203. assertEquals(testValue1, injector.getInstance(Integer.class));
  204. assertEquals(
  205. testValue2,
  206. injector.getInstance(Key.get(Integer.class, Names.named(name))));
  207. }
  208. public void testBindingWithGenerics() {
  209. final List<Integer> testIntList = Arrays.asList(new Integer[] {1, 2, 3});
  210. final List<Boolean> testBoolList = Arrays.asList(new Boolean[] {true, true, false});
  211. Object instance = new Object() {
  212. @Bind private List<Integer> anIntList = testIntList;
  213. @Bind private List<Boolean> aBoolList = testBoolList;
  214. };
  215. BoundFieldModule module = BoundFieldModule.of(instance);
  216. Injector injector = Guice.createInjector(module);
  217. assertEquals(testIntList, injector.getInstance(new Key<List<Integer>>() {}));
  218. assertEquals(testBoolList, injector.getInstance(new Key<List<Boolean>>() {}));
  219. }
  220. public void testBoundValueDoesntChange() {
  221. Integer testValue = 1024;
  222. FieldBindableClass instance = new FieldBindableClass(testValue);
  223. BoundFieldModule module = BoundFieldModule.of(instance);
  224. Injector injector = Guice.createInjector(module);
  225. assertEquals(testValue, injector.getInstance(Integer.class));
  226. instance.anInt++;
  227. assertEquals(testValue, injector.getInstance(Integer.class));
  228. }
  229. public void testIncompatibleBindingType() {
  230. final Integer testInt = 1024;
  231. Object instance = new Object() {
  232. @Bind(to = String.class) private Integer anInt = testInt;
  233. };
  234. BoundFieldModule module = BoundFieldModule.of(instance);
  235. try {
  236. Guice.createInjector(module);
  237. fail();
  238. } catch (CreationException e) {
  239. assertContains(e.getMessage(),
  240. "Requested binding type \"java.lang.String\" is not assignable from field binding type "
  241. + "\"java.lang.Integer\"");
  242. }
  243. }
  244. public void testFailureOnMultipleBindingAnnotations() {
  245. final Integer testInt = 1024;
  246. Object instance = new Object() {
  247. @Bind
  248. @Named("a")
  249. @SomeBindingAnnotation
  250. private Integer anInt = testInt;
  251. };
  252. BoundFieldModule module = BoundFieldModule.of(instance);
  253. try {
  254. Guice.createInjector(module);
  255. fail();
  256. } catch (CreationException e) {
  257. assertContains(e.getMessage(), "More than one annotation is specified for this binding.");
  258. }
  259. }
  260. public void testBindingSuperTypeAndBindingAnnotation() {
  261. final Integer testValue = 1024;
  262. Object instance = new Object() {
  263. @Bind(to = Number.class)
  264. @Named("foo")
  265. private Integer anInt = testValue;
  266. };
  267. BoundFieldModule module = BoundFieldModule.of(instance);
  268. Injector injector = Guice.createInjector(module);
  269. assertEquals(testValue, injector.getInstance(Key.get(Number.class, Names.named("foo"))));
  270. }
  271. public void testBindingProvider() {
  272. final Integer testValue = 1024;
  273. Object instance = new Object() {
  274. @Bind private Provider<Integer> anInt = new Provider<Integer>() {
  275. @Override public Integer get() {
  276. return testValue;
  277. }
  278. };
  279. };
  280. BoundFieldModule module = BoundFieldModule.of(instance);
  281. Injector injector = Guice.createInjector(module);
  282. assertEquals(testValue, injector.getInstance(Integer.class));
  283. }
  284. public void testBindingJavaxProvider() {
  285. final Integer testValue = 1024;
  286. Object instance = new Object() {
  287. @Bind private javax.inject.Provider<Integer> anInt = new javax.inject.Provider<Integer>() {
  288. @Override public Integer get() {
  289. return testValue;
  290. }
  291. };
  292. };
  293. BoundFieldModule module = BoundFieldModule.of(instance);
  294. Injector injector = Guice.createInjector(module);
  295. assertEquals(testValue, injector.getInstance(Integer.class));
  296. }
  297. public void testBindingNullField() {
  298. Object instance = new Object() {
  299. @Bind private Integer anInt = null;
  300. };
  301. BoundFieldModule module = BoundFieldModule.of(instance);
  302. try {
  303. Guice.createInjector(module);
  304. fail();
  305. } catch (CreationException e) {
  306. assertContains(e.getMessage(),
  307. "Binding to null values is not allowed. "
  308. + "Use Providers.of(null) if this is your intended behavior.");
  309. }
  310. }
  311. public void testBindingNullProvider() {
  312. Object instance = new Object() {
  313. @Bind private Provider<Integer> anIntProvider = null;
  314. };
  315. BoundFieldModule module = BoundFieldModule.of(instance);
  316. try {
  317. Guice.createInjector(module);
  318. fail();
  319. } catch (CreationException e) {
  320. assertContains(e.getMessage(),
  321. "Binding to null values is not allowed. "
  322. + "Use Providers.of(null) if this is your intended behavior.");
  323. }
  324. }
  325. private static class IntegerProvider implements Provider<Integer> {
  326. private final Integer value;
  327. IntegerProvider(Integer value) {
  328. this.value = value;
  329. }
  330. @Override public Integer get() {
  331. return value;
  332. }
  333. }
  334. public void testProviderSubclassesBindToTheProviderItself() {
  335. final IntegerProvider integerProvider = new IntegerProvider(1024);
  336. Object instance = new Object() {
  337. @Bind private IntegerProvider anIntProvider = integerProvider;
  338. };
  339. BoundFieldModule module = BoundFieldModule.of(instance);
  340. Injector injector = Guice.createInjector(module);
  341. assertEquals(integerProvider, injector.getInstance(IntegerProvider.class));
  342. }
  343. public void testProviderSubclassesDoNotBindParameterizedType() {
  344. final Integer testValue = 1024;
  345. Object instance = new Object() {
  346. @Bind private IntegerProvider anIntProvider = new IntegerProvider(testValue);
  347. };
  348. BoundFieldModule module = BoundFieldModule.of(instance);
  349. Injector injector = Guice.createInjector(module);
  350. try {
  351. injector.getInstance(Integer.class);
  352. fail();
  353. } catch (ConfigurationException e) {
  354. assertContains(e.getMessage(), "Could not find a suitable constructor in java.lang.Integer.");
  355. }
  356. }
  357. private static class ParameterizedObject<T> {
  358. ParameterizedObject(T instance) {
  359. this.instance = instance;
  360. }
  361. @Bind private T instance;
  362. }
  363. public void testBindParameterizedTypeFails() {
  364. ParameterizedObject<Integer> instance = new ParameterizedObject<Integer>(0);
  365. BoundFieldModule module = BoundFieldModule.of(instance);
  366. try {
  367. Guice.createInjector(module);
  368. fail();
  369. } catch (CreationException e) {
  370. assertContains(e.getMessage(), "T cannot be used as a key; It is not fully specified.");
  371. }
  372. }
  373. public void testBindSubclassOfParameterizedTypeSucceeds() {
  374. final Integer testValue = 1024;
  375. ParameterizedObject<Integer> instance = new ParameterizedObject<Integer>(testValue) {};
  376. BoundFieldModule module = BoundFieldModule.of(instance);
  377. Injector injector = Guice.createInjector(module);
  378. assertEquals(testValue, injector.getInstance(Integer.class));
  379. }
  380. public void testBindArray() {
  381. final Integer[] testArray = new Integer[] { 1024, 2048 };
  382. Object instance = new Object() {
  383. @Bind private Integer[] anIntArray = testArray;
  384. };
  385. BoundFieldModule module = BoundFieldModule.of(instance);
  386. Injector injector = Guice.createInjector(module);
  387. assertEquals(testArray, injector.getInstance(Integer[].class));
  388. }
  389. public void testRawProviderCannotBeBound() {
  390. final Integer testValue = 1024;
  391. Object instance = new Object() {
  392. @Bind private Provider anIntProvider = new Provider() {
  393. @Override public Object get() {
  394. return testValue;
  395. }
  396. };
  397. };
  398. BoundFieldModule module = BoundFieldModule.of(instance);
  399. try {
  400. Guice.createInjector(module);
  401. fail();
  402. } catch (CreationException e) {
  403. assertContains(e.getMessage(),
  404. "Non parameterized Provider fields must have an "
  405. + "explicit binding class via @Bind(to = Foo.class)");
  406. }
  407. }
  408. public void testExplicitlyBoundRawProviderCanBeBound() {
  409. final Integer testValue = 1024;
  410. Object instance = new Object() {
  411. @Bind(to = Integer.class) private Provider anIntProvider = new Provider() {
  412. @Override public Object get() {
  413. return testValue;
  414. }
  415. };
  416. };
  417. BoundFieldModule module = BoundFieldModule.of(instance);
  418. Injector injector = Guice.createInjector(module);
  419. assertEquals(testValue, injector.getInstance(Integer.class));
  420. }
  421. public void testRawProviderCanBindToIncorrectType() {
  422. final Integer testValue = 1024;
  423. Object instance = new Object() {
  424. @Bind(to = String.class) private Provider anIntProvider = new Provider() {
  425. @Override public Object get() {
  426. return testValue;
  427. }
  428. };
  429. };
  430. BoundFieldModule module = BoundFieldModule.of(instance);
  431. Injector injector = Guice.createInjector(module);
  432. assertEquals(testValue, injector.getInstance(String.class));
  433. }
  434. public void testMultipleErrorsAreAggregated() {
  435. Object instance = new Object() {
  436. @Bind private Provider aProvider;
  437. @Bind(to = String.class) private Integer anInt;
  438. };
  439. BoundFieldModule module = BoundFieldModule.of(instance);
  440. try {
  441. Guice.createInjector(module);
  442. fail();
  443. } catch (CreationException e) {
  444. assertEquals(2, e.getErrorMessages().size());
  445. }
  446. }
  447. public void testBindingProviderWithProviderSubclassValue() {
  448. final Integer testValue = 1024;
  449. Object instance = new Object() {
  450. @Bind private Provider<Integer> anIntProvider = new IntegerProvider(testValue);
  451. };
  452. BoundFieldModule module = BoundFieldModule.of(instance);
  453. Injector injector = Guice.createInjector(module);
  454. assertEquals(testValue, injector.getInstance(Integer.class));
  455. }
  456. public void testBoundFieldsCannotBeInjected() {
  457. Object instance = new Object() {
  458. @Bind
  459. @Inject
  460. Integer anInt = 0;
  461. };
  462. BoundFieldModule module = BoundFieldModule.of(instance);
  463. try {
  464. Guice.createInjector(module);
  465. fail();
  466. } catch (CreationException e) {
  467. assertContains(
  468. e.getMessage(),
  469. "Fields annotated with both @Bind and @Inject are illegal.");
  470. }
  471. }
  472. public void testIncrementingProvider() {
  473. final Integer testBaseValue = 1024;
  474. Object instance = new Object() {
  475. @Bind private Provider<Integer> anIntProvider = new Provider<Integer>() {
  476. private int value = testBaseValue;
  477. @Override public Integer get() {
  478. return value++;
  479. }
  480. };
  481. };
  482. BoundFieldModule module = BoundFieldModule.of(instance);
  483. Injector injector = Guice.createInjector(module);
  484. assertEquals(testBaseValue, injector.getInstance(Integer.class));
  485. assertEquals((Integer) (testBaseValue + 1), injector.getInstance(Integer.class));
  486. assertEquals((Integer) (testBaseValue + 2), injector.getInstance(Integer.class));
  487. }
  488. public void testProviderDoesNotProvideDuringInjectorConstruction() {
  489. Object instance = new Object() {
  490. @Bind private Provider<Integer> myIntProvider = new Provider<Integer>() {
  491. @Override public Integer get() {
  492. throw new UnsupportedOperationException();
  493. }
  494. };
  495. };
  496. BoundFieldModule module = BoundFieldModule.of(instance);
  497. Guice.createInjector(module);
  498. // If we don't throw an exception, we succeeded.
  499. }
  500. private static class InvalidBindableClass {
  501. @Bind(to = String.class) Integer anInt;
  502. }
  503. public void testIncompatibleBindingTypeStackTraceHasUserFrame() {
  504. Object instance = new InvalidBindableClass();
  505. BoundFieldModule module = BoundFieldModule.of(instance);
  506. try {
  507. Guice.createInjector(module);
  508. fail();
  509. } catch (CreationException e) {
  510. assertContains(e.getMessage(), "at " + InvalidBindableClass.class.getName() + " field anInt");
  511. }
  512. }
  513. private static class InjectedNumberProvider implements Provider<Number> {
  514. @Inject Integer anInt;
  515. @Override public Number get() {
  516. return anInt;
  517. }
  518. }
  519. public void testBoundProvidersAreInjected() {
  520. final Integer testValue = 1024;
  521. Object instance = new Object() {
  522. @Bind private Integer anInt = testValue;
  523. @Bind private Provider<Number> aNumberProvider = new InjectedNumberProvider();
  524. };
  525. BoundFieldModule module = BoundFieldModule.of(instance);
  526. Injector injector = Guice.createInjector(module);
  527. assertEquals(testValue, injector.getInstance(Number.class));
  528. }
  529. public void testBoundInstancesAreInjected() {
  530. final Integer testValue = 1024;
  531. final InjectedNumberProvider testNumberProvider = new InjectedNumberProvider();
  532. Object instance = new Object() {
  533. @Bind private Integer anInt = testValue;
  534. @Bind private InjectedNumberProvider aNumberProvider = testNumberProvider;
  535. };
  536. BoundFieldModule module = BoundFieldModule.of(instance);
  537. Guice.createInjector(module);
  538. assertEquals(testValue, testNumberProvider.anInt);
  539. }
  540. private static class InvalidBindableSubclass extends InvalidBindableClass {}
  541. public void testClassIsPrintedInErrorsWhenCauseIsSuperclass() {
  542. Object instance = new InvalidBindableSubclass();
  543. BoundFieldModule module = BoundFieldModule.of(instance);
  544. try {
  545. Guice.createInjector(module);
  546. fail();
  547. } catch (CreationException e) {
  548. assertContains(
  549. e.getMessage(),
  550. "Requested binding type \"java.lang.String\" is not assignable from field binding type "
  551. + "\"java.lang.Integer\"");
  552. }
  553. }
  554. private static class FieldBindableSubclass2 extends FieldBindableClass {
  555. @Bind Number aNumber;
  556. FieldBindableSubclass2(Integer anInt, Number aNumber) {
  557. super(anInt);
  558. this.aNumber = aNumber;
  559. }
  560. }
  561. public void testFieldsAreBoundFromFullClassHierarchy() {
  562. final Integer testValue1 = 1024, testValue2 = 2048;
  563. FieldBindableSubclass2 instance = new FieldBindableSubclass2(testValue1, testValue2);
  564. BoundFieldModule module = BoundFieldModule.of(instance);
  565. Injector injector = Guice.createInjector(module);
  566. assertEquals(testValue1, injector.getInstance(Integer.class));
  567. assertEquals(testValue2, injector.getInstance(Number.class));
  568. }
  569. static final class LazyClass {
  570. @Bind(lazy = true) Integer foo = 1;
  571. }
  572. public void testFieldBound_lazy() {
  573. LazyClass asProvider = new LazyClass();
  574. Injector injector = Guice.createInjector(BoundFieldModule.of(asProvider));
  575. assertEquals(1, injector.getInstance(Integer.class).intValue());
  576. asProvider.foo++;
  577. assertEquals(2, injector.getInstance(Integer.class).intValue());
  578. }
  579. public void testFieldBound_lazy_rejectNull() {
  580. LazyClass asProvider = new LazyClass();
  581. Injector injector = Guice.createInjector(BoundFieldModule.of(asProvider));
  582. assertEquals(1, injector.getInstance(Integer.class).intValue());
  583. asProvider.foo = null;
  584. try {
  585. injector.getInstance(Integer.class);
  586. fail();
  587. } catch (ProvisionException e) {
  588. assertContains(e.getMessage(),
  589. "Binding to null values is not allowed. "
  590. + "Use Providers.of(null) if this is your intended behavior.");
  591. }
  592. }
  593. static final class LazyProviderClass {
  594. @Bind(lazy = true) Provider<Integer> foo = Providers.of(null);
  595. }
  596. public void testFieldBoundAsProvider_rejectProvider() {
  597. LazyProviderClass asProvider = new LazyProviderClass();
  598. try {
  599. Guice.createInjector(BoundFieldModule.of(asProvider));
  600. fail();
  601. } catch (CreationException e) {
  602. assertContains(e.getMessage(), "'lazy' is incompatible with Provider valued fields");
  603. }
  604. }
  605. }