Unit testing a non-class Groovy function using Gradle - unit-testing

I was given a Jenkins Library project, and I'd like to write some unit tests for it. I'm currently using the Jenkins Pipeline Shared Libraries Gradle Plugin which sets up source paths for unit and integration tests.
The source file I want to test is located at src/org/example/pipeline/Terraform.groovy and looks like:
package org.example.pipeline
def terraformNewWorkspace(String env, Map config) {
def tfWSCreate = """
#!/bin/bash
set -e
terraform workspace new ${env} || true
terraform workspace select ${env}
terraform workspace list
"""
if (config.currentWorkingDirectory != null) {
dir("${config.currentWorkingDirectory}") {
sh(returnStatus: false, script: tfWSCreate)
}
} else {
sh(returnStatus: false, script: tfWSCreate)
}
}
and I've created a test/unit/groovy/example/PipelineTests.groovy which looks like the following:
import static org.example.pipeline.Terraform.terraformNewWorkspace as terraformNewWorkspace
class TerraformUnitTests extends GroovyTestCase {
void testNewWorkspace() {
terraformNewWorkspace("dev", [currentWorkingDirectory: "/tmp/test"])
}
}
However I get a groovy.lang.MissingMethodException at PipelineTests.groovy when I try to execute this using gradle test. I've also tried the following as some documentation seems to indicate functions that are not part of a class get added in Groovy to a class with the name of the file, but I get the same exception about a missing method.
import org.example.pipeline.Terraform
...
t = new Terraform()
t.terraformNewWorkspace("dev", [currentWorkingDirectory: "/tmp/test"])
What is the correct way to import and test this individual function using gradle/groovy?

It was a simple syntax error. I forgot to use def for my variable:
import org.example.pipeline.Terraform
class TerraformUnitTests extends GroovyTestCase {
void testNewWorkspace() {
def t = new Terraform()
t.terraformNewWorkspace("dev", [currentWorkingDirectory: "/tmp/test"])
}
}

Related

Testing that a plugin function is called in jenkins shared library

I'm trying to write a unit test for a util function in a Jenkins shared library. The util function, calls the Office365Connector plugin in Jenkins.
My util function looks like this:
#!/usr/bin/env groovy
import pe.ui.BuildNotificationSettings
import pe.ui.BuildStates
def call(BuildNotificationSettings options, BuildStates status) {
options.teamsChannelWebhookUrlList.each {
office365ConnectorSend (
status: status.toString(),
webhookUrl: "$it",
color: BuildStates.valueOf(status.toString()).color,
message: "Build ${status.toString()}: ${JOB_NAME} - ${BUILD_DISPLAY_NAME}<br>Pipeline duration: ${currentBuild.durationString}"
)
}
}
The use case that I'm trying to test is that the office365ConnectorSend function is called.
I've tried the following approach:
import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification
import pe.ui.BuildNotificationSettings
import pe.ui.BuildStates
public class _sendTeamsMessageSpec extends JenkinsPipelineSpecification {
def _sendTeamsMessage = null
def setup() {
_sendTeamsMessage = loadPipelineScriptForTest("vars/_sendTeamsMessage.groovy")
def office365ConnectorSend = Mock(office365ConnectorSend)
}
def "returns without sending no build notification settings are passed" () {
given:
def options = new BuildNotificationSettings(
shouldSendNotification: null,
teamsChannelWebhookUrlList: null
)
when:
def result = _sendTeamsMessage(options, null)
then:
0 * explicitlyMockPipelineVariable("office365ConnectorSend")(_)
}
}
running this on Jenkins gives me a java.lang.IllegalStateException:
There is no pipeline variable mock for [office365ConnectorSend]., what am I doing wrong in this approach?
After following this thread, I ended up calling the explicitlyMockPipelineStep function on the office365ConnectorSend plugin function.
This made the function visible in the tests and I was able to resume testing.

How to add unittest for Kotlin code in the same file and run it?

