source: vlo/branches/vlo-3.0/vlo-web-app/src/test/java/eu/clarin/cmdi/vlo/wicket/pages/TestFacetedSearchPage.java @ 4631

Last change on this file since 4631 was 4631, checked in by Twan Goosen, 10 years ago

fixed configuration n page tests

File size: 4.1 KB
Line 
1package eu.clarin.cmdi.vlo.wicket.pages;
2
3import eu.clarin.cmdi.vlo.VloWicketApplication;
4import eu.clarin.cmdi.vlo.config.DefaultVloConfigFactory;
5import eu.clarin.cmdi.vlo.config.VloConfigFactory;
6import eu.clarin.cmdi.vlo.config.VloSpringConfig;
7import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
8import eu.clarin.cmdi.vlo.service.FacetFieldsService;
9import eu.clarin.cmdi.vlo.service.SolrDocumentService;
10import java.util.Arrays;
11import org.apache.solr.client.solrj.response.FacetField;
12import org.apache.solr.common.SolrDocument;
13import org.apache.wicket.util.tester.WicketTester;
14import org.jmock.Expectations;
15import org.jmock.Mockery;
16import org.jmock.integration.junit4.JUnit4Mockery;
17import org.junit.Before;
18import org.junit.Test;
19import org.junit.runner.RunWith;
20import org.springframework.beans.factory.annotation.Autowired;
21import org.springframework.context.annotation.Bean;
22import org.springframework.context.annotation.ComponentScan;
23import org.springframework.context.annotation.Configuration;
24import org.springframework.test.annotation.DirtiesContext;
25import org.springframework.test.context.ContextConfiguration;
26import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27import org.springframework.test.context.support.AnnotationConfigContextLoader;
28
29/**
30 * Abstract base class for tests that require dependency injection of (mock)
31 * objects and services. Based on blog post by Petri Kainulainen found at
32 * {@link http://www.petrikainulainen.net/programming/tips-and-tricks/mocking-spring-beans-with-apache-wicket-and-mockito/}
33 *
34 */
35@RunWith(SpringJUnit4ClassRunner.class)
36@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
37//@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) // gives us a fresh context for each test
38public class TestFacetedSearchPage {
39
40    @Configuration
41    static class ContextConfiguration extends VloSpringConfig {
42
43        @Bean
44        public Mockery mockery() {
45            return new JUnit4Mockery();
46        }
47
48        @Override
49        public FacetFieldsService facetFieldsService() {
50            return mockery().mock(FacetFieldsService.class, "facetFieldsService");
51        }
52
53        @Override
54        public SolrDocumentService documentService() {
55            return mockery().mock(SolrDocumentService.class);
56        }
57
58        @Override
59        public VloConfigFactory vloConfigFactory() {
60            //TODO: Separate test config? -> override vloConfig() instead
61            return new DefaultVloConfigFactory();
62        }
63
64    }
65
66    private WicketTester tester;
67    @Autowired(required = true)
68    private VloWicketApplication application;
69    @Autowired(required = true)
70    private Mockery mockery;
71
72    @Autowired(required = true)
73    private FacetFieldsService facetFieldsService;
74
75    @Autowired(required = true)
76    private SolrDocumentService documentService;
77
78    @Before
79    public void setUp() {
80        tester = new WicketTester(application);
81    }
82
83    @Test
84    public void homepageRendersSuccessfully() {
85        // mock behaviour of facet fields service
86        mockery.checking(new Expectations() {
87            {
88                // mock facets
89                atLeast(1).of(facetFieldsService).getFacetFieldCount();
90                will(returnValue(2L));
91                atLeast(1).of(facetFieldsService).getFacetFields(with(any(QueryFacetsSelection.class)));
92                will(returnValue(Arrays.asList(new FacetField("collection"), new FacetField("language"), new FacetField("resource class"))));
93
94                // mock search results
95                atLeast(1).of(documentService).getDocumentCount(with(any(QueryFacetsSelection.class)));
96                will(returnValue(1000L));
97                oneOf(documentService).getDocuments(with(any(QueryFacetsSelection.class)), with(equal(0)), with(equal(10)));
98                will(returnValue(Arrays.asList(new SolrDocument(), new SolrDocument())));
99            }
100        });
101
102        //start and render the test page
103        tester.startPage(FacetedSearchPage.class);
104
105        //assert rendered page class
106        tester.assertRenderedPage(FacetedSearchPage.class);
107    }
108}
Note: See TracBrowser for help on using the repository browser.