Announcement Announcement Module
Collapse
No announcement yet.
In unit testing, transactions aren't committed Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    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.

    Comment


    • #17
      Originally posted by Marten Deinum View Post
      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.
      Now I'm doing this:

      Code:
      mMockMVC = MockMvcBuilders
                  .webAppContextSetup(mWAC)
                  .addFilter(new org.springframework.orm.hibernate3.support.OpenSessionInViewFilter())
                  .build();
      But it complains. There's a sessionFactory configure in the application context (or there was, before I made these changes).

      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)

      Comment


      • #18
        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();

        Comment


        • #19
          And, I'm back to where I was several hours ago: transactions are not committing. I'm doing this:

          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());
          }
          On the first call, it should return CREATED, and on the second, it should return OK (because the record already exists in the DB).

          But instead, both calls return CREATED. Note that this works in the normal servlet container context.

          Comment


          • #20
            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.

            Comment

            Working...
            X