Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Should we be suggesting Unit Testing?

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Should we be suggesting Unit Testing?

    This is mainly for the other helpers on the forum: when it comes to helping someone with their issues, should we be advocating unit testing more (or at all)? It seems to me that a lot of basic issues we see from new java programmers could be solved/debugged if basic unit testing was done by the OP. It is something extremely useful they could probably benefit from learning. It also encourages programmers to split up longer methods into smaller, more manageable ones (largely so code is easier to unit test).
    Keeping in mind that many of those who ask questions here are new to java (and usually programming in general), would suggesting unit testing be too "over-their-head", or something we should consider starting to do?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Should we be suggesting Unit Testing?

    I understand the point you're getting at, but the work involved in this, in correlation to the simple problems most users face is way too much work. At the bare-bones level, it usually involves downloading jars, adding jars to the build path (IDE dependent), importing libraries, creating multiple classes, using annotations and reading the relevant APIs. Agreed, after a certain threshold this work is trivial and hardly takes up any time, but we're typically dealing with new learners, who if taught responsibly, are starting off with the command line.

    Something you've noted however, is the ability unit testing has to highlight problematic areas and quickly resolve issues. With this in mind, I believe the best course of action would be to get an article or a blog up which acts as an introduction to unit testing and when a question of a sufficient skill level arisies which could be quickly resolved with this, you point them in the direction of the blog/article.

    Obviously everything i've referred to is with respect to unit-testing frameworks. Although users can easily make their own test harnesses without any library, my personal belief is that if you're ready to get into unit testing, you should be ready to use one of the defacto frameworks. However, some people might not agree with me on that.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Should we be suggesting Unit Testing?

    In my experience, there are not many blogs out there that describe unit testing in such a way where it isn't daunting to first timers. I agree that the dependency issue is significant if you are not using an IDE with junit already included.
    But, perhaps we could suggest an alternative for those who just want to dip their toes in it. Something as simple as asking them to create another class with just a main, which sets up variable to send to the problematic method, calls the method, and then prints out the output could replicate most of the the unit testing behavior, without having to be dependent on the junit framework.
    The problem I see with newbies on this forum is they approach the problem from: given my input in the beginning, why is my output in the end wrong?
    That is a very unproductive approach to debugging. Ideally, they should be questing the input and output for every single step of their program, not on just the input and output for the entire program.
    At the very least, unit testing forces the OP to be very aware of their intended input and output.
    Alternatively to even this, we could encourage the use of the assert keyword in buggy code. It would get the user to be aware of their intensions without having to create another class or require on dependencies (the assert keyword is built in, I'm not talking about the JUnit Assert class).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Should we be suggesting Unit Testing?

    I see your point, but we already do ask for that: we ask for an SSCCE that demonstrates exactly and only the problem in as few lines as possible. I think asking for formal unit testing is a bit much for a newbie (especially because most of them can barely cobble together an SSCCE). But asking them to break down the problem into smaller pieces and approach each piece one at a time is exactly what we already do.

    But if you want to throw together a blog about unit testing and point them to it, go for it!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Should we be suggesting Unit Testing?

    I think the problem with the SSCCE for newbies is that they see it as too confusing. Most of them seem to think that their programs cannot be simplified, since they believe the cause of their problem is an entire program thing, instead of what is usually just a single line of code.
    What the SSCCE doesn't address, which I feel is the root-cause of so many issues, is a complete disregard for coding standards. I'm not sure who these teachers and professors are who are teaching all of them coding but, with the exception of perhaps GUI creation, you usually should not have a method which is 100+ lines of code (I think at work, our limit is 50 lines), especially not a main which is 100+ lines. That code should be divided logically into different methods.
    Too many times do we see someone who has ALL of their code in their main. While I understand where the teachers are coming from, it is a horrible thing to encourage.

    I'll consider throwing together a simple blog or tutorial regarding how to "properly" divide large code segments, and then perform unit testing (whether with the junit framework and/or just a class with a main that tests the methods of another class) on code after I get through with the next release of my personal project.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Should we be suggesting Unit Testing?

    I can agree with the basics of what you're saying, but you can't really hold newbies to the same standard as a professional developer. You have to pick your battles so as not to overwhelm them. Maybe they don't really know how methods work, so everything is kept in the main method until they learn the basics of variables, if statements, and for loops. Proposing unit testing will go over their heads, I think. But encouraging them to break the problem down (is it your input or output? can you hardcode the input and get it working? what are the values of each variable before, during, and after that loop?) is actually teaching the skills that are required for unit testing. You can't require unit testing before teaching the prerequisites of isolating the problem, which we try to teach by asking for an SSCCE.

    But like I said this is really a personal preference. If you want to try to ask for unit testing, go for it.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Group/Unit, Folder/File?
    By rohmoh in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2012, 06:13 PM
  2. Java Unit Testing How-To Gettin' started
    By Massaslayer in forum Java Theory & Questions
    Replies: 2
    Last Post: May 1st, 2012, 07:58 AM
  3. Java automated testing tools for Unit testing
    By rameezraja in forum Member Introductions
    Replies: 2
    Last Post: April 14th, 2012, 08:51 AM
  4. HTML Unit Help
    By Seegee in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 2nd, 2012, 08:38 PM
  5. Unit Vector
    By mingming8888 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 14th, 2010, 02:53 PM