How can I make gmaven-plugin groovy code cause maven to exit with a failure? - gmaven-plugin

I have a pom.xml file with some groovy code that is run by gmaven-plugin.
I would like to detect if a setting is missing and then have maven exit with 'BUILD FAILURE'.
I would prefer to be able to display an error message as well, but I can manually do that so no big deal.
This works but is very ugly (since it makes maven say "Exception: / by zero")
testResult = true
if (testResult) {
println "good keep going"
} else {
println "bad make maven fail"
someVal = 1.0 / 0.0
}

You can always explicitly throw an exception, e.g.:
throw new RuntimeException('build is failing! call the fire brigade!');

Related

Kotlin test not found, but only sometimes

Here is the test that I'd like to run:
#Test
fun testRequestFails() = runBlocking {
assertFailsWith(ClientRequestException::class) {
val result = httpClient.get<MyResponse>("$baseUrl/some/req/that/should/fail") {
header(API_KEY_HEADER, testApiKey)
}
}
}
httpClient.get is a suspend fun. When I try to run this with ./gradlew test or IDEA, the test is silently skipped! When I try to run this one specifically in IDEA, it says "No tests found for given includes."
However! I can make it findable and runnable by either assigning the result of assertFailsWith to a variable, or by adding assertEquals(1,1) to the end of the test.
I'm probably doing something wrong with coroutines, but in my head, if I write a test wrong, it should fail, not disappear
It's deeply troubling that a test can disappear without warning!
Is there a way to ensure the test is discoverable, without doing a useless variable assignment or a useless assert?
Is there a way to make giant alarm bells go off if a test fails to get discovered?
Snippets from build.gradle.kts:
tasks.test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}
}
dependencies {
testImplementation(kotlin("test-junit5"))
testImplementation("org.junit.jupiter:junit-jupiter:5.7.0")
testImplementation("io.ktor:ktor-client-mock-jvm:$ktorVersion")
testImplementation("io.ktor:ktor-server-tests:$ktorVersion")
}

How to write a test case method having System.exit() using Junit 5?

Scenario: Below negative scenario to be tested during Integration test. currently the test case getting failed due exit and not reaching to the test method.
example :
private void method1(int a){
try{
if(a == 0){
throw exception();
}else{
---
}
}catch(exceptionclass e){
System.exit(1);
}
}
Sound like a bad smell to me that calling a method on a object can cause JVM to exist. Normally it should be done in the main method.
So I would refactor your codes such that your testing object will throw a kind of Exception to indicate that some kind of fatal error happens such that the main method can catch it and terminate the JVM.
Then you can simply test that if it will throw this Exception from your test case.

Check all assertion and verifications when using mockk verification together with assertion libraries

I want the test to report all assertions and verifications. So both the mockk verification AND the the assertion library (in this case, KotlinTest) assertions should run and not shortcircuit.
In other words I don't want the test to stop ...
verify(exactly = 1) { mock.methodcall(any()) } // ... here
success shouldBe true // how can I check this line too
nor ...
success shouldBe true // ... here
verify(exactly = 1) { mock.methodcall(any()) } // how can I check this line too
How to do this? I am open to use just one tool if I can do both with it.
As per your comment, you said you are using KotlinTest.
In KotlinTest, I believe you can use assertSoftly for the behavior you want:
Normally, assertions like shouldBe throw an exception when they fail. But sometimes you want to perform multiple assertions in a test, and would like to see all of the assertions that failed. KotlinTest provides the assertSoftly function for this purpose.
assertSoftly {
foo shouldBe bar
foo should contain(baz)
}
If any assertions inside the block failed, the test will continue to run. All failures will be reported in a single exception at the end of the block.
And then, we can convert your test to use assertSoftly:
assertSoftly {
success shouldBe true
shouldNotThrowAny {
verify(exactly = 1) { mock.methodcall(any()) }
}
}
It's necessary to wrap verify in shouldNotThrowAny to make assertSoftly aware of it when it throws an exception

Automatically close PhantomJs after running script

I want to run PhantomJs scripts from my program, but since the scripts may not be written by me, I need to make sure PhantomJs exits after the execution are either completed or fails for any reason (e.g., invalid syntax, timeout, etc). So far, All I've read says you must always include the instruction phantom.exit() for PhantomJs to exit. Is there any way to automatically close PhantomJs after it executes a given script?
Thanks.
Create a file run-javascript.js:
var system = require('system');
try {
for (var i=1; i<system.args.length; i++) {
var scriptFileName = system.args[i];
console.log("Running " + scriptFileName + " ...");
require(scriptFileName);
}
}
catch(error) {
console.log(error);
console.log(error.stack);
}
finally {
phantom.exit();
}
Then to run your file myscript.js:
phantomjs run-javascript.js ./myscript.js
You have to include an explicit path for the myscript.js, i.e. ./myscript.js, otherwise phantomjs will look for the script as a module.
There are three execution scenarios that are handled here:
Successful execution, in which case phantom.exit() is called in the finally clause.
Error in the script being run, in which case the require function prints a stacktrace and returns (without throwing any error to the calling code).
Error running the script (e.g. it doesn't exist), in which case the catch clause prints out the stacktrace and phantom.exit() is called in the finally clause.

How to use #Rule in Selenium Junit to fail a test

I've got the following test which I have put a try catch around so it fails if actual value does not equal expected value:
try{
Assert.assertEquals(ExpectedCount, ActualCount);
}catch (Throwable t){
System.out.println ("Actual value is not equal to expected value");
}
Whenever I run this test it passes. However if expected=actual no message is printed, which is correct. If they are not equal then the message i printed. So the test logic is valid.
However the Juint test still passes.
I have been trying the incorporate the following but cannot get it to work:
(Added this at public level in the class of the test)
#Rule
Public ErrorCollected errCollector = new ErrorCollector();
Added:
errCollector.addError(t);
Under
System.out.println ("Actual value is not equal to expected value");
I am getting the error back "Rule cannot be resolved to a type" It does not give me any option to import.
I've been trying to find out how use the #Rule method to make the test to fail, but cannot figure it out.
As #oers says, the reason you can't find Rule is probably because your version of JUnit is too old. Try a later version, > 4.7.
The easiest solution to your problem is just to rethrow the exception that you're catching:
try {
Assert.assertEquals(ExpectedCount, ActualCount);
} catch (Throwable t) {
System.out.println ("Actual value is not equal to expected value");
throw t;
}
but you'll need to add throws Throwable to your test method. You have another alternative, to add a description to your assert:
Assert.assertEquals("Actual value is not equal to expected value", ExpectedCount, ActualCount);