Unit testing Spring MVC

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Unit testing Spring MVC

charlei1968
Hi!

Unit testing throws an Expception:

Dandelion-core: 1.1.0
Dandelion Datatables: 1.0.1
Spring MVC framework: 4.2.0

Here my Test

package at.pansoma.panaccount;

import at.pansoma.panaccount.config.HibernateBeansConfig;
import at.pansoma.panaccount.config.HibernateConfig;
import at.pansoma.panaccount.config.WebMvcConfig;
import org.apache.shiro.subject.Subject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;

import javax.inject.Inject;

import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.expect;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("test")
@WebAppConfiguration
@ContextConfiguration(classes = {
        HibernateBeansConfig.class,
        HibernateConfig.class,
        WebMvcConfig.class
})
public class GlobalControllerTest extends AbstractShiroTest {
    @Inject
    protected WebApplicationContext webApplicationContext;

    protected MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        this.mockMvc = webAppContextSetup(this.webApplicationContext).build();
    }

    @Test
    public void testShowScreenLogin() throws Exception {
        mockMvc.perform(get("/"))
                .andExpect(status().isOk())
                .andExpect(view().name("login"));
    }
}

And that's the beginning of the Stack Trace (actuelle this is another Test, but the same Error appears, whenever there is an ddl:bundles-include attribute in an html template.)

2015-08-19 12:54:02 INFO  MockServletContext:443 - Initializing Spring FrameworkServlet ''
2015-08-19 12:54:02 INFO  TestDispatcherServlet:488 - FrameworkServlet '': initialization started
2015-08-19 12:54:02 INFO  TestDispatcherServlet:507 - FrameworkServlet '': initialization completed in 12 ms
[main] INFO org.thymeleaf.TemplateEngine - [THYMELEAF] INITIALIZING TEMPLATE ENGINE
[main] INFO org.thymeleaf.templateresolver.AbstractTemplateResolver - [THYMELEAF] INITIALIZING TEMPLATE RESOLVER: org.thymeleaf.templateresolver.ServletContextTemplateResolver
[main] INFO org.thymeleaf.templateresolver.AbstractTemplateResolver - [THYMELEAF] TEMPLATE RESOLVER INITIALIZED OK
[main] INFO org.thymeleaf.messageresolver.AbstractMessageResolver - [THYMELEAF] INITIALIZING MESSAGE RESOLVER: org.thymeleaf.spring4.messageresolver.SpringMessageResolver
[main] INFO org.thymeleaf.messageresolver.AbstractMessageResolver - [THYMELEAF] MESSAGE RESOLVER INITIALIZED OK
[main] INFO org.thymeleaf.TemplateEngine.CONFIG - [THYMELEAF] TEMPLATE ENGINE CONFIGURATION:
[THYMELEAF] * Cache Factory implementation: org.thymeleaf.cache.StandardCacheManager
[THYMELEAF] * Template modes:
[THYMELEAF]     * LEGACYHTML5
[THYMELEAF]     * XHTML
[THYMELEAF]     * XML
[THYMELEAF]     * VALIDXHTML
[THYMELEAF]     * VALIDXML
[THYMELEAF]     * HTML5
[THYMELEAF] * Template resolvers (in order):
[THYMELEAF]     * org.thymeleaf.templateresolver.ServletContextTemplateResolver
[THYMELEAF] * Message resolvers (in order):
[THYMELEAF]     * org.thymeleaf.spring4.messageresolver.SpringMessageResolver
[THYMELEAF] * Dialect [1 of 3]: org.thymeleaf.spring4.dialect.SpringStandardDialect
[THYMELEAF]     * Prefix: "th"
[THYMELEAF] * Dialect [2 of 3]: com.github.dandelion.thymeleaf.dialect.DandelionDialect
[THYMELEAF]     * Prefix: "ddl"
[THYMELEAF] * Dialect [3 of 3]: com.github.dandelion.datatables.thymeleaf.dialect.DataTablesDialect
[THYMELEAF]     * Prefix: "dt"
[THYMELEAF] TEMPLATE ENGINE CONFIGURED OK
[main] INFO org.thymeleaf.TemplateEngine - [THYMELEAF] TEMPLATE ENGINE INITIALIZED
[main] ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][main] Exception processing template "layouts/blank": Error during execution of processor 'com.github.dandelion.thymeleaf.processor.BundleAttrProcessor' (layouts/blank:2)

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'com.github.dandelion.thymeleaf.processor.BundleAttrProcessor' (layouts/blank:2)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
	at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
	at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
	at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
	at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:156)
	at at.pansoma.panaccount.GlobalControllerTest.testDoLogout(GlobalControllerTest.java:91)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:85)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:86)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:241)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

I'm lost, right no I started testing with avoiding "ddl:bundles" attributes in the html Templates. The application is working so far.
I'm missing something in the setup of the MockMvC Object.

Regards,

Karin
 
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing Spring MVC

Thibault Duchateau
Administrator
Hi,

You're probably missing the DandelionFilter.

What if you test with something like:

public class GlobalControllerTest extends AbstractShiroTest {
    @Inject
    protected WebApplicationContext webApplicationContext;

    protected DandelionFilter dandelionFilter;
    protected MockMvc mockMvc;

    @Before
    public void setUp() throws Exception {
        this.dandelionFilter = new DandelionFilter();
        this.dandelionFilter.init(new MockFilterConfig());
        this.mockMvc = webAppContextSetup(this.webApplicationContext).addFilters(dandelionFilter).build();
    }

    @Test
    public void testShowScreenLogin() throws Exception {
        mockMvc.perform(get("/"))
                .andExpect(status().isOk())
                .andExpect(view().name("login"));
    }
}

Regards,
Thibault.
Reply | Threaded
Open this post in threaded view
|

Re: Unit testing Spring MVC

charlei1968
Hi Thibault!

Thank you so much, now I'm getting an "Assertion Error" as expected!

Regards,

Karin