wpgsh / cloud4water

Apache License 2.0
0 stars 0 forks source link

If we can simplify the unit test #11

Closed asd1245dss closed 8 years ago

asd1245dss commented 8 years ago

image image

@alexlukichev I've read the sample servlet unit test,what you do there is set up one jetty server and resolve all the dependencies AccessTokenServlet class need and send the http request through the HttpURLConnection. My question is:

  1. I write a new unit test for the AuthorizationServlet class,but I just use my local karaf environment for the test server,so I don't need to define the server and any other dependencies.It maybe quite easier for us to build servlet unit test.It's ok for the unit test specification?
  2. When I write the AuthenticationServiceImplTest,I need to inject its dependencies like UserDao and so on.You just resolve it by yourself.If there is any other methods to resolve it through the blueprint,so that we can just use the @Reference annotation to make it.
alexlukichev commented 8 years ago

One possible way is to use something like described here: https://davidvaleri.wordpress.com/2013/09/17/integration-testing-osgi-bundles-in-apache-karafservicemix-with-pax-exam-3/

(note, you may need to upgrade the versions of the dependencies)

I have not tried that solution myself, but it seems workable.

asd1245dss commented 8 years ago

@alexlukichev I tried it,but failed because of missing some dependencies. So I go to the OPS4j Pax Exam offcial website, I found the following documentation for osgi unit test. Getting+Started+with+OSGi+Tests Test+Containers image image

I followed the tutorial image But it seems still missing some configuration. maven.txt

asd1245dss commented 8 years ago

@alexlukichev I found a unit test demo from the official karaf github repository. image image Just the same as the before what I tried.And I clone one demo from this repository which matain by one of the contributor of karaf.At last,it works through maven test or junit test. image image I try to copy the approch to our project,but still failed.What I find different betweeen these two projects is that the official demo uses the feature install not the kar file. image So I refactor the project's structure and using the feature file instead kar file to install our project,but the karaf says missing some dependencies. image image image Although it still can't work,I think if the feature install take effect,the unit test problems should be solved.

alexlukichev commented 8 years ago

Can you please create a branch with your changes?

On Aug 18, 2016 7:24 PM, "Summer" notifications@github.com wrote:

@alexlukichev https://github.com/alexlukichev I found a unit test demo from the official karaf github repository. [image: image] https://cloud.githubusercontent.com/assets/2149974/17794611/9356643a-65e3-11e6-83ca-353bc5f83330.png [image: image] https://cloud.githubusercontent.com/assets/2149974/17794603/82490b20-65e3-11e6-9f62-8c2adf0ef272.png Just the same as the before what I tried.And I clone one demo from this repository which matain by one of the contributor of karaf.At last,it works through maven test or junit test. [image: image] https://cloud.githubusercontent.com/assets/2149974/17794671/26798292-65e4-11e6-9ae9-8b830a61c4c6.png [image: image] https://cloud.githubusercontent.com/assets/2149974/17794718/85949cd0-65e4-11e6-8942-f564c3bf201e.png I try to copy the approch to our project,but still failed.What I find different betweeen these two projects is that the official demo uses the feature install not the kar file. [image: image] https://cloud.githubusercontent.com/assets/2149974/17794765/f9a46484-65e4-11e6-8fe9-53c7dbc5b5a5.png So I refactor the project's structure and using the feature file instead kar file to install our project,but the karaf says missing some dependencies. [image: image] https://cloud.githubusercontent.com/assets/2149974/17794887/f5444818-65e5-11e6-91d0-e7e177c4ddc2.png [image: image] https://cloud.githubusercontent.com/assets/2149974/17794885/ee2890ac-65e5-11e6-874d-3bc9d8cbcf19.png [image: image] https://cloud.githubusercontent.com/assets/2149974/17794881/e49926e6-65e5-11e6-818e-455a170efe2b.png Although it still can't work,I think if the feature install take effect,the unit test problems should be solved.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/wpgsh/cloud4water/issues/11#issuecomment-240896049, or mute the thread https://github.com/notifications/unsubscribe-auth/AFCdLTtCKewX9B8L22VVKnbSyRQNVLyTks5qhPfbgaJpZM4Jisf3 .

asd1245dss commented 8 years ago

image Yes,I have create the branch before.

alexlukichev commented 8 years ago

BTW, have you considered mockito for mock objects?

(also it is not worth trying Sling OSGi Mock -- doesn't work that well)

asd1245dss commented 8 years ago

Yes! We want to use it for our recent project test, but not very familiar with it.

alexlukichev commented 8 years ago

I have submitted an example of mockito usage in the branch mock-sample.

Take a look at apps/authn/bundles/net.wapwag.authn.ui/src/test. The sample test class is AccessTokenServletTest. I've factored out some common code into BaseServletTest and UserDaoMock. The latter contains the actual use of mockito tools.

asd1245dss commented 8 years ago

image In my AuthorizationServletTest class,it need user to login first before get authorization code,so how to set the login session in the jetty container or if I need to add loginservlet to jetty and mock related dao method ?