You can add your filters as normal and the request should pass through it just as your normal environment.
You can add your filters as normal and the request should pass through it just as your normal environment.
Marten Deinum
Java Consultant / Pragmatist / Open Source Enthousiast / Author
Pro Spring MVC: With Web Flow
Conspect
Have you read the reference guide.
Use the [ code ] tags, young padawan
Add them where?
I'm now creating my test class like this:
Code:@RunWith(org.springframework.test.context.junit4.SpringJUnit4ClassRunner.class) @WebAppConfiguration("file:target/build") @ContextConfiguration ({ "file:target/build/WEB-INF/config/springContext.xml", "file:target/build/WEB-INF/config/springWebDispatcherConfig.xml", }) public class TestServiceController { ... }
In your test method you have your test method that whips up the mockmvc stuff which also allows for adding filters or at least access to the chain to add filters.
Marten Deinum
Java Consultant / Pragmatist / Open Source Enthousiast / Author
Pro Spring MVC: With Web Flow
Conspect
Have you read the reference guide.
Use the [ code ] tags, young padawan
I'm pointing the @WebAppConfiguration annotation at my web app directory. Is it not using web.xml in there to configure things?
I configure MockMvc like this:
Code:@Before public void setup() { mMockMVC = MockMvcBuilders.webAppContextSetup(mWAC).build(); } @Autowired private WebApplicationContext mWAC;
The MockMvc builder has an addFilter method, you might need to cast it to an AbstractMockMvcBuilder for that. You can then construct an OSIVF (assuming you use the filter) and add it.
Marten Deinum
Java Consultant / Pragmatist / Open Source Enthousiast / Author
Pro Spring MVC: With Web Flow
Conspect
Have you read the reference guide.
Use the [ code ] tags, young padawan
Now I'm doing this:
But it complains. There's a sessionFactory configure in the application context (or there was, before I made these changes).Code:mMockMVC = MockMvcBuilders .webAppContextSetup(mWAC) .addFilter(new org.springframework.orm.hibernate3.support.OpenSessionInViewFilter()) .build();
Code:java.lang.IllegalArgumentException: ServletContext must not be null at org.springframework.util.Assert.notNull(Assert.java:112) at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:115) at org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(WebApplicationContextUtils.java:105) at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:88) at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:277) at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.lookupSessionFactory(OpenSessionInViewFilter.java:263) at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:195)
Okay, I think I figured out the filter part. I did this:
Code:javax.servlet.Filter osiv = new org.springframework.orm.hibernate3.support.OpenSessionInViewFilter(); org.springframework.mock.web.MockFilterConfig filterConfig = new org.springframework.mock.web.MockFilterConfig(mWAC.getServletContext(), "osiv"); osiv.init(filterConfig); mMockMVC = MockMvcBuilders .webAppContextSetup(mWAC) .addFilter(osiv) .build();
And, I'm back to where I was several hours ago: transactions are not committing. I'm doing this:
On the first call, it should return CREATED, and on the second, it should return OK (because the record already exists in the DB).Code:@Test public void testCreatePlayerNew() throws Exception { JSONObject d = new JSONObject(); d.put("someParam", kSomeValue); String url = String.format("/myserviceurl"); RequestBuilder reqB = MockMvcRequestBuilders .put(url) .accept(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON) .content(d.toString().getBytes("UTF-8")); mMockMVC .perform(reqB) .andExpect(status().isCreated()); mMockMVC .perform(reqB) .andExpect(status().isOk()); }
But instead, both calls return CREATED. Note that this works in the normal servlet container context.
So, part of the confusion I had stemmed from the fact that H2 DB was closing the in-memory DB after each last connection closed, then creating a new DB. This gave the appearance of not committing changes.
Since a given test might have several transactions, I finally changed the way I used the DB. It's opened once before Spring setup and the schema and base data is created, then after each test the tables are deleted. This has the disadvantage that autoincrement IDs don't start over, but I've just decided to write tests so that the don't rely on any particular ID being created.
As a side note: I was not able to use H2's INIT script to populate the DB. Even after configuring it to not destroy the DB after the last connection closes, it still re-ran the creation SQL each time. I modified my code to use JDBC to run the creation SQL in the @BeforeClass method.