简体   繁体   中英

What is the proper way to test a multi-step process in a Java Spring/Hibernate/Maven project?

I initially just had a Java Servlet that I needed to unit test. I wanted to make sure it would handle requests correctly, so I used Spring's MockHttpServletRequest in a jUnit test and that method worked great. Very simple unit test.

Now, I want to extend the test and I want to do a series of database transactions and mock http servlet requests to simulate users using the system over a period of time.

I guess I could cram all of this into a single unit test but that doesn't seem like the right thing to do since it would violate the spirit of a unit test.

So what is the proper way to test a series of events in a particular order like this?

Here's a stripped down version of what I have so far:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"testContext.xml"}) 
public class servletTest {

//Injected request
@Resource(name="mockTestServletRequest")
MockHttpServletRequest request;

@Test
public void mockRequest() {
    //perform a mock servlet request
}

So do I simulate a timeline of events by just adding more methods annotated with @Test above and below the one I have already ? Am I guaranteed that these methods will be executed in the order listed?

That will be an integration test. Take a look at spring's testing framework . This means you will start your spring context, and have everything running. You can either use an in-memory database, or a standalone one.

When everything is started, you can inject an instance of a controller of yours and trigger a request, then another one. By default each method is in a transaction, which gets rolled back when the method completes, so don't worry about polluting the database.

From a testing mentality perspective, I do agree with you in that a true Unit test should be exactly that: something that tests a single touch point in the code and not beyond that.

Since you are using Maven, you actually have some nice options available. The recommendation here on codehaus might be valuable. By modularizing your "Integrated Unit Tests", you can assign that to a separate goal and let your more intelligent and orchestrated tests live there.

Another option that I have used is putting an annotation on the test as follows:

@IfProfileValue(name="integration", value="true")

That allows you to set arguments on whether to run just your true unit tests or integration tests also.

Finally, if you are using a development database and would like to continue doing so, that's fine but if this is a continuous test and you are not rolling back, you might be well served by Mockito or another mocking framework. That really depends on the context of your test though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM