为什么我在Eclipse中使用Cover As而不是使用Junit时收到错误?

时间:2012-07-26 09:54:30

标签: eclipse junit cobertura

我将Eclipse与eCobertura一起使用 我有一个带Controller的小项目(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我收到错误。

错误:

enter image description here

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

enter image description here

我不明白错误。

非常感谢

1 个答案:

答案 0 :(得分:1)

我有

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

在我的applicationContext.xml中,它现在运行正常

非常感谢