[atunit commit] r17 - in trunk: example/atunit/example src/atunit src/atunit/core src/atunit/spring

0 views
Skip to first unread message

codesite...@google.com

unread,
Nov 8, 2007, 10:07:00 PM11/8/07
to atunit...@googlegroups.com
Author: logan.johnson
Date: Thu Nov 8 19:06:53 2007
New Revision: 17

Modified:
trunk/example/atunit/example/ExampleGuiceAndJMockTest.java
trunk/example/atunit/example/ExampleSpringEasyMockTest.java
trunk/src/atunit/AtUnit.java
trunk/src/atunit/core/NoContainer.java
trunk/src/atunit/spring/SpringContainer.java

Log:
- SpringContainer: @Bean is optional for @Mock and @Stub fields; can
be used to specify bean name

- AtUnit: any field values set by the MockFramework and not injected by
the Container will be injected by AtUnit. Upshot is you don't usually
need @Inject or @Bean on your mocks and stubs.

Modified: trunk/example/atunit/example/ExampleGuiceAndJMockTest.java
==============================================================================
--- trunk/example/atunit/example/ExampleGuiceAndJMockTest.java (original)
+++ trunk/example/atunit/example/ExampleGuiceAndJMockTest.java Thu Nov
8 19:06:53 2007
@@ -40,10 +40,14 @@


/**
- * This example shows the full power of AtUnit. Mock objects are
supplied by JMock,
- * just as in {@link ExampleJMockTest}. Dependency injection is
provided by Guice,
- * just as in ExampleGuiceTest.
- *
+ * This example shows the full power of AtUnit. Mock objects are
supplied by
+ * JMock, just as in {@link ExampleJMockTest}. Dependency injection is provided
+ * by Guice, just as in ExampleGuiceTest.
+ *
+ * Any fields created by the MockFramework and not injected by the Container
+ * will be injected by AtUnit itself, so there's usually no need to
use Guice
+ * annotations on your Mockery, mocks, or stubs.
+ *
* @author Logan Johnson <logan....@gmail.com>
*/
@RunWith(AtUnit.class)
@@ -51,11 +55,14 @@
@MockFramework(MockFramework.Option.JMOCK)
public class ExampleGuiceAndJMockTest implements Module {

- @Inject Mockery mockery;
- @Inject @Mock ExampleInterface myMock;
- @Inject @Stub IgnoredInterface ignored;
- @Inject @Named("field") String setting;
@Inject @Unit ExampleClass myUnit;
+
+ Mockery mockery;
+ @Mock ExampleInterface myMock;
+ @Stub IgnoredInterface ignored;
+
+ @Inject @Named("field") String setting;
+

public void configure(Binder b) {
b.bind(String.class).annotatedWith(Names.named("field")).toInstance("hooray");

Modified: trunk/example/atunit/example/ExampleSpringEasyMockTest.java
==============================================================================
--- trunk/example/atunit/example/ExampleSpringEasyMockTest.java (original)
+++ trunk/example/atunit/example/ExampleSpringEasyMockTest.java Thu Nov
8 19:06:53 2007
@@ -46,13 +46,12 @@
@Bean @Unit SimpleManager manager;

/**
- * Mocks and Stubs that are also annotated with Bean will be placed
into the application context like
- * any other bean. If a name is specified, it will be used. If no
name is specified, one will be generated.
- * Mock or Stub Beans are candidates for autowiring by type.
- *
- * In other words: Declare a Mock or Stub field, annotate it with
Bean, and it's just like you've put it
- * in the XML.
+ * Mocks and Stubs are automatically placed into the application context
+ * like any other bean. If you also annotate them with Bean, you can give
+ * them a bean name which will be used in the context.
*
+ * In other words: Declare a Mock or Stub field, and it's just like you've
+ * put it in the XML.
*/
@Bean("daoBean") @Mock SimpleDao dao;


Modified: trunk/src/atunit/AtUnit.java
==============================================================================
--- trunk/src/atunit/AtUnit.java (original)
+++ trunk/src/atunit/AtUnit.java Thu Nov 8 19:06:53 2007
@@ -48,23 +48,28 @@
Container container = getContainerFor(c);
MockFramework mockFramework = getMockFrameworkFor(c);

- final Map<Field,Object> fieldValues = new HashMap<Field,Object>();
-
// make sure we have one (and only one) @Unit field
Field unitField = getUnitField(c);
if ( unitField.getAnnotation(Mock.class) != null ) {
throw new IncompatibleAnnotationException(Unit.class, Mock.class);
}

- Map<Field,Object> mfValues = mockFramework.getValues(c.getDeclaredFields());
- if ( mfValues.containsKey(unitField)) {
+ final Map<Field,Object> fieldValues = mockFramework.getValues(c.getDeclaredFields());
+ if ( fieldValues.containsKey(unitField)) {
throw new IncompatibleAnnotationException(Unit.class, unitField.getType());
}
- for ( Field field : mfValues.keySet() ) {
- fieldValues.put(field, mfValues.get(field));
+
+ Object test = container.createTest(c, fieldValues);
+
+ // any field values created by AtUnit but not injected by the
container are injected here.
+ for ( Field field : fieldValues.keySet() ) {
+ field.setAccessible(true);
+ if ( field.get(test) == null ) {
+ field.set(test, fieldValues.get(field));
+ }
}

- return container.createTest(c, fieldValues);
+ return test;
}

protected Field getUnitField(Class<?> testClass) throws
NoUnitException, TooManyUnitsException {

Modified: trunk/src/atunit/core/NoContainer.java
==============================================================================
--- trunk/src/atunit/core/NoContainer.java (original)
+++ trunk/src/atunit/core/NoContainer.java Thu Nov 8 19:06:53 2007
@@ -22,12 +22,7 @@
public class NoContainer implements Container {

public Object createTest(Class<?> testClass, Map<Field, Object>
fieldValues) throws Exception {
- Object testInstance = testClass.newInstance();
- for ( Field field : fieldValues.keySet() ) {
- field.setAccessible(true); // take that, security!
- field.set(testInstance, fieldValues.get(field));
- }
- return testInstance;
+ return testClass.newInstance();
}

}

Modified: trunk/src/atunit/spring/SpringContainer.java
==============================================================================
--- trunk/src/atunit/spring/SpringContainer.java (original)
+++ trunk/src/atunit/spring/SpringContainer.java Thu Nov 8 19:06:53 2007
@@ -42,11 +42,10 @@
for ( Field field : fieldValues.keySet() ) {

Bean beanAnno = field.getAnnotation(Bean.class);
- if ( beanAnno == null ) continue;

AbstractBeanDefinition beandef =
defineInstanceHolderFactoryBean(field.getType(), fieldValues.get(field));

- if (!beanAnno.value().equals("")) {
+ if ((beanAnno != null) && !beanAnno.value().equals("")) {
ctx.registerBeanDefinition(beanAnno.value(), beandef);
} else {
BeanDefinitionReaderUtils.registerWithGeneratedName(beandef, ctx);

Reply all
Reply to author
Forward
0 new messages