How-to integrate gUnit ( ANTLR grammar testing ) in the NetBeans build cycle? - netbeans

Background: I am using NetBeans 7.1 and ANTLR 3.4. I have integrated java code generation in the NetBeans build script using the following tutorial: http://wiki.netbeans.org/Integrating_ANTLR_without_learning_Ant. I want to use automated tests for testing grammars. I have read about gUnit on the ANTLR site. There are, supposedly, two modes: one 'direct' method and another method which generates jUnit code. I have worked with jUnit before.
Question(s):
What method is advisable? Direct or via jUnit? Or perhaps both?
What should I do to integrate gUnit in the NetBeans build cycle?

My personal preference is to use JUnit since then you can run the generated tests like any other JUnit test in NetBeans.
To get the JUnit classes you need to run the org.antlr.gunit.Interp class (in the Antlr JAR) as a Java program with the -o flag. The result will be JUnit classes in the same dir as the gunit file. Add the generated source to your project and compile/run.

Related

Spock script not compiling automatically in Spring MVC project which is using Eclipse IDE

I am working on a Spring based web project (Eclipse as IDE), in which we want to introduce integration testing framework using Spock. This project is based on Maven. For this purpose I have installed Greclipse plugin and converted the existing project to groovy nature. Also configured Maven to run the test classes in src/test/groovy folder, and everything working fine and all the spock tests are running fine with run "Maven test" phase. Configured the build path to compile the groovy test files to target/test-classes.
The problem is coming when I run the Spock tests using Run -> Run configuration. It runs file with Junit runner using run configuration, it is picking up the compiled test class from target/test-classes. Whenever I modify the Spock test script file, it is not automatically generating the classes, Run with Run Configuration is always picking up the old compiled class.
How to force the script to compile so that I don't always need to Maven clean and Maven test, to force it compile and run.
I have found in some other threads talking about modifying the Groovy compiler options to "Enable script folder Support", both checking and unchecking is of no use either. Its not forcing the Spock script to compile (also tried enabling the same option in Eclipse Preferences global Groovy compiler option)
Any help is greatly appreciated.
Spock tests are implemented as a class extending Specification. So they are treated like any other Groovy class.
If in Eclipse under "Project" → "Build Automatically" is enabled, Eclipse compiles the classes automatically on every change. So if you change a Spock test in Eclipse, running it should always use the newest compiled version.
For automatic compilation to work correctly, the source has to be configured properly in Eclipse. Check via "Configure Build Path..." on your Eclipse project that under the "Source" tab the src/test/groovy/ folder is configured correctly.

How to run a JUnit test in eclipse?

I'm using eclipse and I have downloaded a JUnit test, and imported it into my package that I'm currently working on.
However instead of showing test.java it shows test.java.txt. And when I click 'Run as', JUnit test does not come up as one of the options.
Thank you in advance for your help.!
For the "Run as JUnit" option to appear, the file must be a Java file (extension .java).
JUnit scans your class for test methods. Depending on the version of JUnit that you're using, it can be done in multiple ways. JUnit before version 4.0 requires your test methods' names to start with the word test; with JUnit 4.0 onwards, you can use annotations to designate your test methods.
If your code contains JUnit annotations (such as #Test), but JUnit still complains that it can't find any test methods, then it means that you're running JUnit 3.x and not JUnit 4.x.

Beginner: How to do JUnit tests on GWT application?

I want to preform a JUnit tests on my application. I've never done JUnit testing before so I have a couple of (maybe trivial) questions:
Where should I put a test class? I came across with this thread:
Where should I put my JUnit tests?,
and the guy that answers the question is referring to maven projects, but I don't use maven. He explains (in the thread I linked above) that he puts the test class in a different location but in the same package. How can it be done in a GWT project?
How should I execute these tests once they are ready (where in the code to put the execution)?
You should begin by reviewing this: Unit Testing GWT Applications with JUnit.
The other thread is good and reflects the typical JUnit practice, and isn't specific to maven: use a mirror of your package tree under a directory called test. So for instance if your GWT EntryPoint module is located in this directory structure:
project/src/com/myproject/mypackage/MyEntryPoint.java
Then your test code will be here:
project/test/com/myproject/mypackage/MyEntryPointTests.java
If you've created your GWT project using webAppCreator then you should already have a test directory containing the package structure as described.
If you use webAppCreator to create your project, the project can be created with unit testing built-in like so:
webAppCreator -junit -out MyProject com.myproject.mypackage.MyEntryPoint
This will create a test target. If you're using Eclipse, then you should have a Run selection for: Run As -> GWT Unit Test for running your tests.
If you're using ant instead of Eclipse then this should run your unit tests:
ant test
If you didn't use -junit to create the project, the test targets are typically still there, just commented out. Search junit in build.xml to find the targets, and un-comment them.
You need to take a look at this article, MVP1 and MVP2, these are a pattern designs used to Unit Test your application in pure java environment, because using GWT Test Case runs very slow the patterns also has many advantages like separate the logic from the view so you can change the view for Android, for example.

Using JUnit #Rule in Eclipse's JUnit runner

Eclipse 3.7.2
I just implemented an #Rule in some JUnit 4 tests, but when I run them in Eclipse the MethodRule methods are not being called. It's like the Eclipse test runner doesn't recognize the #Rule implementations and doesn't do anything special with fields that are annotated with #Rule.
I even tried using a "Standard" MethodRule like org.junit.rules.TestName but it doesn't work properly (the test's names are not populated into the instance). Even the example test in the JavaDoc of TestName fails when run in Eclipse.
Is there some trick? Does Eclipse simply not support JUnit Rules?
It turns out that somebody had included a seemingly "required" JAR on the runtime classpath of the project in question. That JAR embeds, among other things it should not, the JUnit packages! So there is this JAR, named something innocuous like our_runtime_library.jar that has some app-specific code along with some unknown (but old) versions of JUnit, Spring, and who knows what else. When running the project as a Unit Test, Eclipse was picking up the JUnit in that JAR instead of its own version (as it should, project-specific libraries always take precedence), the (Eclipse) version that the project is built against.
What a mess; now off to figure out who deserves 50 lashings for this one.
It should work, at least in my Eclipse(in embeded JUnit 4.8).
So you could show your code.
Additional, JavaDoc says: Note that MethodRule is now deprecated, you should be using TestRule instead.

Creating ANT file for use in Eclipse and CruiseControl

I have a project in Eclipse that currently has no ANT build file, but is about to have one created (to facilitate building jars). I'd like to write the build file so that it would work for both. I was just going to write the full build in ANT but I noticed this line in an IBM Help doc.
The Java builder runs the internal
Eclipse Java compiler which in turn is
responsible for indexing your source
so that searching, refactoring and
many other features are available.
Thus it is not possible to replace the
internal Eclipse Java compiler by
using a project builder. You can
disable the Java builder and you can
control when the Java Builder runs
with respect to the project builders
that you define.
Do I need to write the ANT file so that it conditionally compiles (javac) only outside Eclipse? Are there other things I need to do to make ANT and Eclipse play nicely together?
There are maybe more sophisticated approaches, but here's what works for me. Just set up your Java project in Eclipse to output classes to the same dir as your Ant buildfile does. Let Eclipse compile your project using normal Java compiler.
During typical development, you will probably be changing Java source files and wanting to see that they compile. The Eclipse Java compiler will take care of this. Less frequently you will need to rebuild jars. Ant will take care of that. You can kick off the Ant build when you need to from the command line or from Eclipse.