Loading ...
Sorry, an error occurred while loading the content.

[TDD] Practicing tdd..

Expand Messages
  • Martin Hauner
    Hi, I ve bought into the tdd idea but I m still a novice in actually practicing tdd. I ve started a tdd learning project. After the first few steps I have
    Message 1 of 7 , Jun 1, 2009
    View Source
    • 0 Attachment
      Hi,

      I've bought into the tdd idea but I'm still a novice in actually practicing tdd.

      I've started a tdd learning project. After the first few steps I have collected
      a couple of questions. Maybe you can help me and tell me if its reasonable what
      I'm doing or if I'm completly off track. :)

      The first step in my simple project was to download an rss feed using
      java.net.URL. The first idea was to test the URL download, but then, it is a jdk
      class and I don't want to test that but my own code. Don't I?

      My code would be simply storing the feed content internally or throwing an
      exception if the download fails. Because URL can't be mocked (at least with
      mockito) I created an URL wrapper interface (FeedURL) and created two test
      cases. One where the mocked FeedURL returns some rss feed xml and one where it
      throws an exception.

      That worked. But for the live code I would need a real implementation for
      FeedURL. How would I tdd that into existance?

      There is the issue that I don't want to setup a http server to put a real http
      url in URL. file: & jar: have a the problem about properly locating the file or
      jar when running the test.

      After googling a bit it looked not to complicated to create my own in memory URL
      protocol. It would call a method on a class and deliver its result. This way I
      could store my test data in memory and use the real URL class. E.g. the url
      bytes:iam/learning/tdd/Class#Method would call Method on class
      iam.learning.tdd.Class.

      So I started to tdd it. After the tests run, I started to refactor the main
      method which was really dirty with several try/catches for all the stuff that
      can happen when sing reflection.

      I extracted 2 new classes. Since I extracted them during refactoring they do not
      have any tests.

      Is this ok? Or should I create new tests for them? Does this violate the rule
      that each production code should be forced into existance by tests? Initially it
      was, but the code in the class now has a number of small methods and so on. So
      it is very different from the original code.

      On another learning exercise I ended up with this scenario: On refactoring the
      tested class and extracting a new class the tests that I wrote against the
      original class would better fit to the extracted class now.

      Would I move the tests to the new class?


      --
      Martin
    • Olof Bjarnason
      Many similarities to my experience of TDD so far... Some notes upon reading your story: 1. You _don t_ have to test everything. Especially testing third party
      Message 2 of 7 , Jun 1, 2009
      View Source
      • 0 Attachment
        Many similarities to my experience of TDD so far...

        Some notes upon reading your story:

        1. You _don't_ have to test everything. Especially testing third party APIs
        is somewhat "vague" to me - if you really want to be sure they don't change,
        for example you are using a library under heavy development, that might be a
        good idea. But if you are using som Java SDK stable things, then those
        integration tests (I call tests that excercise third party code integration
        tests) are not so valuable to me.

        2. Refactoring code into new classes, "microclasses", is a common pattern
        for me too. That code is actually covered by tests, just not "class close"
        tests. So I think they are OK, even without tests.

        cya

        2009/6/1 Martin Hauner <hauner@...>

        >
        >
        > Hi,
        >
        > I've bought into the tdd idea but I'm still a novice in actually practicing
        > tdd.
        >
        > I've started a tdd learning project. After the first few steps I have
        > collected
        > a couple of questions. Maybe you can help me and tell me if its reasonable
        > what
        > I'm doing or if I'm completly off track. :)
        >
        > The first step in my simple project was to download an rss feed using
        > java.net.URL. The first idea was to test the URL download, but then, it is
        > a jdk
        > class and I don't want to test that but my own code. Don't I?
        >
        > My code would be simply storing the feed content internally or throwing an
        > exception if the download fails. Because URL can't be mocked (at least with
        >
        > mockito) I created an URL wrapper interface (FeedURL) and created two test
        > cases. One where the mocked FeedURL returns some rss feed xml and one where
        > it
        > throws an exception.
        >
        > That worked. But for the live code I would need a real implementation for
        > FeedURL. How would I tdd that into existance?
        >
        > There is the issue that I don't want to setup a http server to put a real
        > http
        > url in URL. file: & jar: have a the problem about properly locating the
        > file or
        > jar when running the test.
        >
        > After googling a bit it looked not to complicated to create my own in
        > memory URL
        > protocol. It would call a method on a class and deliver its result. This
        > way I
        > could store my test data in memory and use the real URL class. E.g. the url
        >
        > bytes:iam/learning/tdd/Class#Method would call Method on class
        > iam.learning.tdd.Class.
        >
        > So I started to tdd it. After the tests run, I started to refactor the main
        >
        > method which was really dirty with several try/catches for all the stuff
        > that
        > can happen when sing reflection.
        >
        > I extracted 2 new classes. Since I extracted them during refactoring they
        > do not
        > have any tests.
        >
        > Is this ok? Or should I create new tests for them? Does this violate the
        > rule
        > that each production code should be forced into existance by tests?
        > Initially it
        > was, but the code in the class now has a number of small methods and so on.
        > So
        > it is very different from the original code.
        >
        > On another learning exercise I ended up with this scenario: On refactoring
        > the
        > tested class and extracting a new class the tests that I wrote against the
        > original class would better fit to the extracted class now.
        >
        > Would I move the tests to the new class?
        >
        > --
        > Martin
        >
        >



        --
        twitter.com/olofb
        olofb.wordpress.com
        olofb.wordpress.com/tag/english


        [Non-text portions of this message have been removed]
      • Steven Smith
        Some good problems to work on as you learn TDD are the Euler problems: http://projecteuler.net/ You can also check out the bowling game kata:
        Message 3 of 7 , Jun 1, 2009
        View Source
        • 0 Attachment
          Some good problems to work on as you learn TDD are the Euler problems:
          http://projecteuler.net/

          You can also check out the bowling game kata:
          http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata

          I'm planning to post some info about katafication of the first few
          Euler problems based on numerous TDD exercises we've done with them at
          NimblePros.com when I have a chance.

          Steve
          Blog: http://SteveSmithBlog.com/


          On Mon, Jun 1, 2009 at 10:17 AM, Olof Bjarnason
          <olof.bjarnason@...> wrote:
          >
          >
          > Many similarities to my experience of TDD so far...
          >
          > Some notes upon reading your story:
          >
          > 1. You _don't_ have to test everything. Especially testing third party APIs
          > is somewhat "vague" to me - if you really want to be sure they don't change,
          > for example you are using a library under heavy development, that might be a
          > good idea. But if you are using som Java SDK stable things, then those
          > integration tests (I call tests that excercise third party code integration
          > tests) are not so valuable to me.
          >
          > 2. Refactoring code into new classes, "microclasses", is a common pattern
          > for me too. That code is actually covered by tests, just not "class close"
          > tests. So I think they are OK, even without tests.
          >
          > cya
          >
          > 2009/6/1 Martin Hauner <hauner@...>
          >
          >>
          >>
          >> Hi,
          >>
          >> I've bought into the tdd idea but I'm still a novice in actually
          >> practicing
          >> tdd.
          >>
          >> I've started a tdd learning project. After the first few steps I have
          >> collected
          >> a couple of questions. Maybe you can help me and tell me if its reasonable
          >> what
          >> I'm doing or if I'm completly off track. :)
          >>
          >> The first step in my simple project was to download an rss feed using
          >> java.net.URL. The first idea was to test the URL download, but then, it is
          >> a jdk
          >> class and I don't want to test that but my own code. Don't I?
          >>
          >> My code would be simply storing the feed content internally or throwing an
          >> exception if the download fails. Because URL can't be mocked (at least
          >> with
          >>
          >> mockito) I created an URL wrapper interface (FeedURL) and created two test
          >> cases. One where the mocked FeedURL returns some rss feed xml and one
          >> where
          >> it
          >> throws an exception.
          >>
          >> That worked. But for the live code I would need a real implementation for
          >> FeedURL. How would I tdd that into existance?
          >>
          >> There is the issue that I don't want to setup a http server to put a real
          >> http
          >> url in URL. file: & jar: have a the problem about properly locating the
          >> file or
          >> jar when running the test.
          >>
          >> After googling a bit it looked not to complicated to create my own in
          >> memory URL
          >> protocol. It would call a method on a class and deliver its result. This
          >> way I
          >> could store my test data in memory and use the real URL class. E.g. the
          >> url
          >>
          >> bytes:iam/learning/tdd/Class#Method would call Method on class
          >> iam.learning.tdd.Class.
          >>
          >> So I started to tdd it. After the tests run, I started to refactor the
          >> main
          >>
          >> method which was really dirty with several try/catches for all the stuff
          >> that
          >> can happen when sing reflection.
          >>
          >> I extracted 2 new classes. Since I extracted them during refactoring they
          >> do not
          >> have any tests.
          >>
          >> Is this ok? Or should I create new tests for them? Does this violate the
          >> rule
          >> that each production code should be forced into existance by tests?
          >> Initially it
          >> was, but the code in the class now has a number of small methods and so
          >> on.
          >> So
          >> it is very different from the original code.
          >>
          >> On another learning exercise I ended up with this scenario: On refactoring
          >> the
          >> tested class and extracting a new class the tests that I wrote against the
          >> original class would better fit to the extracted class now.
          >>
          >> Would I move the tests to the new class?
          >>
          >> --
          >> Martin
          >>
          >>
          >
          > --
          > twitter.com/olofb
          > olofb.wordpress.com
          > olofb.wordpress.com/tag/english
          >
          > [Non-text portions of this message have been removed]
          >
          >



          --
          Steve Smith
          http://SteveSmithBlog.com/
          http://twitter.com/ardalis
        • Donaldson, John (GEO)
          Martin, It sounds as though you re going along nicely. I d echo Olof and say if you find new classes while refactoring, you don t need to create explicit tests
          Message 4 of 7 , Jun 1, 2009
          View Source
          • 0 Attachment
            Martin,

            It sounds as though you're going along nicely. I'd echo Olof and say if you find new classes while refactoring, you don't need to create explicit tests for them.

            My other input would be to get away from live feeds as quickly as you can. Just one or two "integration" tests, and the rest should be isolated from the real world.

            I have another practice that helps me sometimes - not really TDD - create a simple version of the real application that uses the growing body of code and run that from time to time - it's usually instructive and revealing.

            John D.

            -----Original Message-----
            From: testdrivendevelopment@yahoogroups.com [mailto:testdrivendevelopment@yahoogroups.com] On Behalf Of Martin Hauner
            Sent: 01 June 2009 15:29
            To: testdrivendevelopment@yahoogroups.com
            Subject: [TDD] Practicing tdd..

            Hi,

            I've bought into the tdd idea but I'm still a novice in actually practicing tdd.

            I've started a tdd learning project. After the first few steps I have collected
            a couple of questions. Maybe you can help me and tell me if its reasonable what
            I'm doing or if I'm completly off track. :)

            The first step in my simple project was to download an rss feed using
            java.net.URL. The first idea was to test the URL download, but then, it is a jdk
            class and I don't want to test that but my own code. Don't I?

            My code would be simply storing the feed content internally or throwing an
            exception if the download fails. Because URL can't be mocked (at least with
            mockito) I created an URL wrapper interface (FeedURL) and created two test
            cases. One where the mocked FeedURL returns some rss feed xml and one where it
            throws an exception.

            That worked. But for the live code I would need a real implementation for
            FeedURL. How would I tdd that into existance?

            There is the issue that I don't want to setup a http server to put a real http
            url in URL. file: & jar: have a the problem about properly locating the file or
            jar when running the test.

            After googling a bit it looked not to complicated to create my own in memory URL
            protocol. It would call a method on a class and deliver its result. This way I
            could store my test data in memory and use the real URL class. E.g. the url
            bytes:iam/learning/tdd/Class#Method would call Method on class
            iam.learning.tdd.Class.

            So I started to tdd it. After the tests run, I started to refactor the main
            method which was really dirty with several try/catches for all the stuff that
            can happen when sing reflection.

            I extracted 2 new classes. Since I extracted them during refactoring they do not
            have any tests.

            Is this ok? Or should I create new tests for them? Does this violate the rule
            that each production code should be forced into existance by tests? Initially it
            was, but the code in the class now has a number of small methods and so on. So
            it is very different from the original code.

            On another learning exercise I ended up with this scenario: On refactoring the
            tested class and extracting a new class the tests that I wrote against the
            original class would better fit to the extracted class now.

            Would I move the tests to the new class?


            --
            Martin


            ------------------------------------

            Yahoo! Groups Links
          • Martin Hauner
            Hi, ... How do you organize your test code? I like to separate the production code and test into different folders but in the same (java) package. Where do you
            Message 5 of 7 , Jun 2, 2009
            View Source
            • 0 Attachment
              Hi,

              On 02.06.2009 8:45 Uhr, Donaldson, John (GEO) wrote:
              > Martin,
              >
              > It sounds as though you're going along nicely. I'd echo Olof and say if
              > you find new classes while refactoring, you don't need to create
              > explicit tests for them.
              >
              > My other input would be to get away from live feeds as quickly as you
              > can. Just one or two "integration" tests, and the rest should be
              > isolated from the real world.

              How do you organize your test code? I like to separate the production code and
              test into different folders but in the same (java) package. Where do you place
              the "integration" tests? Simply along the "unit" tests or in another folder?

              > I have another practice that helps me sometimes - not really TDD -
              > create a simple version of the real application that uses the growing
              > body of code and run that from time to time - it's usually instructive
              > and revealing.
              >
              > John D.

              Thanks for the feedback :)

              --
              Martin
            • Donaldson, John (GEO)
              Martin, ... Good questions. I have a separate .NET project for my test suite and a bunch of projects for my code (it will depend a bit on what architecture
              Message 6 of 7 , Jun 2, 2009
              View Source
              • 0 Attachment
                Martin,

                > How do you organize your test code? I like to separate the production code and
                > test into different folders but in the same (java) package. Where do you place
                > the "integration" tests? Simply along the "unit" tests or in another folder?

                Good questions.

                I have a separate .NET project for my test suite and a bunch of projects for my code (it will depend a bit on what architecture I've got - for example with MVP I may have three separate projects plus something to hold interfaces. But there are many ways to organize this and I don't feel very strict.

                For real integration tests I'll use a separate project. But for the example we're talking about, I'd probably keep everything together to start with. The real reason to split things up, I think, will be how fast your tests run. If they all run fast, then run them all! But likely the "integration" tests will run slowly.

                John D.


                -----Original Message-----
                From: testdrivendevelopment@yahoogroups.com [mailto:testdrivendevelopment@yahoogroups.com] On Behalf Of Martin Hauner
                Sent: 02 June 2009 21:04
                To: testdrivendevelopment@yahoogroups.com
                Subject: Re: [TDD] Practicing tdd..

                Hi,

                On 02.06.2009 8:45 Uhr, Donaldson, John (GEO) wrote:
                > Martin,
                >
                > It sounds as though you're going along nicely. I'd echo Olof and say if
                > you find new classes while refactoring, you don't need to create
                > explicit tests for them.
                >
                > My other input would be to get away from live feeds as quickly as you
                > can. Just one or two "integration" tests, and the rest should be
                > isolated from the real world.

                How do you organize your test code? I like to separate the production code and
                test into different folders but in the same (java) package. Where do you place
                the "integration" tests? Simply along the "unit" tests or in another folder?

                > I have another practice that helps me sometimes - not really TDD -
                > create a simple version of the real application that uses the growing
                > body of code and run that from time to time - it's usually instructive
                > and revealing.
                >
                > John D.

                Thanks for the feedback :)

                --
                Martin


                ------------------------------------

                Yahoo! Groups Links
              • Lior Friedman
                Just to add, There might come a point in time in which the number of test will become just too big to handle all of them in a single project. When that happens
                Message 7 of 7 , Jun 3, 2009
                View Source
                • 0 Attachment
                  Just to add,



                  There might come a point in time in which the number of test will become
                  just too big to handle all of them in a single project.

                  When that happens a common solution is to split the tests to several
                  projects the same way the production code is split.

                  That is for each production project there's a parallel test project.



                  In many cases I saw the same logic dictates an identical folders structure
                  on the disk where the production folder tree was duplicated for tests and
                  each test suite is stored according to the class under tests location.



                  Lior







                  From: testdrivendevelopment@yahoogroups.com
                  [mailto:testdrivendevelopment@yahoogroups.com] On Behalf Of Donaldson, John
                  (GEO)
                  Sent: Wednesday, June 03, 2009 12:16 AM
                  To: testdrivendevelopment@yahoogroups.com
                  Subject: RE: [TDD] Practicing tdd..

                  Martin,

                  > How do you organize your test code? I like to separate the production code
                  and
                  > test into different folders but in the same (java) package. Where do you
                  place
                  > the "integration" tests? Simply along the "unit" tests or in another
                  folder?

                  Good questions.

                  I have a separate .NET project for my test suite and a bunch of projects for
                  my code (it will depend a bit on what architecture I've got - for example
                  with MVP I may have three separate projects plus something to hold
                  interfaces. But there are many ways to organize this and I don't feel very
                  strict.

                  For real integration tests I'll use a separate project. But for the example
                  we're talking about, I'd probably keep everything together to start with.
                  The real reason to split things up, I think, will be how fast your tests
                  run. If they all run fast, then run them all! But likely the "integration"
                  tests will run slowly.

                  John D.

                  -----





                  [Non-text portions of this message have been removed]
                Your message has been successfully submitted and would be delivered to recipients shortly.