I'm working on a service class that needs to process some sort of data payload. In my automated tests, I'm adding some mock data to check the behavior of the service for different inputs. I need to extract these mock data to several files so I can reuse them for other tests. Where do I put such file in a Grails (2.4) app? By convention, most Java projects have src/test/resources for the purpose, but Grails doesn't seem to consider that.
If you are writing unit tests you can put them under test/unit/resources and if you are writing integration tests you put them under test/integration/resources.
Related
When I create a jenkins job via the API, I use a previous job's config.xml, make my modifications and then make the POST call to create the job.
My questions is, is there a way to generate this programmatically? I.e. is there a structure of a config.xml, what XML entities it should have, what values, etc so I can write a small module to generate one and send it to the jenkins API call?
I don't think there's any mandatory XML entities. Submitting an empty structure should result in a job that has default values for all settings.
What you want to do is exactly what's done by the Jenkins Job Builder. It provides a YAML-based framework for creating Job configuration XML files and submitting them to Jenkins. It's a common alternative to the Job DSL plugin. I wouldn't recommend to re-implement such a solution yourself -- handling all the plugin-specific XML configuration parts will be a nightmare.
We create our jobs using Job DSL plugin. You can try the playground http://job-dsl.herokuapp.com/.
At first, it seems that it is hard to learn, but after the first seed job, it is much better.
When we started writing our scripts we were afraid that there will not be suitable API methods for our needs. It turned out that we had one such case, which was solved using the configure block.
Get started guide here.
I'd like to write a custom CodeNarc rule that validates some aspects of Grails integration test classes. e.g. Spock integration tests should extend IntegrationSpec; not Specification.
But to do this, I need to filter on integration tests while visiting the classes in the rule, and I don't know how to do that.
One idea was to look at the source file path to see if it's in test/integration, but I don't know if it's possible to get the source path of a file.? Any other ideas?
You can definitely limit rule application based on path (as well as class name). Take a look at the configuring rules CodeNarc page.
In particular, you're probably looking for the applyToClassNames and doNotApplyToClassNames properties or the applyToFileNames and doNotApplyToFileNames properties, which should be available on any of the built-in Rules and any custom rules that extend the provided AbstractRule class.
I've got a Grails plugin that exports domain objects so that several applications can share the same schema. We have a few SQL scripts for setting up some complex triggers, views and other functions that just don't really belong in GORM/Hibernate, at least not elegantly. I'd like to store the scripts inside the same project. Is the "scripts" folder (the one containing _[Un]Install/Upgrade.groovy) the best place for this? I saw a StackOverflow answer that was building a catalog from scripts stored in grails-app/conf/sql. But I'm not actually trying to execute them from within the a project.
The absolute best solution for anything database related is to use the database migration plugin. This way you can ensure that any database your application is pointed to (dev, test, prod, etc.) will have the same information/schema/functions/procedures etc.
Personal preference. I usually add a 'database' dir for all that kind of stuff. The 'scripts' dir is for Grails scripts, at least in 1.x and 2.x. See Creating Gant Scripts or the create-script command for more on those. In Grails 3 these kind of scripts have been moved to src/main/scripts.
My Jenkins job runs many tests that create log files. In case of failure, I want to look at the log of the failed test. I'd rather use Jenkins web-server to do it, even have a link in the email it sends me.
Is there any plugin that can do it? Or maybe another way?
You provide few details in your question, so it is impossible to give specific advice. In a general level: this is already possible. When your test framework creates JUnit XML files with test results, the test output can be included between the <failure> and </failure> tags. Usually test frameworks should take care of this automatically, so you are probably not using a test framework and are manually generating the XML files containing test results?
I recommend you adopt some test framework. It is usually well worth the effort.
My goal is to write test for my own Grails plugin. steps taken so far:
Step - Create separate grails app (in our case, testApp) inside
the Grails Plugin 'test' folder
Step - Make the plugin to be
inline for the application - testApp by adding following Line in BuildConfig.groovy
grails.plugin.location.PluginName = '../../../../PluginName'
The issue is that in the Unit test, the Service (from the inline plugin) are not injected like it is for regular app in Unit Test. Here is sample test:
#TestFor(SampleService)
class SampleServiceUnitSpec extends UnitSpec {
def "Sample Test"(){
setup:
def test = 'ok'
when:
test = "changed"
then:
assert service
assert "changed" == test
}
}
The service is injected if this test is run from grails app and service is part of the app. The service is not injected if this test is run from the testApp with inline plugin containing the service. The service is not injected if the test is plugin unit test and the service declared in the plugin.
How to inject service so it can be tested? Any good docs,posts.etc on how best test Grails plugins? Thank You
Why you want to create a unit test for your plugin service inside an application. Shouldn't be better your application just worry with his own code?
I suggest you to separate your concerns: plugin classes should be tested in the plugin, and application classes in the application. For the point of view fo the application, assume that your services "should just work", because they are well tested in the plugin.
Also, I advise against creating an application inside your Grails plugin. The test folder is used only for your test classes and nothing more.
If you want to maintain only the last version of your plugin, you can create just one application (in another folder, outside your plugin) and always update there.
If you need to maintain more than one version, or more than one Grails version, then maybe a Groovy script that create your test application with an specified version will be a better approach.