using junit 4 in grails - testing

I'd like to use some JUnit 4 functionality in my grails testing, but currently grails tests run under JUnit 3. JUnit 4 can be used from groovy but replacing the JUnit jar within grails with a JUnit 4 one doesn't seem to enable the functionality I'm looking for.
Anyone know how I can get grails to run my tests with junit 4?

As of Grails 1.3.6, it looks like Junit 4 is supported incompletely. Integration tests are fine in Junit 4, but unit tests that extend GrailsUnitTestCase are limited to Junit 3. GrailsUnitTestCase extends GroovyTestCase which is still tied to Junit 3.
In the Groovy doc (http://groovy.codehaus.org/Using+JUnit+4+with+Groovy) it says that Junit 4 is supported, but notice the statement "Currently, there are no special Groovy extensions for JUnit 4". So you can use it, but none of the Groovy test extensions take advantage of it.
This is a killer for unit tests that need to use any of the Grails test extensions like mockDomain. I'm proceeding on the assumption that I'm effectively stuck with Junit 3.

You can use JUnit 4 with Grails as long as you're using Groovy 1.5+ and Java 5+:
http://groovy.codehaus.org/Using+JUnit+4+with+Groovy

Starting with Grails 1.3M1, JUnit 4 is onboard: http://www.grails.org/1.3-M1+Release+Notes

Grails does not support JUnit 4. They're looking into supporting it in Grails 1.3. You can force a dependency on JUnit 4, but the Grails tools might ignore your tests, run them incorrectly, or you might see other weird side effects.
To force a JUnit 4 dependency (at your own risk), open your grails-app/conf/BuildConfig.groovy file. In that file, modify the dependencies and inherits sections to include the following:
inherits("globals") {
excludes "junit"
}
dependencies {
test "junit:junit:4.7"
}
Then, in SpringSource Tool Suite, right-click your project and select Grails Tools -> Refresh Dependencies. This should replace junit 3.8.1 in your Grails Dependencies with junit 4.7.

Related

How to test JUnit Jupiter (JUnit 5) Extensions

Is there a way to integration test my JUnit Jupiter extension? Of course I can test a good cases of extension usage, but I would like to test things like:
Does it fail tests as expected?
Are the correct reports written on test end?
Is there some built in support for this?
There is no explicit Jupiter extension testing framework -- but with version 1.4 there'll be the Platform Test Kit that you may use to execute the Jupiter engine and your extension in one-go and assert the outcome of a test run.
For details see: https://junit.org/junit5/docs/snapshot/user-guide/#testkit and in addition to that documentation, find usages of the Platform Test Kit within the "platform-tests" project of JUnit 5. For example at: https://github.com/junit-team/junit5/blob/master/platform-tests/src/test/java/org/junit/platform/testkit/engine/ExecutionsIntegrationTests.java
You should not perform integration testing for your own extension of Junit. The Junit 5 API is very, very stable and I bet that your extension will work the same for the entire life of Junit 5. Integration testing is useful in a constantly evolving environment, but Junit only ever adds features and never changes them (unless there is a bug).
If you perform integration testing for your own Junit extension, it's like you're testing Junit itself. Why would you test Junit instead of your extension? Let Junit be tested by Junit developers, unless you think you found a bug. But testing Junit for the sake of testing Junit is a waste of your own time, even if unconsciously you think you're only testing your own extension.
So that kind of tests should be done on the long term by getting your users to report issues to you: "Hey, AwesomeExtension works with Junit 5.A.B but not with 5.X.Y", then find out why they say that and if the issue is not with your extension, then report with Junit.
However, make sure that you get your extension... unit-tested.

How to run junit Selenium tests in parallel at Sauce Labs together with AllureTestRunner?

We use AllureTestRunner (https://github.com/allure-examples/allure-gradle-junit-example/blob/master/src/test/java/ru/yandex/qatools/allure/junit/BaseTest.java) to run junit selenium tests and generate allure reports.
And also we need to run them in parallel at Sauce Labs. There is runner for this: ConcurrentParameterized(https://github.com/saucelabs-sample-test-frameworks/Java-Junit-Selenium/blob/master/src/test/java/com/yourcompany/Tests/TestBase.java).
JUnit does not allow to use several #RunWith annotations.
Is it possible to combine two runners?
The problem is that there is no way to add listener to JUnit using Gradle. There are few workarounds available. The first one is use custom Runner that adds listener, the other one - use AspectJ magic to get this done. For more details you can see the following Gradle issue https://github.com/gradle/gradle/issues/1330
At the moment there is a Gradle plugin https://github.com/d10xa/gradle-allure-plugin that can add listener to JUnit using AspectJ. So simply remove AllureTestRunner and use plugin instead.
Update
New Allure Gradle plugin that supports Allure 2 is available now. See the docs https://docs.qameta.io/allure/2.0/#_gradle_3 for more details.

How do I setup Cucumber with Grails 3.2.4 for Testing with Grails?

I've been trying to do BDD testing with Grails 3.2.4 and I've had difficulty integrating Cucumber(https://cucumber.io/) with Grails 3.2. If it can be done then how do I setup cucumber for testing?
I've tried setting up with gradle cucumber but it doesn't seem to be working properly and there isn't much of a guide to getting it setup. See https://github.com/samueltbrown/gradle-cucumber-plugin for gradle cucumber and https://github.com/hauner/grails-cucumber for Grails cucumber which is not compatible with Grails 3.2.
As far as I can understand you are able to use Java libraries. Looking in the docs tells me that it is possible to use JUnit for unit testing.
This leads me to think that you should be able to use the Java Skeleton provided by the Cucumber team.
My approach would be to clone it and get it to run. It should run out of the box. Then include it in your test suite for your project.
I've got cucumber to work with just a dummy test class (not a Grails domain class), but when I try to test a real domain class to make sure the attribute constraints are adhered to, it doesn't work. The class is not seen as a domain class and I'm told GORM has not been initialized. I'll be one step closer to getting Cucumber to work with Grails 3 once I find out how to initialize GORM in the Cucumber step defintions.

JUnit Version in Netbeans 7.3, 7.4

Why latest versions of NetBeans (7.3, 7.4) do not have JUnit 4.11 ?
Rather they come with JUnit 3.8.2 and 4.10
In fact as seen in Maven Repository page of JUnit, 4.11 was last updated in Nov 2012, which is almost a year old !
My guess is, as NetBeans has very good support for Maven, most developers are including JUnit explicitly as a dependency within their POM files. I don't think I've used the copy of JUnit in NetBeans for several years.
As for the age of JUnit 4.11, the JUnit API and implementation are pretty stable and, as time goes on, I use Hamcrest and Mockito to augment what JUnit provides.

Howto handle a maven2 junit test project with artefact dependencies?

I have a project 'ABC' with the main code and junit tests. I do have the requirement that i can execute the set unit tests against a older version of the product artefacts.
To solve this requirement i would create a maven project which only contains the junit tests.
Another maven product builds my product code and places the artifact into the repository.
Now i could launch my tests against any product build by changing the build dependency within the junit test project.
Is this a good solution? Are there perhaps better solutions to solve this requirement?
I think thats a pretty good approach. You could create a profile for each old version and then activate them via the profile name and test different old versions without having to change the pom file for each run. You could then also run the different profiles separately scheduled on a continuous integration server...