Bonjour à tous,
je ne sais pas très bien si je suis dans le bon forum mais comme ça concerne Spring MVC, je tente ma chance.

Je suis en train de mettre en place un projet avec Spring MVC et je veux réaliser les tests avec JUnit et voir si la couverture de tests est ok avec Cobertura.

Pour ce faire, j'ai mis en place mon environnement Eclipse + eCobertura sur mon poste mais j'ai également une mise en place avec Maven et Jenkins.

Le problème se trouve dans la couverture de tests. Cobertura m'indique que le test de la méthode du contrôleur n'est pas couvert mais je ne vois pas comment mieux le couvrir.

Voici mon contrôleur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
 
@Controller
public class AvailibilityController {
	@RequestMapping(value="/available")	
    public ModelAndView available(HttpServletRequest request) {	
		ModelAndView mav = new ModelAndView("available", "sample", new String("availability on 0.0.1")); 
		return mav;
	}	
}
Vraiment très simple. Quand je lance Jetty pas de problème, la page "available.html" s'affiche.

J'ai créé une classe de test unitaire et j'ai essayé pas mal tests, dont certains avec Mockito :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-servlet.xml"})
public final class AvalibilityControllerTest {
 
	@Autowired
    private RequestMappingHandlerAdapter handlerAdapter;
 
    @Autowired
    private RequestMappingHandlerMapping handlerMapping;
 
    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
 
    private static final Logger logger = LoggerFactory.getLogger(AvalibilityControllerTest.class);
 
	@Before
	public void setUp() throws Exception {		
		MockitoAnnotations.initMocks(this);
		request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
	}
 
	@After
	public void tearDown() throws Exception {
	}
 
	@Test
	public void testAvailable1() throws Exception {		
		logger.debug("Start testAvailable2");
		logger.debug("Test only availibility of the apps");
		request.setMethod("GET");
        request.setRequestURI("/available.html"); 
        final Object handler = handlerMapping.getHandler(request).getHandler();
        logger.debug("Get the Model and View");
        final ModelAndView mav = (ModelAndView) handlerAdapter.handle(request, response,handler);        
        final Map<String, Object> model = mav.getModel();        
        Assert.assertEquals(model.get("sample"),"availability on 0.0.1");
        Assert.assertTrue(model.containsKey("sample"));
        Assert.assertEquals("available", mav.getViewName());
        Assert.assertTrue(mav instanceof ModelAndView);
        final BindingResult result = mock(BindingResult.class);
        when(result.hasErrors()).thenReturn(true);
        logger.debug("End testAvailable2");
	}
 
	@Test
	public void testAvailable2() throws Exception {		
		logger.debug("Start testAvailable2");
		logger.debug("Test only availibility of the apps");
		request.setMethod("GET");
        request.setRequestURI("/available.html"); 
        final Object handler = handlerMapping.getHandler(request).getHandler();
        logger.debug("Get the Model and View");
        final ModelAndView mav = (ModelAndView) handlerAdapter.handle(request, response,handler);
        Assert.assertEquals(200, response.getStatus());
        ModelAndViewAssert.assertAndReturnModelAttributeOfType(mav, "sample", String.class);
        ModelAndViewAssert.assertModelAttributeAvailable(mav, "sample");
        ModelAndViewAssert.assertModelAttributeValue(mav, "sample", "availability on 0.0.1");
        ModelAndViewAssert.assertViewName(mav, "available");        
        logger.debug("End testAvailable");
	}
	public void testAvailable3() throws Exception {
		AvailibilityController c = new AvailibilityController();
		final Object mav = c.available(request);
		Assert.assertTrue(mav instanceof ModelAndView);
	}
}
Si je lance les tests tout est ok.

Par contre, lorsque je fait un "Cover As" > Junit dans Eclipse, j'obtiens du rouge sur le contrôleur (indiquant que le return mav n'est pas couvert :



Je ne vois pas comment faire mieux pour couvrir entièrement ce test.

Quelqu'un pourrait m'aider ?

Merci