You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@onami.apache.org by si...@apache.org on 2012/12/08 09:43:26 UTC
svn commit: r1418627 - in /incubator/onami/trunk/test/src/site/apt: core.apt
guice.apt index.apt.vm mock.apt
Author: simonetripodi
Date: Sat Dec 8 08:43:23 2012
New Revision: 1418627
URL: http://svn.apache.org/viewvc?rev=1418627&view=rev
Log:
[test] documentation tide-up
Modified:
incubator/onami/trunk/test/src/site/apt/core.apt
incubator/onami/trunk/test/src/site/apt/guice.apt
incubator/onami/trunk/test/src/site/apt/index.apt.vm
incubator/onami/trunk/test/src/site/apt/mock.apt
Modified: incubator/onami/trunk/test/src/site/apt/core.apt
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/test/src/site/apt/core.apt?rev=1418627&r1=1418626&r2=1418627&view=diff
==============================================================================
--- incubator/onami/trunk/test/src/site/apt/core.apt (original)
+++ incubator/onami/trunk/test/src/site/apt/core.apt Sat Dec 8 08:43:23 2012
@@ -1,7 +1,7 @@
------
JUnice core - The JUniceRunner
------
- The 99 Software Foundation
+ The Apache Onami developers team
------
2010
------
@@ -39,16 +39,18 @@ Getting Started
way:
+----------------------------------------+
-@RunWith(JUniceRunner.class)
-public class SimpleTest {
+@RunWith( JUniceRunner.class )
+public class SimpleTest
+{
@com.google.inject.Inject
private com.google.inject.Injector injector;
@org.junit.BeforeClass
- public static void setUpYourClass(){
+ public static void setUpYourClass()
+ {
// use injector.
- injector.getInstance(AcmeService.class);
+ injector.getInstance( AcmeService.class );
}
}
@@ -57,8 +59,9 @@ public class SimpleTest {
The <JUnice> runner can be used also in a super class:
+----------------------------------------+
-@com.google.inject.ImplementedBy(AcmeDatabasePoolImpl.class)
-public interface AcmeDatabasePool {
+@com.google.inject.ImplementedBy( AcmeDatabasePoolImpl.class )
+public interface AcmeDatabasePool
+{
void open();
@@ -67,34 +70,45 @@ public interface AcmeDatabasePool {
+----------------------------------------+
+----------------------------------------+
-@RunWith(JUniceRunner.class)
-abstract public class DatabasePoolInitTest {
+@RunWith( JUniceRunner.class )
+public abstract class DatabasePoolInitTest
+{
@com.google.inject.Inject
private static AcmeDatabasePool pool;
- @org.junit.BeforeClass
- public static void setUpYourClass(){
- pool = injector.getInstance(AcmeDatabasePool.class);
+ public void setPool( AcmeDatabasePool pool )
+ {
+ this.pool = pool;
+ }
+
+ @org.junit.BeforeClass
+ public static void setUpYourClass()
+ {
+ ...
pool.open();
...
- }
+ }
- @org.junit.AfterClass
- public static void setUpYourClass(){
+ @org.junit.AfterClass
+ public static void tearDownYourClass()
+ {
...
pool.close();
...
- }
+ }
}
+----------------------------------------+
+----------------------------------------+
-public class DatabaseClassTest extends DatabasePoolInitTest{
+public class DatabaseClassTest
+ extends DatabasePoolInitTest
+{
@org.junit.Test
- public void test(){
+ public void test()
+ {
...
}
Modified: incubator/onami/trunk/test/src/site/apt/guice.apt
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/test/src/site/apt/guice.apt?rev=1418627&r1=1418626&r2=1418627&view=diff
==============================================================================
--- incubator/onami/trunk/test/src/site/apt/guice.apt (original)
+++ incubator/onami/trunk/test/src/site/apt/guice.apt Sat Dec 8 08:43:23 2012
@@ -1,7 +1,7 @@
------
Guice Integration
------
- The 99 Software Foundation
+ The Apache Onami developers team
------
2010
------
@@ -41,11 +41,14 @@ Guice integration!
module:
+----------------------------------------+
-public class SimpleModule extends AbstractModule {
+public class SimpleModule
+ extends AbstractModule
+{
@Override
- protected void configure() {
- bind(Hello.class).to(HelloWorld.class);
+ protected void configure()
+ {
+ bind( Hello.class ).to( HelloWorld.class );
}
}
@@ -54,33 +57,34 @@ public class SimpleModule extends Abstra
then users can reuse it specifying
+----------------------------------------+
-@RunWith(JUniceRunner.class)
-@GuiceModules(modules={ SimpleModule.class,
- AnotherAcmeModule.class })
-public class SimpleTest {
-
- @com.google.inject.Inject
- private Hello helloWorld;
-
- @org.junit.Test
- public void testInjectNotStatic() {
- assertNotNull(helloWorld);
- assertEquals("Hello World!!!!", helloWorld.sayHallo());
- }
+@RunWith( JUniceRunner.class )
+@GuiceModules( { SimpleModule.class, AnotherAcmeModule.class } )
+public class SimpleTest
+{
+
+ @com.google.inject.Inject
+ private Hello helloWorld;
+
+ @org.junit.Test
+ public void testInjectNotStatic()
+ {
+ assertNotNull( helloWorld );
+ assertEquals( "Hello World!!!!", helloWorld.sayHallo() );
+ }
}
+----------------------------------------+
-
* GuiceProvidedModule annotation
GuiceProvidedModule is usefull when your test needs a module that not have a default costructor.
So in you testcase you have to declare a <<public static>> method with the return type is <com.google.inject.Module>
or <Iterable\<com.google.inject.Module\>> or or <com.google.inject.Module[]>
-
+----------------------------------------+
-public class ComplexModule extends AbstractModule {
+public class ComplexModule
+ extends AbstractModule
+{
private String name;
@@ -90,30 +94,33 @@ public class ComplexModule extends Abstr
@Override
protected void configure() {
- bind(WhoIm.class).toInstance(new WhoIm(name));
+ bind( WhoIm.class ).toInstance( new WhoIm(name) );
}
}
+----------------------------------------+
-
+ then, in the test class:
+----------------------------------------+
-@RunWith(JUniceRunner.class)
-public class SimpleTest {
+@RunWith( JUniceRunner.class )
+public class SimpleTest
+{
@GuiceProvidedModules
- public static Module createComplexModule(){
- return new ComplexModule("Marco Speranza");
+ public static Module createComplexModule()
+ {
+ return new ComplexModule( "Marco Speranza" );
}
@com.google.inject.Inject
public static WhoIm whoIm;
@org.junit.Test
- public void testWhoIm() {
- assertNotNull(whoIm);
- assertEquals("Marco Speranza", whoIm.sayWhoIm());
+ public void testWhoIm()
+ {
+ assertNotNull( whoIm );
+ assertEquals( "Marco Speranza", whoIm.sayWhoIm() );
}
}
@@ -124,24 +131,26 @@ public class SimpleTest {
<<<com.google.inject.Module>>>
+----------------------------------------+
-@RunWith(JUniceRunner.class)
-public class SimpleTest extends AbstractModule {
+@RunWith( JUniceRunner.class )
+public class SimpleTest
+ extends AbstractModule
+{
@Override
- public void configure() {
- bind(Integer.class)
- .annotatedWith(Names.named("version"))
- .toInstance(10);
+ public void configure()
+ {
+ bind( Integer.class ).annotatedWith( Names.named("version") ).toInstance( 10 );
}
@Inject
- @Named("version")
+ @Named( "version" )
private Integer version;
@Test
- public void testInjectModuleClass(){
- assertNotNull(version);
- assertEquals(10, version.intValue());
+ public void testInjectModuleClass()
+ {
+ assertNotNull( version );
+ assertEquals( 10, version.intValue() );
}
}
Modified: incubator/onami/trunk/test/src/site/apt/index.apt.vm
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/test/src/site/apt/index.apt.vm?rev=1418627&r1=1418626&r2=1418627&view=diff
==============================================================================
--- incubator/onami/trunk/test/src/site/apt/index.apt.vm (original)
+++ incubator/onami/trunk/test/src/site/apt/index.apt.vm Sat Dec 8 08:43:23 2012
@@ -1,7 +1,7 @@
------
- Welcome to JUnice
+ Welcome to ${project.name}
------
- The 99 Software Foundation
+ The Apache Onami developers team
------
2010
------
@@ -24,9 +24,9 @@
~~ under the License.
~~
-Welcome to JUnice!
+Welcome to ${project.name}!
- JUnice is a small java library for <JUnit> that minimizes the boilerplate code
+ ${project.name} is a small java library for <JUnit> that minimizes the boilerplate code
in your unit test relying on <Google Guice> to inject directly into your test
cases all the required dependecies.
Moreover it makes easier creating and injecting Mock and Stubs for your test
@@ -38,21 +38,12 @@ Before Coding...
+----------------------------------------+
<dependencies>
- ...
- <dependency>
- <groupId>org.nnsoft.guice.junice</groupId>
- <artifactId>junice</artifactId>
- <version>${project.version}</version>
- </dependency>
- ...
+ ...
+ <dependency>
+ <groupId>${project.groupId}</groupId>
+ <artifactId>${project.artifactId}</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ ...
</dependencies>
+----------------------------------------+
-
-
-Acknowledgements
-
- This work is dedicated to our city, L'Aquila, destroyed by a terrible
- earthquake the 6th April, 2009... That day more than 300 people were killed
- because buildings collapsed after a magnitudo 6.3 earthquake at 3:32 am.
-
- We'll never forget that episode.
Modified: incubator/onami/trunk/test/src/site/apt/mock.apt
URL: http://svn.apache.org/viewvc/incubator/onami/trunk/test/src/site/apt/mock.apt?rev=1418627&r1=1418626&r2=1418627&view=diff
==============================================================================
--- incubator/onami/trunk/test/src/site/apt/mock.apt (original)
+++ incubator/onami/trunk/test/src/site/apt/mock.apt Sat Dec 8 08:43:23 2012
@@ -1,7 +1,7 @@
------
Mock framework
------
- The 99 Software Foundation
+ The Apache Onami developers team
------
2010
------
@@ -39,8 +39,6 @@ Mock framework
<<by default>> JUnice creates Easy-Mock mock objects.
-
-
* Choose your favorite mock framework
To choose a different mock framework that <JUnice> shuld be used to create your mock object,
@@ -48,15 +46,14 @@ Mock framework
+----------------------------------------+
-@RunWith(JUniceRunner.class)
-@MockFramework(type=MockType.MOCKITO)
-public class SimpleTest {
+@RunWith( JUniceRunner.class )
+@MockFramework( MockType.MOCKITO )
+public class SimpleTest
+{
}
+----------------------------------------+
-
-
* Mock annotation
<<<org.nnsoft.guice.junice.annotation.Mock>>> is a field annotation used by core <Runner> to
@@ -64,7 +61,6 @@ public class SimpleTest {
<<<org.nnsoft.guice.junice.annotation.Mock>>> can be configured with this parameters:
-
*----+------+------+
|| Property || Type || Description |
*----+------+------+
@@ -81,21 +77,20 @@ public class SimpleTest {
| <<type>> | <<<org.nnsoft.guice.junice.annotation.MockObjType>>> | Specifies the mock type. |
*----+------+------+
-
-
In the following example <HelloWorld> class dependencies will be injected with
the mocked class created by the <Runner>:
+----------------------------------------+
-
-public class HelloWorld {
+public class HelloWorld
+{
@Inject
private Service service;
...
- public String sayHalloByService() {
+ public String sayHalloByService()
+ {
return service.go();
}
@@ -103,13 +98,12 @@ public class HelloWorld {
}
+----------------------------------------+
-
Then, in the test class:
+----------------------------------------+
-
-@RunWith(JUniceRunner.class)
-public class SimpleTest {
+@RunWith( JUniceRunner.class )
+public class SimpleTest
+{
//Create and inject a simple EasyMock Strict mock
@org.nnsoft.guice.junice.annotation.Mock
@@ -119,13 +113,14 @@ public class SimpleTest {
private HelloWorld helloWorld;
@Test
- public void testMock(){
+ public void testMock()
+ {
//Stub created mock
- EasyMock.expect(service.go()).andReturn("Ciao");
- EasyMock.replay(providedMock);
+ EasyMock.expect( service.go() ).andReturn( "Ciao" );
+ EasyMock.replay( providedMock );
- assertNotNull(service);
- assertEquals("Ciao", helloWorld.sayHalloByService());
+ assertNotNull( service );
+ assertEquals( "Ciao", helloWorld.sayHalloByService());
EasyMock.verify(service);
}
@@ -133,8 +128,6 @@ public class SimpleTest {
}
+----------------------------------------+
-
-
** Mock resetAfter
<<Note>>
@@ -142,7 +135,6 @@ public class SimpleTest {
a mock has to be resetted manually, fields must to be annotated in this way:
+----------------------------------------+
-
...
@Mock(resetAfter=false)
@@ -151,66 +143,63 @@ public class SimpleTest {
...
+----------------------------------------+
-
** Mock provider
The Runner class create a simple mock for each <<<...@Mock>>> annotated filed.
- You shuld delegate the mock creation to a method in this way:
+ You should delegate the mock creation to a method in this way:
+----------------------------------------+
+@RunWith( JUniceRunner.class )
+public class SimpleTest
+{
-@RunWith(JUniceRunner.class)
-public class SimpleTest {
-
- @Mock(providedBy="providerMethod")
+ @Mock( providedBy="providerMethod" )
private Service service;
- public static Service providerMethod() {
- Service mocked = EasyMock.createNiceMock(Service.class);
+ public static Service providerMethod() {
+ Service mocked = EasyMock.createNiceMock(Service.class);
- ...
+ ...
- return mockedService;
- }
+ return mockedService;
+ }
- ...
+ ...
}
+----------------------------------------+
-
It's possible also delegate an external class:
+----------------------------------------+
+public class ServiceMockProvider
+{
-public class ServiceMockProvider {
-
- public static Service providerMethod() {
- Service mocked = EasyMock.createNiceMock(Service.class);
+ public static Service providerMethod()
+ {
+ Service mocked = EasyMock.createNiceMock( Service.class );
- ...
+ ...
- return mockedService;
- }
+ return mockedService;
+ }
}
+----------------------------------------+
+----------------------------------------+
+@RunWith( JUniceRunner.class )
+public class SimpleTest
+{
-@RunWith(JUniceRunner.class)
-public class SimpleTest {
-
- @Mock(providedBy="providerMethod",
- providerClass=ServiceMockProvider.class)
+ @Mock( providedBy = "providerMethod",
+ providerClass = ServiceMockProvider.class )
private Service service;
...
}
+----------------------------------------+
-
-
** Mock type
The Runner class create a simple mock for each <<<...@Mock>>> annotated filed.
@@ -223,8 +212,6 @@ public class SimpleTest {
* EASY_MOCK_NICE to create a nice EasyMock object mock
-
-
* Replace module
<JUnice> core <Runner> will replace all existing bindings found into modules declared via
@@ -234,62 +221,59 @@ public class SimpleTest {
So given google-guice module:
+----------------------------------------+
-
-public class ServiceModule extends AbstractModule {
+public class ServiceModule
+ extends AbstractModule
+{
@Override
- protected void configure() {
- bind(Service.class)
- .to(ServiceImpl.class)
- .asEagerSingleton();
+ protected void configure()
+ {
+ bind( Service.class).to( ServiceImpl.class ).asEagerSingleton();
...
}
}
+----------------------------------------+
-
and your implementation
+----------------------------------------+
+public class ServiceImpl
+ implements Service
+{
-public class ServiceImpl implements Service {
-
- public String go() {
+ public String go()
+ {
return "It's real class";
}
}
+----------------------------------------+
-
-
+----------------------------------------+
-
-@RunWith(JUniceRunner.class)
-@GuiceModules(modules=ServiceModule.class)
-public class TestCustomInjectionTest {
+@RunWith( JUniceRunner.class )
+@GuiceModules( ServiceModule.class )
+public class TestCustomInjectionTest
+{
@Mock
private static Service service;
@Test
- public void testOverideModule() throws Exception {
- assertNotNull(service);
+ public void testOverideModule()
+ throws Exception
+ {
+ assertNotNull( service );
- EasyMock.expect(service.go())
- .andReturn("Mocked injected class");
+ EasyMock.expect( service.go() ).andReturn( "Mocked injected class" );
- assertEquals("Mocked injected class", service.go());
+ assertEquals( "Mocked injected class", service.go() );
}
}
-
+----------------------------------------+
-
service instance will be injected with the mocked object.
-
* Multiple annotation for same type
It's possible annotate multiple types with the <<<org.nnsoft.guice.junice.annotation.Mock>>> annotation
@@ -315,14 +299,15 @@ public class TestCustomInjectionTest {
and the field will be injected <<only>> into the test class.
So if necessary you have to create a proper binding for this <<<org.nnsoft.guice.junice.annotation.Mock>>> field:
-
+----------------------------------------+
-
-@RunWith(JUniceRunner.class)
-public class TestCustomInjectionTest extendes Module {
-
- public void configure(Binder binder) {
- binder.bind(Service.class).toInstance(serviceOne);
+@RunWith( JUniceRunner.class )
+public class TestCustomInjectionTest
+ implements Module
+{
+
+ public void configure( Binder binder )
+ {
+ binder.bind( Service.class ).toInstance( serviceOne );
}
@Mock
@@ -335,20 +320,14 @@ public class TestCustomInjectionTest ext
private HelloWorld helloWorld;
@Test
- public void testOverideModule() throws Exception {
- assertNotNull(serviceOne);
- assertNotNull(serviceTwo);
+ public void testOverideModule()
+ throws Exception
+ {
+ assertNotNull( serviceOne );
+ assertNotNull( serviceTwo );
}
}
+----------------------------------------+
-
-
in this case <<<HelloWorld>>> class has a dependency with <<<Service>>> interface. If
- you dont specify a proper binding <<<Google Guice>>> raises an <<<com.google.inject.ConfigurationExcepion>>>.
-
-
-
-
-
-
+ you don't specify a proper binding <<<Google Guice>>> raises an <<<com.google.inject.ConfigurationExcepion>>>.