Reputation: 2523
I've never written a single unit test. But since every article I read, they are talking about unit testing. I figured I should get started with it.
But how?
Can someone point me to a very simple unit tested hello world example? Without the use of jUnit or the likes.
Upvotes: 1
Views: 2902
Reputation: 21
To answer the actual original question, this is possible with the use of "assert". Take for instance, this class:
public class Calculator
{
public int add(int a, int b)
{
// a bug
return a - b;
}
public void testAdd()
{
assert this.add(1, 1) == 2;
}
public Calculator()
{
this.testAdd();
}
}
Trying to create a new instance of this class will automatically run the test as part of the constructor. This allows for Self Testing Code. Take this Main class,
public class Main
{
public static void main(String[] args) throws Exception
{
Calculator calculator = new Calculator();
}
}
Compile the Main class like you normally would, but with the caveat that you set the "enableassertions" or "ea" flag:
javac Main.java
java -ea Main
output:
Exception in thread "main" java.lang.AssertionError
at Calculator.testAdd(Calculator.java:10)
at Calculator.<init>(Calculator.java:15)
at Main.main(Main.java:5)
Upvotes: 2
Reputation: 54386
If you don't want to use any other libraries then you have to do a lot of work yourself. For example, suppose you have a class with one function that you want to test:
class Foo {
public int bar(int input);
}
You can now write a test class:
class TestFoo {
public void testBarPositive() {
Foo foo = new Foo();
System.out.println(foo.bar(5) == 7);
}
public void testBarNegative() {
Foo foo = new Foo();
System.out.println(foo.bar(-5) == -7);
}
public static void main(String[] args) {
TestFoo t = new TestFoo();
t.testBarPositive();
t.testBarNegative();
}
}
This is a very basic example but it shows you how you could write your own unit tests.
That said, I strongly recommend using a library like JUnit. It gives you a lot for free and removes a huge amount of boiler-plate code that you would have to write yourself. It also generates nice reports and (when combined with something like Cobertura) can give you a fairly comprehensive view of how complete your testing is.
Upvotes: 6
Reputation: 7183
I would recomend that you DO use some other library. It sounds like what you're wanting to do is build your own Unit testing library. The problem with that is that any basic problems with your code, any logical errors you may be making, you're just as likely to make in your Unit testing code as well, so that yes, your Units pass testing, but only because they do what you expect them to do, not because it's what they actually SHOULD be doing using strict standards in whatever language your using.
Let's say you wrote a function that compares a true/false and returns 0. in PHP (0==false)=true but (0===false)=false if you've coded for scenario 1 but ignored scenario 2 and then write a unit test that tests that scenario 1 is valid but ignores scenario 2 (which you might if you always code that way) then your unit test passes based on what you have defined but is still wrong.
Upvotes: 0
Reputation: 308938
Check out the JUnit Cook's Tour.
I'd recommend either JUnit (version 4.4 or higher) or TestNG.
Upvotes: 2