开发者

How to swap in mock services during Selenium ui testing?

开发者 https://www.devze.com 2023-02-16 08:03 出处:网络
We wish to introduce Selenium testing to our maven build process. Happily, there is a ton of information available on how to do this, but I\'m having trouble figuring out how to handle one of our requ

We wish to introduce Selenium testing to our maven build process. Happily, there is a ton of information available on how to do this, but I'm having trouble figuring out how to handle one of our requirements.

In an effort to separate our testing layers, we want to use mock service objects for the ui tests. All of these objects are already defined in Spring configuration files that we use in unit tests. Wiring these services is easy in a unit test (we're using @ContextConfiguration), but I don't know how to handle this configu开发者_运维百科ration swap when we're deploying the war to Jetty for Selenium tests.

We're using:

  • Spring MVC 3.0
  • Maven
  • Hudson


Worst: introduce special user/interface parameter/checkbox/role. In an application remember to use mocks for this special case everywhere in the code. Horrible in maintenance, error-prone and, let's face it, pretty lame. Most common thou...

Easiest solution: develop conditional includes in your Spring application context:

<import resource="services-${env}.xml"/>

where ${env} comes from pom.xml:

<properties>
    <env>prd</env> <!-- or test depending on build profile -->
</properties>

Remember to turn on resources filtering and use build profiles.

when doing Selenium tests. Switching can be done during Maven build or by some other filtering tool. Both files (services-prd.xml and services-test.xml) define same beans (same interfaces and/or ids), but of course the latter one uses mock implementations.

Best (IMHO): if you need to change the implementation at runtime, AOP + JMX will be great. Just wrap your real services with aspects and depending on some flag (accessible via JMX), use real services or mocks. Very clean and noninvasive.

0

精彩评论

暂无评论...
验证码 换一张
取 消