Beginner: How to do JUnit tests on GWT application? - gwt

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.

Related

Tycho-surefire throws java.lang.IllegalAccess Exceptions

I have a maven tycho build (which is running fine) and I want now to add the already existing unit tests to the build setup.
The unit tests are organized in a way that each plugin has its test fragment.
All tests are called from a single test suite which contains the tests suites of the fragments and these are containing the actual unit test classes. This is possible due to the Eclipse-ExtensibleAPI: true setting in Manifest.MF
Each test fragment has its pom.xml which contains true to avoid executing tests twice. The test fragments are set as in main pom.xml.
The main test plugin (which contains the main test suite) contains in its pom.xml an target platform extension (which is a feature containing the test fragments).
Now as soon as a tests is called which is written to test a protected method the tycho-surefire throws an java.lang.IllegalAccessException.
In Eclipse the unit tests run fine (as unit tests, not as plugin unit tests!).
I assume that somehow the classes and the test classes are loaded with different class loaders?
Otherwise, since the test is contained in a fragment to the host plugin and the Eclipse-ExtensibleAPI: true should take care that the visibility is such that this should not happen?
Therefor, I would expect tycho to detect fragments and that it is loading them in a way that they have the same visibility?!
Is there a way/strategy to avoid this behaviour?
I know that tycho-surefire tests are executed in an OSGi environment.
But what does that mean regarding class loading of fragments and the IllegalAccessException?
Any help is highly appreciated!
Thanks in advance!
I found the reason why it was not working.
There where two plugins (one containing the ui code, one the domain model code) and one test fragment. The test fragment was referring to the ui code plugin but contained also tests which tested classes from domain model code.
The packages in the test fragment where named the same as where the packages within the two plugins. I can only make a guess but I think that is why it was working with junit called from within eclipse.
Within the OSGi environment the tycho-surefire tests are running this was not working anymore.
To solve this I split the one test fragment into two (one for each plugin), named the packages of the test classes the same as the packages in the plugin and then it worked as expected.
This is also reflected in my short example project on github.

How can you run junit5 tests across multiple projects in Eclipse?

I am looking at migrating the unit tests for a set of projects from JUnit4 to JUnit5. This is proving to be generally pretty straightforward. However there is one problem outstanding: how to run unit tests across all projects from within Eclipse.
With JUnit4 there is a simple solution using ClassPathSuite: create a new project which has all the other projects on it's classpath and add a single class with no methods:
#RunWith(ClasspathSuite.class)
public class RunAllTests {
}
This still works fine with tests written for JUnit4 and run with JUnit5 using the vintage engine. However once tests are converted to native JUnit5 ClassPathSuite no longer finds them.
Eclipse Oxygen (v4.9.0) Test configuration only allows tests to be configured within the confines of a single project, package or source folder so does not appear to offer a solution to this problem.
Any suggestions?
JUnit 5.8 introduced #Suite and suite engine.
This functionality seems to be similar to what ClasspathSuite JUnit 4 extension offers.
Eclipse seems to be working pretty well with JUnit 5 suites:
All the tests on the screenshot come from different projects.

My jar won't add my junit tests

The contents of my jar file does not contain my junit tests. I tried creating a main class that calls my tests with "Junit Core" but that too doesn't get added to my jar file. What am I missing?
Your production package (your jar) is not supposed to contain the test code (your unit tests).
Test code is supposed to be invoked only when the tests are been executed.
You shouldn't do it, but if you want the test code to be included in your package just put it in the same root folder of your production code. Ex: if you are using Maven, your folders structure might be something like this:
project_name/src/main/java/
-> production code
project_name/src/test/java/
-> test code
In that case, change it to:
project_name/src/main/java/
-> production code
-> test code
Be aware that doing that you will have some problems, like the tests not executing during a normal build. We might help you better if you put a question asking how to accomplish your goal (what is your goal?).
Update based on the goal explained in the comments: Would be better if this tool (Silk Central) could run your tests using Maven or Ant, and I'm pretty sure it can.
I've been used different tools to run my Junit (or other XUnit frameworks) tests. What I do is to configure the tool to:
Get the source code from the version control.
Run the build/tests by calling Maven or Ant installed along the tool.
Read the Junit outputs.
Sometimes the tool has its own integration with Junit and you don't have even to use Maven or Ant.
I mean, your central build or tests tool should be able to run your tests just like you do in your own machine.
Take a look on this or search by "Silk Central junit" in the web:
http://community.microfocus.com/borland/test/silk_central/w/wiki/465.zero-maintenance-junit-testing-using-classpathsuite-in-sctm.aspx

How to run a ScalaTest form a different project in Eclipse?

I have two Scala projects open in Eclipse (the Scala IDE, more precisely). One project is the main project and the other is where I want to place my tests.
If I place a test file (i.e. a class Test1 extends FlatSpec) in my main project, I can right click on the file and choose "Run Configurations", and it offers me to "Run as ScalaTest - File". However, if I copy that same test file to the second project, right clicking doesn't offer that option. Why is that?
If I am not explaining the situation with enough detail, I'll be glad to add more information.
From ScalaTest guide:
Using ScalaTest in a Scala project
To use ScalaTest in your Scala
project, you must download ScalaTest and include it in build path of
your project.
If it is in your build path, you could just create run-configurations for that particular class.
I've had problems in the past with eclipse and not seeing run java application. I just created the run configurations myself.

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

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.