To quell a lingering feeling of inadequacy, you took the time to build your own planetary death ray, a veritable rite of passage in the engineering profession. Congratulations. And you were feeling pretty proud until the following weekend, when you purchased the limited-edition Star Wars trilogy with Ewok commentary, and upon watching the Death Star destroy Alderaan, you realized that you had made a bad decision: Your planetary death ray has a blue laser, but green lasers look so much cooler. But it's not a simple matter of going down to Radio Shack to purchase a green laser that you can swap into your existing death ray. You're going to have to build another planetary death ray from the ground-up to have a green laser, which is fine by you because owning two death rays instead of one will only make the neighbors more jealous.
Both your planetary death rays should interoperate with a variety of other bed-wettingly awesome technology, so it's natural that they export the same Java API:
public interface PlanetaryDeathRay {
public void aim(double xPosition, double yPosition);
public boolean fire(); /* call this if she says the rebel
base is on Dantooine */
}
public class BlueLaserPlanetaryDeathRay
implements PlanetaryDeathRay { /* implementation here */ }
public class GreenLaserPlanetaryDeathRay
implements PlanetaryDeathRay { /* implementation here */ }
Testing both death rays is important so there are no major malfunctions, like destroying Omicron Persei VIII instead of Omicron Persei VII. You want to run the same tests against both implementations to ensure that they exhibit the same behavior – something you could easily do if you only once defined tests that run against any PlanetaryDeathRay implementation. Start by writing the following abstract class that extends junit.framework.TestCase:
public abstract class PlanetaryDeathRayTestCase
extends TestCase {
protected PlanetaryDeathRay deathRay;
@Override protected void setUp() {
deathRay = createDeathRay();
}
@Override protected void tearDown() {
deathRay = null;
}
protected abstract PlanetaryDeathRay createDeathRay();
/* create the PlanetaryDeathRay to test */
public void testAim() {
/* write implementation-independent tests here against
deathRay.aim() */
}
public void testFire() {
/* write implementation-independent tests here against
deathRay.fire() */
}
}
Note that the setUp method gets the particular PlanetaryDeathRay implementation to test from the abstract createDeathRay method. A subclass needs to implement only this method to create a complete test: the testAim and testFire methods it inherits will be part of the test when it runs:
public class BlueLaserPlanetaryDeathRayTest
extends PlanetaryDeathRayTestCase {
protected PlanetaryDeathRay createDeathRay() {
return new BlueLaserPlanetaryDeathRay();
}
}
You can easily add new tests to this class to test functionality specific to BlueLaserPlanetaryDeathRay.
Remember to download this episode of Testing on the Toilet and post it in your office. Permalink | Links to this post |
8 comments:
I really like this blog. You're doing great job. Keep going.
Wow!
I went to upgrade my system to use one more laser: cyan
My missile defence system wasn't interface testable and now its all gone to hell! Curse you Google Test Blog!
Great tip its worked really well for us, originally read about it in Expert One-on-One J2EE Design and Development by Rod Johnson...this book has a great section on unit tests.
This is a cute post, but I respectfully disagree on the usefulness of abstract TestCase classes with test methods in the base class. My project went down this route recently. The problem we experienced was with finding failures. Our tools (IDE) would report a failing test, but it was often a failure in the base class test method and not the subclass test method that we were working on. So we'd experience test failures and not know which class was failing... the base class or the subclass? Without the failure being a "smoking gun" as to the problem, we quickly scaled back on this approach in favor of a composition based strategy.
In summary: if you do this then I recommend NOT putting any more test methods in the subclass and instead defining a new test case for any subclass-specific tests. Otherwise failing tests will be a frustrating exercise in debugging.
Really nice! This way the abstract test case acts as a Template Method and createDeathRay() creates the specific implementation of the interface on every subclass. Other classes which adhere to this contract can be added and tested easily! Keep going!
Weekends to peopleig2tmean that they can have a two-day wowgold4europe good rest. For example, people gameusdcan go out to enjoy themselves or get meinwowgoldtogether with relatives and friends to talk with each storeingameother or watch interesting video tapes with the speebiewhole family.
Everyone spends agamegoldweekends in his ownmmoflyway. Within two days,some people can relax themselves by listening to music, reading novels,or watchingogeworld films. Others perhaps are more active by playing basketball,wimming ormmorpgvipdancing. Different people have different gamesavorrelaxations.
I often spend weekends withoggsalemy family or my friends. Sometimes my parents take me on a visit to their old friends. Sometimesgamersell I go to the library to study or borrow some books tommovirtexgain much knowledge. I also go to see various exhibition to broadenrpg tradermy vision. An excursion to seashore or mountain resorts is my favorite way of spending weekends. Weekends are always enjoyable for me.
igxe swagvaultoforu wowgold-usaignmax wowgoldlivebrogame thsaleGoldRockUbrogameswagvaultgoldsoonoforuigxethsale
希望大家都會非常非常幸福~
「朵朵小語‧優美的眷戀在這個世界上,最重要的一件事,就是好好愛自己。好好愛自己,你的眼睛才能看見天空的美麗,耳朵才能聽見山水的清音。好好愛自己,你才能體會所有美好的東西,所有的文字與音符才能像清泉一樣注入你的心靈。好好愛自己,你才有愛人的能力,也才有讓別人愛上你的魅力。而愛自己的第一步,就是切斷讓自己覺得黏膩的過去,以無沾無滯的輕快心情,大步走向前去。愛自己的第二步,則是隨時保持孩子般的好奇,願意接受未知的指引;也隨時可以拋卻不再需要的行囊,一路雲淡風輕。親愛的,你是天地之間獨一無二的旅人,在陽光與月光的交替之中瀟灑獨行.............................................................................................................有時,你覺得痛。胃痛的時候,接受它,承認這個疼痛是你的身體的一部份,與它和平共處。心痛的時候,接受它,承認這個經驗是你的生命的一部份,與它和平共處。抗拒痛的存在,只會讓它更要證明它的存在,於是你就更痛。所以,.無論你有多麼不喜歡痛的感覺,還是要接納這個痛的事實。與你的痛站在同一邊,不逃避,不閃躲,不再與你的痛爭執,如此,你的痛才會漸漸不再胡鬧,才會乖乖平息下去。.................心願-你許下了一個心願,你閉上眼睛,在冥想之中把這個心願交託宙給宇整個讓宇宙推動它全部的力.量去執行.,你看見星球與星球的引力牽繫著彼此,你聽見虛空與虛空.唱裡著和妙美的聲音,為了你的心願,整個宇宙正在相互傳遞,然後你放下了心願,不僅是放下,最好你還把你的心願忘記,唯有如此,它才能脫離你,發展它自己,
當它在宇宙的遊歷結束之後,它自然會來到你身邊,以你曾經希望的方式回應你,許下,只是讓它發生,放下,才是讓>它實現,你的心願使你懂得不能執著的奧秘...................
Post a Comment