Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

sample shows how you can test your custom buildSrc logic with a JUnit4 test #1302

Closed
wants to merge 2 commits into from

Conversation

abendt
Copy link

@abendt abendt commented Dec 20, 2018

Context

While learning how to use the Kotlin DSL for Gradle I found the samples to be immensely helpful. However I missed a sample that demonstrated how to unit-test your custom logic in buildSrc with. I think other users also could benefit from some this sample.

#1087

Contributor Checklist

  • [x ] Base the PR against the develop branch
  • [x ] Make sure that all commits are signed off to indicate that you agree to the terms of Developer Certificate of Origin.
  • Provide integration tests to verify changes from a user perspective
  • [x ] Provide unit tests to verify logic
  • Ensure that tests pass locally: ./gradlew check --parallel

…ogic in buildSrc using JUnit4 Unit tests.

Signed-off-by: Alphonse Bendt <[email protected]>
@abendt abendt changed the title this sample project demonstrates how you can test your custom build l… sample shows how to test your custom buildSrc logic with a JUnit4 test Dec 20, 2018
@abendt abendt changed the title sample shows how to test your custom buildSrc logic with a JUnit4 test sample shows how you can test your custom buildSrc logic with a JUnit4 test Dec 20, 2018
====================

Sample project setup to show how you can test your _buildSrc_ custom build logic using
Junit4 Unit tests.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Junit5 is already released.
I would recommend to use an exmaple with Junit5...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we still use JUnit4, this is why the sample is based on it.
I'd be happy to provide more samples e.g. using JUnit5 or Spek if there's interest.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@StefMa
is it acceptable for you to keep the junit4 example or would you prefer to have a junit5 one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not a maintainer of this Repo nor I work at gradle 🙃. I, personally, would show an example with junit 5 because it's the latest version. There is literally no need to stick at junit 4.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks.

I have created a new PR (#1317) and will close this one.

Sample project setup to show how you can test your _buildSrc_ custom build logic using
Junit4 Unit tests.

`./gradlew hello` this task uses a build helper function from _buildSrc_. The code inside _buildSrc_ will be compiled and tested prior to the execution of this task.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link the buildSrc in that markdown file: [buildSrc](buildSrc/)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -0,0 +1,12 @@
import tools.sayHelloTo

tasks {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a single task the TaskScope is a little bit out of bounds.
I would recommend to use either val hello by tasks.creating {} or directly tasks.register("hello") {}..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

dependencies {
testCompile("junit:junit:4.12")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compile is deprecated. Use implementation

@@ -0,0 +1,3 @@
package tools

fun sayHelloTo(name: String) = "Hello $name"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class of that name (and package) is a little bit missleading when here is just an "sayHello" function inside.
Renaming the package to greeting and the filename to Greeter.kt.
As I remember correctly Gradle use these convention in mutliple other samples too..

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Signed-off-by: Alphonse Bendt <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants