You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tuscany.apache.org by Luciano Resende <lu...@gmail.com> on 2007/01/23 09:44:35 UTC
Help with unit testing using easyMock
I'm trying to use easyMock to create a unit testing to verify that exception
is being thrown for duplicated Components...
Looks like the reader that is simulating the scdl is returning the right
values, but then, the registry does not find a loader to handle
implementation.java
Portion given by the reader so far :
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
name="composite_component-include2">
<component name="component">
<implementation.java class="
org.apache.tuscany.core.mock.component.BasicInterfaceImpl"/>
It's failing as it's not finding the proper loader to handle
implementation.java... How do I tell get the registry properly loaded with
the proper loaders registred ? Or any other way I should handle this ?
Code:
public void testLoadDuplicatedComponentTypeFromMock() throws Exception{
/*
//main
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
name="CompositeComponent">
<include name="composite_component-include1"
scdlLocation="composite_component-include1.scdl"/>
<include name="composite_component-include2"
scdlLocation="composite_component-include2.scdl"/>
</composite>
//include 1
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
name="composite_component-include2">
<component name="component">
<implementation.java class="
org.apache.tuscany.core.mock.component.BasicInterfaceImpl"/>
</component>
<service name="service" >
<interface.java interface="
org.apache.tuscany.core.mock.component.BasicInterface"/>
<reference>component</reference>
</service>
</composite>
*/
EasyMock.expect(mockReader.getName()).andReturn(COMPOSITE);
EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.isA(String.class))).andReturn(COMPOSITE_NAME);
EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.START_ELEMENT);
EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.isA(String.class))).andReturn(COMPOSITE_NAME);
//first component
EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.START_ELEMENT);
EasyMock.expect(mockReader.getName()).andReturn(COMPONENT);
EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.isA(String.class))).andReturn(COMPONENT_NAME);
EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.eq("initLevel"))).andReturn(null);
EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
String.class), EasyMock.isA(String.class))).andReturn(null);
EasyMock.expect(mockReader.nextTag()).andReturn(0);
EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
String.class), EasyMock.isA(String.class))).andReturn(null);
EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.START_ELEMENT);
EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.END_ELEMENT);
EasyMock.expect(mockReader.getName()).andReturn(null
/*COMPONENT_IMPLEMENTATION_JAVA*/);
EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.isA(String.class))).andReturn(null
/*COMPONENT_IMPLEMENTATION_JAVA_CLASS*/);
EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.eq("initLevel"))).andReturn(null);
EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
String.class), EasyMock.isA(String.class))).andReturn(null);
EasyMock.expect(mockReader.nextTag()).andReturn(0);
EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
String.class), EasyMock.isA(String.class))).andReturn(null);
EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.END_ELEMENT);
//second and duplicated component
EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.START_ELEMENT);
EasyMock.expect(mockReader.getName()).andReturn(COMPONENT);
EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.isA(String.class))).andReturn(COMPONENT_NAME);
EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
String.class), EasyMock.isA(String.class))).andReturn(null);
EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.START_ELEMENT);
EasyMock.expect(mockReader.getName
()).andReturn(COMPONENT_IMPLEMENTATION_JAVA);
EasyMock.expect(mockReader.getAttributeValue((String)
EasyMock.isNull(), EasyMock.isA(String.class
))).andReturn(COMPONENT_IMPLEMENTATION_JAVA_CLASS);
EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
String.class), EasyMock.isA(String.class))).andReturn(null);
EasyMock.expect(mockReader.next()).andReturn(
XMLStreamConstants.END_ELEMENT);
//replay reader
EasyMock.replay(mockReader);
ModelObject modelObject = new ModelObject() {};
CompositeComponentType composite = loader.load(
EasyMock.createNiceMock(CompositeComponent.class),
modelObject,
mockReader,
EasyMock.createNiceMock(DeploymentContext.class));
assertEquals(COMPOSITE_NAME, composite.getName());
}
Exception:
java.lang.NullPointerException
at org.apache.tuscany.spi.loader.UnrecognizedElementException.<init>(
UnrecognizedElementException.java:39)
at org.apache.tuscany.core.loader.LoaderRegistryImpl.load(
LoaderRegistryImpl.java:82)
at org.apache.tuscany.core.loader.ComponentLoader.loadImplementation(
ComponentLoader.java:199)
at org.apache.tuscany.core.loader.ComponentLoader.load(
ComponentLoader.java:110)
at org.apache.tuscany.core.loader.ComponentLoader.load(
ComponentLoader.java:79)
at org.apache.tuscany.core.loader.LoaderRegistryImpl.load(
LoaderRegistryImpl.java:84)
at org.apache.tuscany.core.implementation.composite.CompositeLoader.load
(CompositeLoader.java:96)
at org.apache.tuscany.core.implementation.composite.CompositeLoader.load
(CompositeLoader.java:68)
at org.apache.tuscany.core.loader.LoaderRegistryImpl.load(
LoaderRegistryImpl.java:84)
at org.apache.tuscany.core.implementation.composite.CompositeLoader.load
(CompositeLoader.java:96)
at
org.apache.tuscany.core.implementation.composite.CompositeLoaderTestCase.testLoadDuplicatedComponentTypeFromMock
(CompositeLoaderTestCase.java:152)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(
NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(
DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(
JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(
TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(
RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
RemoteTestRunner.java:196)
--
Luciano Resende
http://people.apache.org/~lresende
Re: Help with unit testing using easyMock
Posted by Jim Marino <jm...@myromatours.com>.
On Jan 23, 2007, at 10:02 AM, Luciano Resende wrote:
> Well, it looks like I might be doing more then what is required
> here, but
> I'll try to give some more comments on the scenario.
>
>> I assume you are trying to test an ImplementationLoader for its
>> handling of
>> duplicate components. If so, then you must simply have to focus
>> on that
>> 'unit' i.e. the ImplementationLoader. You need not test if the
>> loader is
>> registered or picked up from the registry and invoked. These
>> things you
>> assume and just start with the phase when the loader is actually
>> invoked.
>
> Yes, I'm trying to check for the duplicated components, and I have
> added
> code to handle that inside CompositeLoader.java
>
>> Through the mock reader you only need to feed those inputs that
>> the Loader
>> actually reads from the XMLReader argument. Basically you provide
>> mock
>> inputs just to the Loader and test how it handles these inputs.
>> So in your
>> case you might be feeding in duplicates to see if the loader is
>> able to
>> detect this.
>
> So, I'm trying to mock the necessary reads just to check the
> duplicate, and
> the way I was thinking to do this was to provide mocked reader that
> would
> return two components with the same name,
> but while loading the composite, compositeLoader will pass control to
> various other loaders, and then will fail inside ComponentLoader with
> inrecognizedElement exception trying to get a loder for the <
> implementation.java... element.:
>
> Code where it starts passing control to the registry to load other
> ares of
> the scdl returned by the mocked reader:
>
> switch (reader.next()) {
> case START_ELEMENT:
> ModelObject o = registry.load(parent, composite,
> reader,
> deploymentContext);
>
> Chain of control:
>
> CompositeLoader
> LoaderRegistryImpl
> CompositeLoader
> ComponentLoader
>
> Where it fails in ComponentLoader
>
> try {
> Implementation<?> impl = loadImplementation(parent, reader,
> deploymentContext);
> registry.loadComponentType(parent, impl,
> deploymentContext);
>
>
> So, my question is more around, how can I test the CompositeLoader
> without
> having to go that farther in the code chain where other things are
> required
> ?
>
> I'll take a look at the read resources Jim sent and see if those
> give me any
> more ideas...
>
Hi Luciano,
I think the following skeleton may help - it demonstrates how to just
test the loader with the other infrastructure classes mocked out:
public class CompositeLoaderTestCase extends TestCase {
private CompositeComponent parent;
private DeploymentContext context;
private ArtifactRepository repo;
public void testFoo() throws Exception {
ComponentDefinition definition = new
ComponentDefinition<Implementation<?>>(null);
LoaderRegistry registry = EasyMock.createMock
(LoaderRegistry.class);
EasyMock.expect(registry.load(EasyMock.isA
(CompositeComponent.class),
EasyMock.isA(ModelObject.class),
EasyMock.isA(XMLStreamReader.class),
EasyMock.isA(DeploymentContext.class))).andReturn
(definition);
EasyMock.replay(registry);
XMLStreamReader reader = EasyMock.createMock
(XMLStreamReader.class);
EasyMock.expect(reader.getAttributeValue(null,
"name")).andReturn("foo");
EasyMock.expect(reader.next()).andReturn
(XMLStreamConstants.START_ELEMENT);
EasyMock.expect(reader.next()).andReturn
(XMLStreamConstants.END_ELEMENT);
EasyMock.expect(reader.next()).andReturn
(XMLStreamConstants.END_ELEMENT);
EasyMock.expect(reader.getName()).andReturn
(CompositeLoader.COMPOSITE);
EasyMock.replay(reader);
CompositeLoader loader = new CompositeLoader(registry, repo);
CompositeComponentType<ServiceDefinition,
ReferenceDefinition, Property<?>> type =
loader.load(parent, null, reader, context);
EasyMock.verify(registry);
EasyMock.verify(reader);
}
protected void setUp() throws Exception {
super.setUp();
parent = EasyMock.createMock(CompositeComponent.class);
EasyMock.replay(parent);
context = EasyMock.createMock(DeploymentContext.class);
EasyMock.replay(context);
repo = EasyMock.createMock(ArtifactRepository.class);
EasyMock.replay(repo);
}
}
HTH
Jim
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-dev-help@ws.apache.org
Re: Help with unit testing using easyMock
Posted by Luciano Resende <lu...@gmail.com>.
Well, it looks like I might be doing more then what is required here, but
I'll try to give some more comments on the scenario.
>I assume you are trying to test an ImplementationLoader for its handling of
>duplicate components. If so, then you must simply have to focus on that
>'unit' i.e. the ImplementationLoader. You need not test if the loader is
>registered or picked up from the registry and invoked. These things you
>assume and just start with the phase when the loader is actually invoked.
Yes, I'm trying to check for the duplicated components, and I have added
code to handle that inside CompositeLoader.java
>Through the mock reader you only need to feed those inputs that the Loader
>actually reads from the XMLReader argument. Basically you provide mock
>inputs just to the Loader and test how it handles these inputs. So in your
>case you might be feeding in duplicates to see if the loader is able to
>detect this.
So, I'm trying to mock the necessary reads just to check the duplicate, and
the way I was thinking to do this was to provide mocked reader that would
return two components with the same name,
but while loading the composite, compositeLoader will pass control to
various other loaders, and then will fail inside ComponentLoader with
inrecognizedElement exception trying to get a loder for the <
implementation.java... element.:
Code where it starts passing control to the registry to load other ares of
the scdl returned by the mocked reader:
switch (reader.next()) {
case START_ELEMENT:
ModelObject o = registry.load(parent, composite, reader,
deploymentContext);
Chain of control:
CompositeLoader
LoaderRegistryImpl
CompositeLoader
ComponentLoader
Where it fails in ComponentLoader
try {
Implementation<?> impl = loadImplementation(parent, reader,
deploymentContext);
registry.loadComponentType(parent, impl, deploymentContext);
So, my question is more around, how can I test the CompositeLoader without
having to go that farther in the code chain where other things are required
?
I'll take a look at the read resources Jim sent and see if those give me any
more ideas...
--
Luciano Resende
http://people.apache.org/~lresende
On 1/23/07, Jim Marino <jm...@myromatours.com> wrote:
>
>
> On Jan 23, 2007, at 9:00 AM, Jeremy Boynes wrote:
>
> > Isn't this a problem in the verify/build phase rather than the loader?
> > --
> > Jeremy
> >
> We already support this in the build phase. Ultimately this function
> would be moved into the verify phase but since we don't have it yet
> and the change is minimal, we can get it in and move it once I get to
> separating the phases.
>
> Jim
>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
> > For additional commands, e-mail: tuscany-dev-help@ws.apache.org
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
> For additional commands, e-mail: tuscany-dev-help@ws.apache.org
>
>
Re: Help with unit testing using easyMock
Posted by Jim Marino <jm...@myromatours.com>.
On Jan 23, 2007, at 10:01 AM, Jeremy Boynes wrote:
> Do we?
Yep in CompositeComponentExtension.register(SCAObject)
> I was looking at CompositeComponentType#getComponents and it does:
> for (Include i : includes.values()) {
> view.putAll(i.getIncluded().getComponents());
> }
> which looks like it just drops duplicates.
>
Yes that is bad. I was referring to the above method
> I think we should check in two places:
> 1) when adding a component, reference or service to the model that
> no other has the same name
> 2) when merging includes that there are no duplicates across the merge
>
+1 as long as it is not done implicit in the model (i.e. the model
should have no behavior). I think that is aligned with what you say
below w.r.t the builder.
> I don't think the second should be done silently in getComponents()
> as that is a bit obscure - better to do an explicit check in the
> builder (until that check can be moved to verify).
>
> --
> Jeremy
>
> On Jan 23, 2007, at 9:30 AM, Jim Marino wrote:
>
>>
>> On Jan 23, 2007, at 9:00 AM, Jeremy Boynes wrote:
>>
>>> Isn't this a problem in the verify/build phase rather than the
>>> loader?
>>> --
>>> Jeremy
>>>
>> We already support this in the build phase. Ultimately this
>> function would be moved into the verify phase but since we don't
>> have it yet and the change is minimal, we can get it in and move
>> it once I get to separating the phases.
>>
>> Jim
>>
>>>
>>> --------------------------------------------------------------------
>>> -
>>> To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
>>> For additional commands, e-mail: tuscany-dev-help@ws.apache.org
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
>> For additional commands, e-mail: tuscany-dev-help@ws.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
> For additional commands, e-mail: tuscany-dev-help@ws.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-dev-help@ws.apache.org
Re: Help with unit testing using easyMock
Posted by Jeremy Boynes <jb...@apache.org>.
Do we? I was looking at CompositeComponentType#getComponents and it
does:
for (Include i : includes.values()) {
view.putAll(i.getIncluded().getComponents());
}
which looks like it just drops duplicates.
I think we should check in two places:
1) when adding a component, reference or service to the model that no
other has the same name
2) when merging includes that there are no duplicates across the merge
I don't think the second should be done silently in getComponents()
as that is a bit obscure - better to do an explicit check in the
builder (until that check can be moved to verify).
--
Jeremy
On Jan 23, 2007, at 9:30 AM, Jim Marino wrote:
>
> On Jan 23, 2007, at 9:00 AM, Jeremy Boynes wrote:
>
>> Isn't this a problem in the verify/build phase rather than the
>> loader?
>> --
>> Jeremy
>>
> We already support this in the build phase. Ultimately this
> function would be moved into the verify phase but since we don't
> have it yet and the change is minimal, we can get it in and move it
> once I get to separating the phases.
>
> Jim
>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
>> For additional commands, e-mail: tuscany-dev-help@ws.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
> For additional commands, e-mail: tuscany-dev-help@ws.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-dev-help@ws.apache.org
Re: Help with unit testing using easyMock
Posted by Jim Marino <jm...@myromatours.com>.
On Jan 23, 2007, at 9:00 AM, Jeremy Boynes wrote:
> Isn't this a problem in the verify/build phase rather than the loader?
> --
> Jeremy
>
We already support this in the build phase. Ultimately this function
would be moved into the verify phase but since we don't have it yet
and the change is minimal, we can get it in and move it once I get to
separating the phases.
Jim
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
> For additional commands, e-mail: tuscany-dev-help@ws.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-dev-help@ws.apache.org
Re: Help with unit testing using easyMock
Posted by Jeremy Boynes <jb...@apache.org>.
Isn't this a problem in the verify/build phase rather than the loader?
--
Jeremy
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-dev-help@ws.apache.org
Re: Help with unit testing using easyMock
Posted by Venkata Krishnan <fo...@gmail.com>.
Hi Luciano,
I am just about sharing my experience in this without going too much into
what you are doing. Hope this helps. If you still have problems, let me
know and I will take a detailed look into this.
I assume you are trying to test an ImplementationLoader for its handling of
duplicate components. If so, then you must simply have to focus on that
'unit' i.e. the ImplementationLoader. You need not test if the loader is
registered or picked up from the registry and invoked. These things you
assume and just start with the phase when the loader is actually invoked.
Through the mock reader you only need to feed those inputs that the Loader
actually reads from the XMLReader argument. Basically you provide mock
inputs just to the Loader and test how it handles these inputs. So in your
case you might be feeding in duplicates to see if the loader is able to
detect this.
- Venkat
On 1/23/07, Luciano Resende <lu...@gmail.com> wrote:
>
> I'm trying to use easyMock to create a unit testing to verify that
> exception
> is being thrown for duplicated Components...
> Looks like the reader that is simulating the scdl is returning the right
> values, but then, the registry does not find a loader to handle
> implementation.java
>
> Portion given by the reader so far :
>
> <composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
> name="composite_component-include2">
>
> <component name="component">
> <implementation.java class="
> org.apache.tuscany.core.mock.component.BasicInterfaceImpl"/>
>
> It's failing as it's not finding the proper loader to handle
> implementation.java... How do I tell get the registry properly loaded with
> the proper loaders registred ? Or any other way I should handle this ?
>
> Code:
>
> public void testLoadDuplicatedComponentTypeFromMock() throws
> Exception{
> /*
> //main
> <composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
> name="CompositeComponent">
> <include name="composite_component-include1"
> scdlLocation="composite_component-include1.scdl"/>
> <include name="composite_component-include2"
> scdlLocation="composite_component-include2.scdl"/>
> </composite>
> //include 1
> <composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
> name="composite_component-include2">
>
> <component name="component">
> <implementation.java class="
> org.apache.tuscany.core.mock.component.BasicInterfaceImpl"/>
> </component>
>
> <service name="service" >
> <interface.java interface="
> org.apache.tuscany.core.mock.component.BasicInterface"/>
> <reference>component</reference>
> </service>
> </composite>
> */
>
>
> EasyMock.expect(mockReader.getName()).andReturn(COMPOSITE);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.isA(String.class))).andReturn(COMPOSITE_NAME);
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.START_ELEMENT);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.isA(String.class))).andReturn(COMPOSITE_NAME);
>
> //first component
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.START_ELEMENT);
> EasyMock.expect(mockReader.getName()).andReturn(COMPONENT);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.isA(String.class))).andReturn(COMPONENT_NAME);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.eq("initLevel"))).andReturn(null);
> EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
> String.class), EasyMock.isA(String.class))).andReturn(null);
> EasyMock.expect(mockReader.nextTag()).andReturn(0);
> EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
> String.class), EasyMock.isA(String.class))).andReturn(null);
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.START_ELEMENT);
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.END_ELEMENT);
> EasyMock.expect(mockReader.getName()).andReturn(null
> /*COMPONENT_IMPLEMENTATION_JAVA*/);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.isA(String.class))).andReturn(null
> /*COMPONENT_IMPLEMENTATION_JAVA_CLASS*/);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.eq("initLevel"))).andReturn(null);
> EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
> String.class), EasyMock.isA(String.class))).andReturn(null);
> EasyMock.expect(mockReader.nextTag()).andReturn(0);
> EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
> String.class), EasyMock.isA(String.class))).andReturn(null);
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.END_ELEMENT);
> //second and duplicated component
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.START_ELEMENT);
> EasyMock.expect(mockReader.getName()).andReturn(COMPONENT);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.isA(String.class))).andReturn(COMPONENT_NAME);
> EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
> String.class), EasyMock.isA(String.class))).andReturn(null);
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.START_ELEMENT);
> EasyMock.expect(mockReader.getName
> ()).andReturn(COMPONENT_IMPLEMENTATION_JAVA);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.isA(String.class
> ))).andReturn(COMPONENT_IMPLEMENTATION_JAVA_CLASS);
> EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
> String.class), EasyMock.isA(String.class))).andReturn(null);
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.END_ELEMENT);
>
> //replay reader
> EasyMock.replay(mockReader);
>
> ModelObject modelObject = new ModelObject() {};
>
> CompositeComponentType composite = loader.load(
> EasyMock.createNiceMock(CompositeComponent.class),
> modelObject,
> mockReader,
> EasyMock.createNiceMock(DeploymentContext.class));
>
> assertEquals(COMPOSITE_NAME, composite.getName());
>
> }
>
> Exception:
>
> java.lang.NullPointerException
> at org.apache.tuscany.spi.loader.UnrecognizedElementException.<init>(
> UnrecognizedElementException.java:39)
> at org.apache.tuscany.core.loader.LoaderRegistryImpl.load(
> LoaderRegistryImpl.java:82)
> at org.apache.tuscany.core.loader.ComponentLoader.loadImplementation(
> ComponentLoader.java:199)
> at org.apache.tuscany.core.loader.ComponentLoader.load(
> ComponentLoader.java:110)
> at org.apache.tuscany.core.loader.ComponentLoader.load(
> ComponentLoader.java:79)
> at org.apache.tuscany.core.loader.LoaderRegistryImpl.load(
> LoaderRegistryImpl.java:84)
> at
> org.apache.tuscany.core.implementation.composite.CompositeLoader.load
> (CompositeLoader.java:96)
> at
> org.apache.tuscany.core.implementation.composite.CompositeLoader.load
> (CompositeLoader.java:68)
> at org.apache.tuscany.core.loader.LoaderRegistryImpl.load(
> LoaderRegistryImpl.java:84)
> at
> org.apache.tuscany.core.implementation.composite.CompositeLoader.load
> (CompositeLoader.java:96)
> at
>
> org.apache.tuscany.core.implementation.composite.CompositeLoaderTestCase.testLoadDuplicatedComponentTypeFromMock
> (CompositeLoaderTestCase.java:152)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(
> NativeMethodAccessorImpl.java:39)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(
> DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:585)
> at junit.framework.TestCase.runTest(TestCase.java:154)
> at junit.framework.TestCase.runBare(TestCase.java:127)
> at junit.framework.TestResult$1.protect(TestResult.java:106)
> at junit.framework.TestResult.runProtected(TestResult.java:124)
> at junit.framework.TestResult.run(TestResult.java:109)
> at junit.framework.TestCase.run(TestCase.java:118)
> at junit.framework.TestSuite.runTest(TestSuite.java:208)
> at junit.framework.TestSuite.run(TestSuite.java:203)
> at
> org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(
> JUnit3TestReference.java:128)
> at org.eclipse.jdt.internal.junit.runner.TestExecution.run(
> TestExecution.java:38)
> at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> RemoteTestRunner.java:460)
> at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> RemoteTestRunner.java:673)
> at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(
> RemoteTestRunner.java:386)
> at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
> RemoteTestRunner.java:196)
>
>
>
>
> --
> Luciano Resende
> http://people.apache.org/~lresende
>
>
Re: Help with unit testing using easyMock
Posted by Jim Marino <jm...@myromatours.com>.
Hi Luciano,
You shouldn't need to use the registry or other loaders to test this.
Just create the loader you want and mock out the XMLStreamReader and
LoaderRegistry to return the correct values. The mocked loader should
return component definitions that are newed up rather than created by
a loader.One way to look at this is that you are verifying the
behavior of the composite loader, not the registry and its
interaction with other loaders.
Some reading I've found useful is:
http://www.martinfowler.com/articles/mocksArentStubs.html
http://www.artima.com/intv/testdrivenP.html
http://www.martinfowler.com/articles/continuousIntegration.html
http://www.easymock.org/EasyMock2_2_Documentation.html
Jim
On Jan 23, 2007, at 12:44 AM, Luciano Resende wrote:
> I'm trying to use easyMock to create a unit testing to verify that
> exception
> is being thrown for duplicated Components...
> Looks like the reader that is simulating the scdl is returning the
> right
> values, but then, the registry does not find a loader to handle
> implementation.java
>
> Portion given by the reader so far :
>
> <composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
> name="composite_component-include2">
>
> <component name="component">
> <implementation.java class="
> org.apache.tuscany.core.mock.component.BasicInterfaceImpl"/>
>
> It's failing as it's not finding the proper loader to handle
> implementation.java... How do I tell get the registry properly
> loaded with
> the proper loaders registred ? Or any other way I should handle this ?
>
> Code:
>
> public void testLoadDuplicatedComponentTypeFromMock() throws
> Exception{
> /*
> //main
> <composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
> name="CompositeComponent">
> <include name="composite_component-include1"
> scdlLocation="composite_component-include1.scdl"/>
> <include name="composite_component-include2"
> scdlLocation="composite_component-include2.scdl"/>
> </composite>
> //include 1
> <composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
> name="composite_component-include2">
>
> <component name="component">
> <implementation.java class="
> org.apache.tuscany.core.mock.component.BasicInterfaceImpl"/>
> </component>
>
> <service name="service" >
> <interface.java interface="
> org.apache.tuscany.core.mock.component.BasicInterface"/>
> <reference>component</reference>
> </service>
> </composite>
> */
>
>
> EasyMock.expect(mockReader.getName()).andReturn(COMPOSITE);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.isA(String.class))).andReturn
> (COMPOSITE_NAME);
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.START_ELEMENT);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.isA(String.class))).andReturn
> (COMPOSITE_NAME);
>
> //first component
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.START_ELEMENT);
> EasyMock.expect(mockReader.getName()).andReturn(COMPONENT);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.isA(String.class))).andReturn
> (COMPONENT_NAME);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.eq("initLevel"))).andReturn(null);
> EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
> String.class), EasyMock.isA(String.class))).andReturn(null);
> EasyMock.expect(mockReader.nextTag()).andReturn(0);
> EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
> String.class), EasyMock.isA(String.class))).andReturn(null);
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.START_ELEMENT);
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.END_ELEMENT);
> EasyMock.expect(mockReader.getName()).andReturn(null
> /*COMPONENT_IMPLEMENTATION_JAVA*/);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.isA(String.class))).andReturn(null
> /*COMPONENT_IMPLEMENTATION_JAVA_CLASS*/);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.eq("initLevel"))).andReturn(null);
> EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
> String.class), EasyMock.isA(String.class))).andReturn(null);
> EasyMock.expect(mockReader.nextTag()).andReturn(0);
> EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
> String.class), EasyMock.isA(String.class))).andReturn(null);
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.END_ELEMENT);
> //second and duplicated component
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.START_ELEMENT);
> EasyMock.expect(mockReader.getName()).andReturn(COMPONENT);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.isA(String.class))).andReturn
> (COMPONENT_NAME);
> EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
> String.class), EasyMock.isA(String.class))).andReturn(null);
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.START_ELEMENT);
> EasyMock.expect(mockReader.getName
> ()).andReturn(COMPONENT_IMPLEMENTATION_JAVA);
> EasyMock.expect(mockReader.getAttributeValue((String)
> EasyMock.isNull(), EasyMock.isA(String.class
> ))).andReturn(COMPONENT_IMPLEMENTATION_JAVA_CLASS);
> EasyMock.expect(mockReader.getAttributeValue(EasyMock.isA(
> String.class), EasyMock.isA(String.class))).andReturn(null);
> EasyMock.expect(mockReader.next()).andReturn(
> XMLStreamConstants.END_ELEMENT);
>
> //replay reader
> EasyMock.replay(mockReader);
>
> ModelObject modelObject = new ModelObject() {};
>
> CompositeComponentType composite = loader.load(
> EasyMock.createNiceMock(CompositeComponent.class),
> modelObject,
> mockReader,
> EasyMock.createNiceMock(DeploymentContext.class));
>
> assertEquals(COMPOSITE_NAME, composite.getName());
>
> }
>
> Exception:
>
> java.lang.NullPointerException
> at
> org.apache.tuscany.spi.loader.UnrecognizedElementException.<init>(
> UnrecognizedElementException.java:39)
> at org.apache.tuscany.core.loader.LoaderRegistryImpl.load(
> LoaderRegistryImpl.java:82)
> at
> org.apache.tuscany.core.loader.ComponentLoader.loadImplementation(
> ComponentLoader.java:199)
> at org.apache.tuscany.core.loader.ComponentLoader.load(
> ComponentLoader.java:110)
> at org.apache.tuscany.core.loader.ComponentLoader.load(
> ComponentLoader.java:79)
> at org.apache.tuscany.core.loader.LoaderRegistryImpl.load(
> LoaderRegistryImpl.java:84)
> at
> org.apache.tuscany.core.implementation.composite.CompositeLoader.load
> (CompositeLoader.java:96)
> at
> org.apache.tuscany.core.implementation.composite.CompositeLoader.load
> (CompositeLoader.java:68)
> at org.apache.tuscany.core.loader.LoaderRegistryImpl.load(
> LoaderRegistryImpl.java:84)
> at
> org.apache.tuscany.core.implementation.composite.CompositeLoader.load
> (CompositeLoader.java:96)
> at
> org.apache.tuscany.core.implementation.composite.CompositeLoaderTestCa
> se.testLoadDuplicatedComponentTypeFromMock
> (CompositeLoaderTestCase.java:152)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(
> NativeMethodAccessorImpl.java:39)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(
> DelegatingMethodAccessorImpl.java:25)
> at java.lang.reflect.Method.invoke(Method.java:585)
> at junit.framework.TestCase.runTest(TestCase.java:154)
> at junit.framework.TestCase.runBare(TestCase.java:127)
> at junit.framework.TestResult$1.protect(TestResult.java:106)
> at junit.framework.TestResult.runProtected(TestResult.java:124)
> at junit.framework.TestResult.run(TestResult.java:109)
> at junit.framework.TestCase.run(TestCase.java:118)
> at junit.framework.TestSuite.runTest(TestSuite.java:208)
> at junit.framework.TestSuite.run(TestSuite.java:203)
> at
> org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(
> JUnit3TestReference.java:128)
> at org.eclipse.jdt.internal.junit.runner.TestExecution.run(
> TestExecution.java:38)
> at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> RemoteTestRunner.java:460)
> at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(
> RemoteTestRunner.java:673)
> at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(
> RemoteTestRunner.java:386)
> at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(
> RemoteTestRunner.java:196)
>
>
>
>
> --
> Luciano Resende
> http://people.apache.org/~lresende
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-dev-help@ws.apache.org