Some problem while writing tests.

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Some problem while writing tests.

Sarthak Gupta
Hello,
I have a component implementation GlossaryTransformation.java
<https://github.com/xwiki-contrib/application-glossary/blob/master/application-glossary-api/src/main/java/org/xwiki/contrib/glossary/internal/GlossaryTransformation.java>.
So it has two methods List<String> getGlossaryEntries() and transform(Block
block, TransformationContext context) in it.
So, I
​ ​
have a test file https://pastebin.com/NixuBMjD for this component. Here I
am trying to mock the behavior of
getGlossaryEntries

method
by putting strings ("glossaryEntry1" and "glossaryEntry2"). And then
implementing "when(this.glossaryTransformation.getGlossaryEntries()).thenReturn(mockArrayList);"
but this line somehow shows NullPointerException.

On searching on StackOverflow, I found that this is happening because the
object of "glossaryTransfromation" is not created, but only a reference is
created. So, I tried to put glosaaryTransformation =
"mock(GlossaryTransformation.class)" in the code. Yes, the above error was
resolved, but the line "Parser parser =
this.mocker.getInstance(Parser.class, "xwiki/2.1");" gave an error "
ComponentLookupException: Can't find descriptor for the component [role =
[interface org.xwiki.rendering.parser.Parser] hint = [xwiki/2.1]]"
.

Is there a better way to achieve this? Or I think a better approach can be
to put the Query method (getGlossaryEntries) of GlossarryTranformation.java
in a separate class and then create a mock object for that class and then
run the test?

Thanks
-Sarthak Gupta
Reply | Threaded
Open this post in threaded view
|

Re: Some problem while writing tests.

vmassol
Administrator
Hi Sarthak,

> On 29 Oct 2017, at 18:48, Sarthak Gupta <[hidden email]> wrote:
>
> Hello,
> I have a component implementation GlossaryTransformation.java
> <https://github.com/xwiki-contrib/application-glossary/blob/master/application-glossary-api/src/main/java/org/xwiki/contrib/glossary/internal/GlossaryTransformation.java>.
> So it has two methods List<String> getGlossaryEntries() and transform(Block
> block, TransformationContext context) in it.

I’ve added some review of the code in GitHub.

> So, I
> ​ ​
> have a test file https://pastebin.com/NixuBMjD for this component. Here I
> am trying to mock the behavior of
> getGlossaryEntries
> ​
> method
> by putting strings ("glossaryEntry1" and "glossaryEntry2"). And then
> implementing "when(this.glossaryTransformation.getGlossaryEntries()).thenReturn(mockArrayList);"
> but this line somehow shows NullPointerException.
>
> On searching on StackOverflow, I found that this is happening because the
> object of "glossaryTransfromation" is not created, but only a reference is
> created. So, I tried to put glosaaryTransformation =
> "mock(GlossaryTransformation.class)" in the code. Yes, the above error was
> resolved, but the line "Parser parser =
> this.mocker.getInstance(Parser.class, "xwiki/2.1");" gave an error "
> ComponentLookupException: Can't find descriptor for the component [role =
> [interface org.xwiki.rendering.parser.Parser] hint = [xwiki/2.1]]"
> .
>
> Is there a better way to achieve this? Or I think a better approach can be
> to put the Query method (getGlossaryEntries) of GlossarryTranformation.java
> in a separate class and then create a mock object for that class and then
> run the test?

Could you commit your test file and I can have a look? It’s hard to send review comments on pastebin :)

I know I told you to only commit working stuff but here that’s the simplest to help you out.

Note: You should not mock getGlossaryEntries(). Instead you should mock the QueryManager result.

Thanks
-Vincent

>
> Thanks
> -Sarthak Gupta