如何将 ScalaTest 与 Spring 集成
我需要使用 Spring 上下文中的 @Autowired
字段填充我的 ScalaTest 测试,但大多数 Scalatest 测试(例如 FeatureSpec
)无法由 SpringJUnit4ClassRunner 运行。 class
-
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="myPackage.UnitTestSpringConfiguration", loader=AnnotationConfigContextLoader.class)
public class AdminLoginTest {
@Autowired private WebApplication app;
@Autowired private SiteDAO siteDAO;
(Java,但您明白要点了)
如何从 ScalaTest 的 ApplicationContext
填充 @Autowired
字段?
class AdminLoginFeatureTest extends FeatureSpec with GivenWhenThen with ShouldMatchersForJUnit {
@Autowired val app: WebApplication = null
@Autowired val siteDAO: SiteDAO = null
feature("Admin Login") {
scenario("Correct username and password") {...}
I need to populate my ScalaTest tests with @Autowired
fields from a Spring context, but most Scalatest tests (eg FeatureSpec
s can't be run by the SpringJUnit4ClassRunner.class
-
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="myPackage.UnitTestSpringConfiguration", loader=AnnotationConfigContextLoader.class)
public class AdminLoginTest {
@Autowired private WebApplication app;
@Autowired private SiteDAO siteDAO;
(Java, but you get the gist).
How do I populate @Autowired
fields from an ApplicationContext
for ScalaTest?
class AdminLoginFeatureTest extends FeatureSpec with GivenWhenThen with ShouldMatchersForJUnit {
@Autowired val app: WebApplication = null
@Autowired val siteDAO: SiteDAO = null
feature("Admin Login") {
scenario("Correct username and password") {...}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
TestContextManager
,因为它会缓存上下文,这样就不会在每次测试时都重新构建它们。它是从类注释配置的。Use the
TestContextManager
, as this caches the contexts so that they aren't rebuilt every test. It is configured from the class annotations.这是一个使用
TestContextManager
作为可堆栈特征实现的版本(以便您可以拥有自己的beforeAll()
和afterAll()
方法,如果需要的话) code> 来完成上下文生命周期。我尝试了其他帖子中建议的原始
TestContextManager.prepareTestInstance()
解决方案,但注意到我的上下文没有关闭,这会导致使用 sbt 控制台时每次连续测试运行后产生副作用并积累垃圾。TestContextManagement Gist
Here's a version implemented as a stackable trait (so that you can have your own
beforeAll()
andafterAll()
methods, if necessary) that usesTestContextManager
to complete the context lifecycle.I tried the raw
TestContextManager.prepareTestInstance()
solution suggested in other posts, but noticed that my contexts weren't getting closed which results in side effects and accumulating garbage after each successive test run when using the sbt console.TestContextManagement Gist
我尝试在 Spring 4 + Scala 2.11 中使用 Duncan 的答案,但出现以下错误:
在配置 ContextConfiguration 而不是字符串时,我必须调整他的代码以使用类:
I tried using Duncan's answer with Spring 4 + Scala 2.11 and I got the following error:
I had to tweak his code to use a class when configuring the ContextConfiguration instead of a string:
如果您使用 Spring boot,您可以使用 TestContextManager (如其他注释建议的那样)和 @SpringBootTest 注释。
这是我使用 scalaTest 和 spring boot 测试控制器的方法:
这里有一篇关于如何使用 spring boot 和 ScalaTest 进行集成测试和单元测试的文章:
ignaciosuay.com/testing-spring-boot-with-scalatest/
If you are using Spring boot you could use the TestContextManager (as other comments suggested) and the @SpringBootTest annotation.
This is how I test a controller with scalaTest and spring boot:
Here there is a post about how to do integration tests and unit tests with spring boot and ScalaTest:
ignaciosuay.com/testing-spring-boot-with-scalatest/