patternjavaMinor
Single class unit test framework in Java
Viewed 0 times
javasingletestclassunitframework
Problem
This is a single class and dependency free1 unit test framework for Java. The only assertion available is
(Utilizes feature of Java7 : Multi catch)
What it does
What I want reviewed
```
package info.simpll.simpletest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Tester {
private static int successCount = 0;
private static int testCount = 0;
//--------------------------------------
//Test annonation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public static @interface Test {
public String name() default "unnamed"; // name of test
public int repeat() default 1; // repeat count if calcTime==true
public boolean calcTime() default false; // calculate Time
}
//--------------------------------------
//---------------------------------------
//Logger
private static enum Logger {
INFO(">"), ERROR("Error -->");
private final String val;
private Logger(String val) {
this.val = val;
}
@Override
public String toString() {
return val;
}
public static void log(Logger level, CharSequence message) {
System.out.printf("%s %s", level, message);
Syste
assertTrue (this can be used for all test cases).(Utilizes feature of Java7 : Multi catch)
What it does
- Execute all methods with annotation
Test. should fail if any arguments are needed.
- Calculate elapsed time using a given
repeatcount.
- Print state for each test case.
- Print number of successful test cases and total number of test cases.
assertTruemust be called to be successful.
What I want reviewed
- Potential performance improvements.
- Potential flaws in coding style.
- Potential flaws in API.
- Or anything else related to this that might be appropriate.
```
package info.simpll.simpletest;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Tester {
private static int successCount = 0;
private static int testCount = 0;
//--------------------------------------
//Test annonation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public static @interface Test {
public String name() default "unnamed"; // name of test
public int repeat() default 1; // repeat count if calcTime==true
public boolean calcTime() default false; // calculate Time
}
//--------------------------------------
//---------------------------------------
//Logger
private static enum Logger {
INFO(">"), ERROR("Error -->");
private final String val;
private Logger(String val) {
this.val = val;
}
@Override
public String toString() {
return val;
}
public static void log(Logger level, CharSequence message) {
System.out.printf("%s %s", level, message);
Syste
Solution
-
You are reinventing-the-wheel for the unit testing framework, are you intending to do so for the logging parts as well? Perhaps consider integrating with an external logging framework?
-
You have code duplication when you need to do timing benchmarks, since you are invoking the method once first, and then if it is successful invoke it
-
It sounds weird to have a
You are reinventing-the-wheel for the unit testing framework, are you intending to do so for the logging parts as well? Perhaps consider integrating with an external logging framework?
- If you are implementing your logging framework, then you may want to re-evaluate how you are specifying the prefix. What I can see now is that you define a
privatefieldvalas the prefix, and then rely on yourLogger.toString()representation inside the staticlog()method to print it. Sounds like a roundabout way to me...
-
You have code duplication when you need to do timing benchmarks, since you are invoking the method once first, and then if it is successful invoke it
n times. You can consider treating all tests to run for n=1 times, which should simplify the implementation. Timing can then be automatically taken for cases where n > 1, and that may mean you don't need the explicit boolean flag to indicate whether the timing is required.-
It sounds weird to have a
SuccessException, but I suppose there's no other way (I didn't study how existing unit testing frameworks work).Context
StackExchange Code Review Q#59180, answer score: 5
Revisions (0)
No revisions yet.