简体   繁体   English

没有Mock的Spring MVC测试

[英]Spring MVC Testing without Mocking

Ok, I come from Rails am having a bit of a hard time trying to get this working. 好的,我来自Rails,要使其正常工作有些困难。

Right know this is what I understand from the Spring framework (and please correct me if I'm wrong): 正确知道这是我从Spring框架中了解的内容(如果我错了,请纠正我):

  1. I am using Tomcat. 我正在使用Tomcat。 So what it does, basically, is go after web.xml, and check any configuration files from there in order to initialize and get beans auto wired, etc. 因此,基本上,它的工作是去到web.xml之后,然后从那里检查所有配置文件以进行初始化并自动连接Bean等。

  2. In my example, I have on web.xml the context config location as application-config.xml 在我的示例中,我在web.xml上将上下文配置位置为application-config.xml

  3. It turns out that application-config.xml has other config files that will take care of the following: 事实证明application-config.xml还有其他配置文件,这些文件将处理以下问题:

Hibernate: 休眠:

<util:properties id="hibernatePropertiesConfigurer" location="classpath:hibernate.properties"/>

JSon Converter: JSon Converter:

<import resource="classpath:json-converter.xml"/>

Managers ( @Component ) Scanner: 管理者( @Component )扫描程序:

<import resource="classpath:scanner-config.xml"/>

Among others. 其中。 In other words, these work just fine, web server gets up, managers ( @Components ) are @Autowired , @Controllers can call them, persist objects in the database, etc. 换句话说,这些工作正常,Web服务器启动,经理( @Components )是@Controllers @Autowired@Controllers可以调用它们,将对象持久存储在数据库中,等等。

Now, I want to test, ie run JUnit tests. 现在,我要测试,即运行JUnit测试。 I did find a lot of examples on how to mock these layers (managers, controllers) but I want to try the real thing: 我确实找到了许多有关如何模拟这些层(管理器,控制器)的示例,但我想尝试一下真正的东西:

  • Test will instantiate @Controller 测试将实例化@Controller
  • Test will post to @Controller object 测试将发布到@Controller对象
  • @Controller will have a real @Autowired (not mocked version) of @Component or @Bean @Controller具有@Component@Bean的真实@Autowired (非@Bean版本)
  • @Component will persist an @Entity @Component将保留一个@Entity
  • And, most important of all, database should see the changes. 而且,最重要的是,数据库应该看到更改。 (I am using MySQL) (我正在使用MySQL)
  • Test will, then, use an @Autowired instance of @Component , and query the database to confirm the persistence occurred. 然后,测试将使用@Component@Autowired实例,并查询数据库以确认发生了持久性。

Is this possible, at all, with Spring, JUnit testing? Spring,JUnit测试完全有可能吗?

If you want to verify the behavior of the entire stack like that, it's no longer a unit test, but an integration test. 如果要像这样验证整个堆栈的行为,则它不再是单元测试,而是集成测试。

If you're using Maven, you can use something like the Maven Jetty plugin to deploy your project into an embedded container during the pre-integration-test phase. 如果您使用的是Maven,则可以在pre-integration-test阶段使用Maven Jetty插件之类的项目将项目部署到嵌入式容器中。 Then in your tests, make the HTTP calls to the server at localhost, and verify you get the expected responses. 然后在测试中,对本地主机上的服务器进行HTTP调用,并验证您是否获得了预期的响应。 Then, in the post-integration-test phase, shut down the Jetty server. 然后,在post-integration-test阶段,关闭Jetty服务器。

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

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