I have some code like this:
class Solution {
fun strStr(haystack: String, needle: String): Int {
return haystack.indexOf(needle)
}
}
In Python normally I can add some tests in the same file and add something like:
<some tests above here>
if __name__ == '__main__':
unittest.main()
To run the unit tests. How do I achieve the same thing in Kotlin ?
The reason why tests are normally put into a separate module in Kotlin/Java projects is that, usually, tests need some additional dependencies that do not make sense for production code, like JUnit or other libraries. Also, a test written in the same file would be compiled into a class that is a part of the production code output.
In a project that is published and used as a dependency for other projects, consider not mixing production and test code.
You can, of course, add those test dependencies to the production code. As an example for JUnit, add the dependency (in a Gradle project: dependencies { compile 'junit:junit:4.12' }, in an IntelliJ project: see the reference), then just add a testing class with a #Test function:
import org.junit.Test
import org.junit.Assert
class Solution {
fun strStr(haystack: String, needle: String): Int {
return haystack.indexOf(needle)
}
}
class SolutionTest {
#Test
fun testSolution() {
val text = "hayhayhayneedlehayhay"
val pattern = "needle"
val result = strStr(text, pattern)
Assert.assertEquals(8, result)
}
}

Unit test fails

I am new in Grails, I want to write unit tests for services using Spock. However I have the following issue.
import grails.transaction.Transactional
#Transactional
class BService {
boolean createB(){
return true
}
...
}
For this class I wrote the following test:
class BServiceTest extends Specification {
def "test createB"(){
given:
def service = new BService()
when:
def boolean temp
temp = service.createB()
then:
temp == true
}
}
The error I am getting when I run this test is the following:
java.lang.IllegalStateException: No transactionManager was specified. Using #Transactional or #Rollback requires a valid configured transaction manager. If you are running in a unit test ensure the test has been properly configured and that you run the test suite not an individual test method.
and it shows in GrailsTransactionTemplate.groovy:60
I would really appreciatie if anyone can give me a hint.
Add a #TestFor(BService) annotation to your unit test and use the instance of the service that it automatically provides. See http://grails.github.io/grails-doc/3.0.x/guide/testing.html for more information on testing.
Thank you ataylor for your reply. However I did a mistake, so I am now ansewring my own question.
First of all, the name conversion is wrong. When I create a service in grails there is automatically set a unit-test for this, so in that case I would have:
#TestFor(BService)
class BServiceSpec extends Specification {
def setup() {
User u = new User(1)
u.save()
}
def cleanup() {
}
def "test something"(){
}
}
In this case when I write a unit test, it runs.
The test that I had before was a functional test where I could not pass objects from the domain, so I had an error of the transactional manager.

Grails build-test-data Plugin - Can't Find TestDataConfig.groovy

I'm trying to utilize the build-test-data plugin in my Grails (v2.4.3) app to assist with test data creation for unit testing, but while running my unit tests the plugin cannot find TestDataConfig.groovy to load my specified values (for unique constraint tests, etc).
I've installed the plugin via including it in BuildConfig.groovy:
plugins {
...
test ":build-test-data:2.2.0"
...
}
I've ran the following command to create the TestDataConfig.groovy template, which places the file at \grails-app\conf\:
grails install-build-test-data-config-template
I've followed the general instructions on the plugin wiki to come up with a properly formatted file:
testDataConfig {
sampleData {
'<path>.User' {
def a = 1
username = { -> "username${a++}" }
}
}
}
(Where path is the fully-qualified class name.)
In my tests, I am using the following general format:
import grails.buildtestdata.TestDataConfigurationHolder
import grails.buildtestdata.mixin.Build
import grails.test.mixin.TestFor
import spock.lang.Specification
import spock.lang.Unroll
#TestFor(User)
#Build(User)
class UserSpec extends Specification {
def setup() {
mockForConstraintsTests(User)
TestDataConfigurationHolder.reset()
user = User.buildWithoutSave()
}
#Unroll
void "test #field must be unique"() {
given: 'a User exists'
user.save(flush: true)
when: 'another User is created with a non-unique field value'
def nonUniqueUser = User.buildWithoutSave()
nonUniqueUser."$field" = user."$field"
then: 'validation fails and the field has a unique constraint error'
!nonUniqueUser.validate()
nonUniqueUser.errors.errorCount == 1
nonUniqueUser.errors.getFieldError("$field").code == 'unique'
where:
field << ['username', '<some other field>']
}
}
But, when the test is run (using IntelliJ IDEA) TestDataConfig.groovy cannot be found via the following method in the plugin:
static Class getDefaultTestDataConfigClass() {
GroovyClassLoader classLoader = new GroovyClassLoader(TestDataConfigurationHolder.classLoader)
String testDataConfig = Holders.config?.grails?.buildtestdata?.testDataConfig ?: 'TestDataConfig'
try {
return classLoader.loadClass(testDataConfig)
} catch (ClassNotFoundException ignored) {
log.warn "${testDataConfig}.groovy not found, build-test-data plugin proceeding without config file"
return null
}
}
So the test continues on without a config file and I do not get uniquely generated data sets.
I've even tried explicitly including the file in Config.groovy:
grails.buildtestdata.testDataConfig = "TestDataConfig"
But, the same method in the plugin shows that Holders.config? is null.
I've looked at a few solutions to a similar problem here on StackOverflow with nothing working in my case; I cannot figure out how to get my app to detect the presence of the TestDataConfig.groovy file.
Any ideas? Thanks so much!

