سؤال

I'm trying to use a custom annotation in order to set up my testing environment for a Spring MVC app that I'm implementing some Spring AOP Aspects into. The setup involves a few Spring Annotations, so I'm trying to condense them down into one. I'm able to get the annotation to show that it's not throwing an error on compile, but when I try to use it in my test cases, it doesn't have any effect.

Here's the annotation itself.

package com.oreillyauto.pricingweb.testingUtilities;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@ActiveProfiles("integration")
@ContextConfiguration(locations = {
    "file:src/main/webapp/WEB-INF/config/application-context.xml",
    "file:src/main/webapp/WEB-INF/config/AOP-context-config.xml"
})
public @interface AcitvateTestProfile {

}

Basically just trying to combine the @ActivateProfiles with @ContextConfiguration. It throws no errors, at least in Eclipse.

Here's my test case, which was written to test the start of an Spring AOP implementation I'm working on. The test is written in Spock, which is a version of Groovy written particularly for testing (for those who don't know). It uses JUNIT, and is really pretty similar...

package com.abc.efg.aspects

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.transaction.annotation.Transactional

import spock.lang.Specification

import com.abc.efg.controller.UserController
import com.abc.efg.testingUtilities.AcitvateTestProfile

@AcitvateTestProfile
@Transactional("datawhseTransactionManager")
class ControllerAspectsSpec extends Specification {

    @Autowired
    UserController userController

    def"placeHolder"(){

        given:
        def returner = userController.getListOfStoresThisUserCanUse(9999)

        expect:
        println("Did it show up?" + returner.toString() )

    }

}

I should note, if I put the @ActivateProfiles and @ContextConfiguration at the top of the test case, the test case will work just fine.

As it sits now, when I run the test case I'm given this: java.lang.NullPointerException: Cannot invoke method getListOfStoresThisUserCanUse() on null object. Which makes me think that the test isn't using the application-context.xml, thus it can't find the beans it needs to inject them into my test case.

I've done a google search and found a few pieces I was missing there (@Retention and @Target), but even though everything seems to be written correctly, I can't get it to work.

And yes, this is all that I've done. I was told that I wouldn't need to do any modifications to the web.xml or application-context.xml files we have in our app, so... Hoping that's correct. I'd rather stay away from writing XML beans because it's inconsistent with our general design pattern. Buuuuut, if I have to I have to. So if anyone can speak to that question, that would also be nice.

هل كانت مفيدة؟

المحلول

I am not sure that what you are trying to do (creating a meta-annotation) works well in Spring versions prior to Spring 4. In any case, you could do the following (that works in older Spring versions too):

@ActiveProfiles("integration")
@ContextConfiguration(locations = {
    "file:src/main/webapp/WEB-INF/config/application-context.xml",
    "file:src/main/webapp/WEB-INF/config/AOP-context-config.xml"
})
public abstract class AbstractSpringTest extends Specification {

}



@Transactional("datawhseTransactionManager")
public class ControllerAspectsSpec extends AbstractSpringTest {

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top