1

我使用 Eclipse 和 eCobertura 我有一个带有控制器(SpringMVC)的小项目。我创建了一个测试(JUnit)。

当我从 JUnit(在 Eclipse IDE 中)运行测试时,一切正常,但是当我运行命令(从菜单)时,我收到一个错误

我的控制器:

package ec.europa.eu.nwi.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author LEBRUJA
 */
@Controller
public class AvailibilityController {
/**
 * @param request 
 * @return mav
 */
@RequestMapping(value = "/available")
public final ModelAndView available(final HttpServletRequest request) {
    final ModelAndView mav = new ModelAndView("available", "sample", 
            new String("availability on 0.0.1"));
    return mav;
}
}

我的测试:

package ec.europa.eu.nwi.web.controller.test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.ModelAndViewAssert;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import ec.europa.eu.nwi.web.controller.AvailibilityController;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-servlet.xml"})
public final class AvalibilityControllerTest {

private transient MockHttpServletRequest request;
private transient MockHttpServletResponse response;

@Autowired
private RequestMappingHandlerAdapter handlerAdapter;

@Autowired
private RequestMappingHandlerMapping handlerMapping;


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

@Before
public void setUp() throws Exception {              
    request = new MockHttpServletRequest();
    response = new MockHttpServletResponse();
}

@After
public void tearDown() throws Exception {
    LOGGER.debug("TearDown");
}

@Test
public void testAvailable() {
    LOGGER.debug("Start testAvailable1");
    LOGGER.debug("Test only availibility of the apps");
    final AvailibilityController avc = new AvailibilityController();
    final Object mav = avc.available(request);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(mav instanceof ModelAndView);
    ModelAndViewAssert.assertAndReturnModelAttributeOfType((ModelAndView)mav, "sample", String.class);
    ModelAndViewAssert.assertModelAttributeAvailable((ModelAndView)mav, "sample");
    ModelAndViewAssert.assertModelAttributeValue((ModelAndView)mav, "sample", "availability on 0.0.1");
    ModelAndViewAssert.assertViewName((ModelAndView)mav, "available");        
    final BindingResult result = mock(BindingResult.class);
    when(result.hasErrors()).thenReturn(true);        
    LOGGER.debug("End testAvailable1");
}

@Test
public void testAvailable1() throws Exception {
    LOGGER.debug("Start testAvailable");
    LOGGER.debug("Test only availibility of the apps");
    request.setMethod("GET");
    request.setRequestURI("/available.html"); 

    Object handler = handlerMapping.getHandler(request).getHandler();
    LOGGER.debug("Get the Model and View");
    ModelAndView modelAndView = handlerAdapter.handle(request, response,handler);        
    Assert.assertEquals("availability on 0.0.1", modelAndView.getModel().get("sample"));
    Assert.assertTrue(modelAndView.getModel().containsKey("sample"));
    LOGGER.debug("End testAvailable");
}
}

如果我使用 JUnit (Run As Junit) 运行,一切正常,但是当我运行 Cover As .. JUnit 时,我收到错误消息。

错误 :

在此处输入图像描述

我过滤了这个类(来自 Eclipse Coverage Configuration 中的排除配置)。如果我删除了过滤器,junit 代码标记为红色

在此处输入图像描述

我不明白这个错误。

非常感谢

4

1 回答 1

1

我有

<aop:aspectj-autoproxy proxy-target-class="true" />

在我的 applicationContext.xml 中,它现在运行良好

非常感谢

于 2012-07-26T11:31:52.487 回答