Posts

Showing posts with the label testing

Integration testing Griffon services

I wanted to create an integration test for some Griffon services - where I wanted to set up some data first, then check that the service produced the appropriate results. Therefore, I wanted the database working normally, but I needed access to a configured Griffon application and the required services. My issue was how to get hold of the services? They don't automatically get injected as I first expected. But luckily, StackOverflow and Andres to the rescue:

When tests become hard

A friend contacted me for advice. He was having trouble writing a test for some code. I asked him to send me the code and the test so I could see what was going on. I encounter this problem quite frequently, so I thought I'd post one of my responses... Okay, here is where I think you are going wrong, and when writing tests becomes hard, it’s usually because of this. It’s important to layer your applications. Web/Services/DB – at least conceptually. What you have is a class that does everything. Web and Service. No-one can reuse your file processing logic without using the web, not even tests! A nice way to code would be to: Write a UNIT test that attempts to process a file. It passes in a file X and asserts that the result is Y. It won’t compile because you haven’t written the file processing code yet! Now you write the service which processes the file The test will compile and run now, but probably fail because you forgot something. Now you can fix your service And ...

Week in Review - 2010-28

I finally got a new battery for my laptop - it only took 3 weeks rather than the estimated 6, so finally I'm back online - on the train. It's barely doing the job though, a new battery barely gets me to and from work - I obviously need to work closer to home! I'm running Ubuntu , Netbeans , Grails , and sometimes a 3G internet connection - so I guess it is working hard for what is sometimes more than a 2 hour round trip! It will be interesting to see how the new battery lasts. I'm also thinking about getting a new laptop at the end of the year - my current laptop is over 2 years old, so technology wise it's out of date. The DELL Adamo looks interesting, with a SSD disk - I'd love to see how much difference that makes, otherwise I'd probably get an Inspiron 13z - I'm mainly interested in the smaller lighter models since I'm so mobile. Its nice to see laptops coming without DVD drives now - who needs them? I found an interesting whitepaper on Agile...

No Scope registered for scope request

Recently I was working with some code which defined beans as request scope. Junit tests failed with the following exception while trying to initialize the application context: Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: ...; nested exception is java.lang.IllegalStateException: No Scope registered for scope 'request' I found the solution by searching around (unfortunately I didn't record the sources). Basically, you can register the required scopes as shown below: [sourcecode language='java'] import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestScope; import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.SessionScope; public class SampleTest extends AbstractDependencyInjectionSpringContextTests { @Override protected v...

Regression testing grails plugins

For regression testing the grails plugins I've released, I have a shell script that: cleans up any previous runs packages the current code for the plugin OR downloads a release from my web site creates a new grails app copies preprepared resources into this new grails app installs the plugin Now all I have to do is run the application and test it through the browser (potential for selenium to help here?). This is great for testing the plugin against a new grails version, or just for testing code changes to the plugin itself. I put the script in <project_home>/test/regression/run.sh and the resources in <project_home>/test/regression/resources. The resources directory contains files like domain objects etc in the same directory layout as a normal grails project. This way I just need to copy the contents of the resources directory over the top of my newly created project. I should have written it as an ANT script though (what was I thinking?) - then it would be platf...