I had a conversation with my friend as follows:
- How can I run my unit tests in some specific order Çetin? I need to execute them in order, otherwise some of my tests fail.
- Unit tests must be “independent”. So, you don’t need any order in fact and you can’t guarantee it also.
I tried to explain him why they must be independent shortly. After this conversation, I opened my unit test reference book (Pragmatic Unit Testing in Java with JUnit) and looked again the properties of good unit tests to refresh my memory. Let’s remember them together briefly:
Unit tests need to be run automatically. Invoking one more unit tests can’t be any more complicated than pressing one button in the IDE or typing in one command at the prompt.
Don’t introduce a test that breaks the automatic model by requiring manual steps. Think about a man sitting in front of the machine waiting for popups to confirm. It is not we want. Whatever test requires (database, network connection etc.) make these an automatic part of the test.
A machine should be running all of the unit tests for all checked-in code continuously. This automatic check ensures that whatever is checked-in hasn’t broken any test, anywhere. Investigate systems such as Cruise Control, Hudson, Teamcity, Anthillpro, Apache Continuum etc.
Test must determine for itself whether it passed or failed. It means you don’t have to think about it. No one needs to read the test results to check whether the code is working or not.
Good unit tests are thorough; they test everything that’s likely to break. But just how thorough? Every line of code, branch, exception or most likely candidates for error like boundary conditions, missing and malformed data, and so on. It’s a question of judgment, based on the needs of your project (time, effort, budget). If you want to aim for more complete coverage, then you may want to invest in code coverage tools (EMMA, Cobertura, Clover etc.) to help.
The goal is that every test should be able to run over and over again, in any order, and produce the same results every time. This means that tests cannot rely on anything in the external environment that isn’t under your direct control.Use mock objects (JMock, EasyMock, JMockit, and so on) as necessary to isolate the item under test and keep it independent from the environment.
Each test should produce the same results every time. If it doesn’t, then that should tell you that there’s a real bug in the code.
Tests need to be kept independent from the environment and each other. Make sure you’re only testing one thing at a time. It doesn’t mean that you use only one assert in a test, but that one test method should focus on a method, or a small set of production methods that, together, provide some feature.
Independent also means that no test relies on any other test; you should be able to run any individual test at any time, and in any order. You don’t want to have to rely on any other test having run first. Use per-test and per-class setup/teardown methods to get a fresh start.
The code you write for a unit test is real. This means that it must be written and maintained to the same professional standards as your production code. You write lots of test code (as much as real code) therefore, test code needs to be kept neat and tidy, well designed and well-factored, just as professional as the production code.
So, a-trip always makes you feel good.
Source: Pragmatic Unit Testing in Java with JUnit
Posted in TDD.
– October 25, 2008
5 tips you can use while debugging your Java code in Eclipse.
- Logical Structure
The logical structure is very handy for examining things like maps and other java collections classes, instead of showing the full detail of all the internal variables the view will only show the “logical” pieces. It is activated/deactivated from “Variables” view using logical structure toggle button.
- Conditional Debugging
Once you know where an error occurs, you will want to see what the program is doing right before it crashes. One way to do this is to step through every statement in the program, one at a time, until you reach the point of concern. Sometimes it’s better to just run a section of code and stop execution at that point so you can examine data at that location. It’s possible to declare conditional breakpoints triggered whenever the value of an expression changes. In addition, code assist is available when typing in the conditional expression.
Right click your breakpoint and select Breakpoint Properties… to set a condition. Check “Enable Condition” box and write your expression below it. You can define a “hit count” that stops your code after that count is reached if you want.
- Java Exception Breakpoint
Use “Java Exception Breakpoint” feature to track your exceptions more easily. For example, your program might be throwing a NullPointerException or FileNotFoundException and you want to track it down. Rather than go to the line of code and set a breakpoint, you can specify that the debugger should always stop when the exception occurs.
- Step Filtering
“Step Into” is one of the most used feature while debugging the source code. Sometimes, it takes us through several stack traces to reach the code we want to debug. In such cases, “Step with Filters” functionality can help us. It simply filters out the classes that we don’t want to step into.
To specify which classes to filter out when “Step with Filters” is used, the preferences must be set. To activate the preferences panel choose Window > Preferences from the main window. Once there navigate to Java > Debug > Step Filtering and choose the classes/packages to be filtered out. When you step through the code, ensure that the “Use Step Filters” toggle button in Debug view is on.
- Expression Evaluation
“Inspect” is a handy feature for evaluating your expressions. In the Debug perspective, select your expression, right click it and select “Inspect” from the menu to evaluate. You can do it by simply Ctrl+Shift+I key combination.
“Display View” is another efficient helper in evaluating expressions. Activate it from Window > Show View > Display, write your expressions to evaluate and execute them by right clicking and selecting “Display” from the context menu in the Debug perspective or simply by Ctrl+Shift+D shortcut. You can use code assist (Ctrl+Space) to compose your expressions.
Posted in Eclipse, Java, Tips & Tricks.
– September 13, 2008
Sometimes, I cannot find time to read my daily selected articles. I bookmark them, later I forget to read. E-mailing those articles can be a solution, however it is not effective and takes time to manage.
“Read It Later” is a Firefox extension that creates a manageable list of links to be read later. New features of it for Firefox 3 users are:
- One click saving, sorting, tagging
- Sync your list with any number of computers
- Offline reading
- Access anywhere
- RSS feeds
- Click to save mode
- Save all tabs for later
Let’s see it in action:
- You like a page and want to save it for later reading. Just click the little “tick” at the end of the address bar. It is added your list and the little “tick” turns red. You can add links to your list by right clicking on the link and selecting “Read This Link Later”.
- You want to read them now. Click the little “book” icon in the navigation toolbar and select the desired link from the list.
- You are complete with the link and want to delete it from the list. Just click the “tick” icon in the address bar one more time, select the “Just Mark As Read” button to delete it. You can add your link to losts of social sites here before deletion if you want.
Tip: You can add links to your list quickly with the “Click to Save Mode” option. You click the links and they are added to your list automatically rather than followed through. It is activated by pressing “Alt + m” or clicking the little red “tick” icon in the bottom right corner. Try it.
Detailed explaination about “Read It Later” is here.
Firefox add-on page for “Read It Later” is here.
I think, it is great. Happy reading.
Posted in Firefox.
– August 26, 2008
Here is my top source code seach engines and I wanna share them with you.
- Google Code Search
- Byte My Code
- Dzone Snippets
- Code Fetch
- O’reilly Code Search
Please share yours with us.
Posted in Online Tools, Programming.
– August 1, 2008