Grails 2.1 Unit Testing Command Object mockForConstraintsTests not working?

I have used manually written as well as Grails generated Unit tests for this command object:
package myapp
#grails.validation.Validateable
class SearchCommand {
String basisBuild
String buildToSearch
static constraints = {
basisBuild(blank: false)
}
}
After having my hand written unit test fail I used Grails:
create-unit-test myapp.SearchCommand
I filled in the Unit Test, and made an assertion that should pass per documentation on mocked constraints:
package myapp
import static org.junit.Assert.*
import grails.test.mixin.*
import grails.test.mixin.support.*
import org.junit.*
#TestMixin(GrailsUnitTestMixin)
class SearchCommandTests {
void setUp() {
mockForConstraintsTests(SearchCommand)
}
void tearDown() {
// Tear down logic here
}
void testSomething() {
SearchCommand commandUnderTest = new SearchCommand()
commandUnderTest.validate(basisBuild: "")
assertEquals "blank", commandUnderTest.errors['basisBuild']
}
}
Why am I getting this failure?
grails> test-app
| Running 9 unit tests... 9 of 9
| Failure: testSomething(com.siemens.soarian.sf.gap.SearchCommandTests)
| java.lang.AssertionError: expected:<blank> but was:<null>
at org.junit.Assert.fail(Assert.java:93)
I believe I found the grails supported way to unit test Command objects in grails 2.0. You need to use mockCommandObject provided by the ControllerUnitTestMixin.
Credit to Erik
http://www.jworks.nl/2012/04/12/testing-command-objects-in-grails-2-0/
EDIT
Using validate() appropriately and mockForConstraintsTest should work if the patch mentioned in the existing Grails bug is in place (Thanks to #codelark for bringing that up). In order to test the command object from a Web App standpoint (using controller) the below information would be helpful.
Test Command Object Using Controller action:-
A command object is only deemed as such when it is used as a parameter in one of the action method inside a controller. Refer Command Objects (Warning NOTE).
Use SearchCommand in an action method, you should be able to assertEquals.
Sample:
void testSomething() {
YourController controller = mockController(YourController) //Or instantiate
SearchCommand commandUnderTest = new SearchCommand ()
//Note the usage here. validate() does not take parameters
commandUnderTest.basisBuild = ''
commandUnderTest.validate()
//Call your action
controller.searchCommandAction(commandUnderTest)
assert response.text == 'Returned'
assertEquals "blank", commandUnderTest.errors['basisBuild']
}
YourController's action:-
def searchCommandAction(SearchCommand sc){
render "Returned"
}
Note:
With out the patch from the grails bug we see the below error in #Grails 2.1.4, 2.2.0 & 2.2.1
I get an error when I only correct the validation and use mockForConstraintTests without using controller action:
You are using the validate method incorrectly. You never set the field on the class, so the field is null, not blank. Try changing your test as follows:
void testSomething() {
SearchCommand commandUnderTest = new SearchCommand()
commandUnderTest.basisBuild = ""
assertFalse commandUnderTest.validate()
assertEquals 'blank', commandUnderTest.errors['basisBuild']
}
Edit: There is also a grails bug when testing command classes that use the #Validatable annotation. There are some workarounds in the bug commentary.