Posts

Showing posts with the label spring

JUnit parameterized test with Spring autowiring AND transactions

I've been writing JUnit Parameterized tests (a nice intro here - also see Theories ) to do some data driven API testing. Now, when introducing Spring into the mix there are a couple of extra things to do . I came unstuck though because I was trying to do Transactional tests - since I'm now using @RunWith(Parameterized.class) and setting up my Spring TestContextManager manually the @Transaction annotations caused an exception: java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given I couldn't find any built in solution, so I've gone with manual transaction management in my test, using doInTransaction: @RunWith(Parameterized.class) @ContextConfiguration(locations = "classpath*:/testContext.xml") public class MyTest { @Autowired PlatformTransactionManager transactionManager; private TestContextManager testContextManager; public MyTest (... parameters for test) { // store parameters in instance v...

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...