无法自动装配webApplicationContext Spring MVC Test

时间:2014-09-02 21:59:27

标签: java spring mockito spring-mvc-test

我有一个使用Spring Framework 3.2.4创建的Rest API。我试图在WebApplicationContext中编写测试用例。我从服务器日志中看到xml配置文件已加载但在执行TestContextManager时失败。

ROR org.springframework.test.context.TestContextManager- Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4c980278] to prepare test instance

这是我的测试类:

@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class })
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
   @ContextConfiguration(locations = {"file:WebContent/WEB-INF/applicationContext.xml","file:WebContent/WEB-INF/spring-security.xml","file:WebContent/WEB-INF/spring-servlet.xml"})


public class AppControllerTest {

private MockMvc mockMvc;

@Autowired
private WebApplicationContext webApplicationContext;


@Before
public void setUp() {
    //Mockito.reset();//awesome_service
    mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}


@Test
public void testGetSignupForm() throws Exception {
    this.mockMvc.perform(get("/apps"))
            .andExpect(status().isOk());
}

}

这是我的控制器

@Controller
@RequestMapping(value = "/apps")

public class AppController
{
@Resource(name = "appAPIService")
private AppAPIService appAPIService;

@Resource(name = "applicationVersionService")
private ApplicationVersionService applicationVersionService;

@Resource(name = "applicationService")
private ApplicationService applicationService;

@Autowired
Validator validator;

private static final Logger LOGGER = LoggerFactory.getLogger(AppController.class);

@InitBinder
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    binder.registerCustomEditor(Model.class, "model", new PropertyEditorSupport() {
        @Override
        public void setAsText(String text) {
            BasicDBObject obj = (BasicDBObject)JSON.parse(text);

            Model model = new Model();
            model.setPrice(obj.getInt(ApplicationFields.MODEL_PRICE,0));
            model.setTrial(obj.getInt(ApplicationFields.MODEL_TRIAL,0));
            model.setType(obj.getString(ApplicationFields.MODEL_TYPE));

            setValue(model);
        }
    });
}

@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<String> find(HttpServletRequest request, @RequestParam(value="query", required=false) String queryString, @RequestParam(value="sort", required=false) String sortString, @RequestParam(value="pageNumber", required=false) Integer pageNumber, @RequestParam(value="limit", required=false) Integer limit, @RequestParam(value="userId", required=false) String hostUserId)
{
    ObjectId marketplaceId = null;
    try
    {   
        marketplaceId = AuthenticationUtils.getUserId();

        BasicDBObject query;
        try
        {
            query = FormatUtils.toJsonObject(queryString);
        }
        catch(Exception e)
        {
            return ResponseUtils.badRequest("query", "Invalid query: " + queryString);
        }

        BasicDBObject sort;
        try
        {
            sort = FormatUtils.toJsonObject(sortString);
        }
        catch(Exception e)
        {
            return ResponseUtils.badRequest("sort", "Invalid sort: " + sortString);
        }

        return appAPIService.find(marketplaceId, query, sort, pageNumber, limit, hostUserId);
    }
    catch (Exception e)
    {
        String message = "Unable to find apps";
        ToStringBuilder builder = new ToStringBuilder(message);
        LoggingUtils.addToBuilder(builder, request);
        builder.append("marketplaceId", AuthenticationUtils.getUserId());

        LOGGER.error(builder.toString(), e);            
        return ResponseUtils.internalError();           
    }
}

...

}

0 个答案:

没有答案