简体   繁体   English

在Java Spring / Hibernate / Maven项目中测试多步骤过程的正确方法是什么?

[英]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. 最初,我只有一个进行单元测试所需的Java Servlet。 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. 我想确保它可以正确处理请求,因此我在jUnit测试中使用了Spring的MockHttpServletRequest,该方法效果很好。 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. 现在,我想扩展测试,并且要进行一系列数据库事务处理并模拟http servlet请求,以模拟一段时间内使用系统的用户。

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 ? 那么,我是否仅通过在已有的方法上方和下方添加更多用@Test注释的方法来模拟事件的时间轴? 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 . 看一下spring的测试框架 This means you will start your spring context, and have everything running. 这意味着您将启动Spring上下文,并运行所有内容。 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. 由于您正在使用Maven,因此实际上有一些不错的选择。 The recommendation here on codehaus might be valuable. 这里关于codehaus的建议可能很有价值。 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. 最后,如果您正在使用开发数据库并且希望继续这样做,那很好,但是如果这是一个连续的测试并且您没有回滚,那么Mockito或其他模拟框架可能会很好地为您服务。 That really depends on the context of your test though. 不过,这实际上取决于测试的环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试hibernate方言的正确方法是什么? - What is the proper way to test a hibernate dialect? 部署Java Hibernate Project的正确方法 - Proper way to deploy Java Hibernate Project Maven项目对代码进行编译和测试的过程是什么? - What is the process with Maven project to compile and test the code? 什么是部署Maven Java适配器的正确方法 - what is the proper way deploy maven java adapter 在Spring Transaction JUnit测试中自动装配Hibernate会话的正确方法 - Proper way to autowire a Hibernate Session in a Spring Transaction JUnit test 多模块项目中测试类的正确位置是什么? - What is the proper location for test classes in a multi module project? 单元测试配置类在 Multi Maven Spring Boot 项目中不起作用 - Unit Test a configuration class not working in Multi Maven Spring Boot project 在Java,Spring和Maven中工作的正确方法是什么? - what is the right way to work in java, spring and maven? Java9多模块Maven项目测试依赖项 - Java9 Multi-Module Maven Project Test Dependencies 在Non-Maven-Non-Spring项目中包含Maven-Spring-Roo-Hibernate jar的正确程序是什么? - What is the correct procedure for including a Maven-Spring-Roo-Hibernate jar in a Non-Maven-Non-Spring project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM