Creating unit tests (semi) automatically?

Is there an environment that supports the creation of some standard unit tests from annotations? An example of what I mean is:

@HasPublicDefaultConstructor public class Foo { } 

This will obviously be used to automatically create a unit test, which checks if Foo has a default constructor. Am I the only person who was still thinking something about this ?;) Although I am most interested in Java, solutions in other languages ​​will certainly be interesting as well.

EDIT: In response to S. Lott's answer, let me clarify:

I am trying to check if a class has a default constructor. (Of course, this is just an example.) I could just do it by writing a test, but I find it rather tedious. So I'm looking for a tool that handled annotations at compile time (via APT) and would generate a test for me. Does something like this exist? If not, do you think this is a good idea?

+6
java unit-testing annotations code-generation automation
source share
11 answers

The question is whether it is really a good idea to β€œpollute” production code with Unit Testing data in the form of additional annotations. Personally, I voted against it.

+7
source share

It looks like you are looking for a programming language with more expressive expressiveness than standard Java. The tests you check fill in the blanks until the compiler can verify the semantics of the declarations.

I do not know a single tool that converts from those annotations that you offer into automated tests. This sounds like a nice TDD exercise, though, especially with the latest (-ish) compiler APIs.

+2
source share

tl; dr: Use code verification tools instead. They can do what you want, do not pollute your code and can easily integrate into the process of automatic assembly.

This is not like what should be handled with unit test. If you are categorical that each class should have a default constructor, a static code analyzer might be better. PMD definitely has a detector for this kind of thing. You can also force this kind of Checkstyle detector.

Both of them are easy to configure and can be part of your continuous integration process right next to your unit tests.

The most critical point:. This will allow you to implement the necessary functions without polluting the code base with additional annotations. This is the highlight for me when I look at this idea: you created a huge software management problem.

Imagine that the situation with modifying a class with a default constructor is unchanged (so that the objects are fully configured at build time). Don't remember to update all of these annotations in this class? Don't remember to change related annotations in other classes that call methods on this? And so on.

+2
source share

Agitar has a [commercial] product , AgitarOne , which generates JUnit tests. I'm not sure that it currently supports annotations, not in 2005 .

Jtest is another Java-oriented testr generator, and Parasoft also offers C ++ Test for creating C ++ unit tests.

I have never tested any of them; A few years ago I read a test document in C ++, but was not convinced.

+1
source share

Just like what you described to test for the presence of a default constructor, I used TestUtil to automatically test getters and setters. You can use either an XML file or JavaDoc tags to configure test parameters. Unfortunately, there is currently no option for annotations.

+1
source share

"process annotations ... and generate a test for me"

For a limited number of cases, this can be done to work. In general, however, it cannot work.

 @StandardTestForClassHierarchy1 @StandardTestForClassHierarchy2 @StandardTestForClassHierarchy3 @StandardTestForSomeOtherFeature4 @AspectFeature5 @AspectFeature6 @HasPublicDefaultConstructor @AspectX @AspectY class SomeClass extends SomeClassHierarchy implements SomeOtherFeature { } 

I cannot distinguish my unittest annotations from my actual annotations.

Do test annotations perform runtime behavior of my application?

  • If they describe runtime behavior, then they are for real AOP annotation, not test descriptions, but real annotations that actually generate code at runtime. And the annotation has its own test for mock classes.

  • If annotations do not describe runtime behavior, I now have this strange mess of non-functional and functional annotations. I do not see the meaning in non-functional annotations.

+1
source share
0
source share

EiffelStudio CDD has an interesting approach. While in debug mode, the IDE collects information about the methods being called and places the context and calls into unit tests. Failures are detected using Design by Contract. I know that there are some developments on contract extensions on top of Java, so this may be a good way.

0
source share

For your specific example, it might be better to use java APT (compilation of processing annotation time) to detect such problems much earlier during compilation.

0
source share

You can detect this at compile time using APT.

http://www.javaspecialists.eu/archive/Issue167.html

0
source share

I think you are unlikely to save anything. But you add complexity.

If I wanted to make sure there was a default constructor, I would add the following to my unit test.

 Foo foo = new Foo(); 
0
source share

All Articles