Skip to main content

Annotations

To use annotations you must use an extension for your test framework as well. The extension is called by your test framework when interesting events happen and takes care of parsing the annotations into the message you want to sent.

Extension loaded?!

This page assumes you have the appropriate extension added to your classpath and loaded by your test framework. If not, go to Extensions.

@Message

This is the main annotation, used to define the message itself.

AttributeTypeDefault ValueDescription
titleString(Required)The title of the message.
bodyString""The content of the message.
detailsArray<String>[]Additional details in key-value format.
linksArray<String>[]List of related links.

details and links expect an array with an even number of items, alternating key and value.

Examples

@Message(title = "The flux capacitor failed")
@Test
fun `title only test`() {
//...
}

@Message.Channel

If you don't configure a default channel, Alerticorn requires that you specify a channel name. The value can be a webhook url, or the name of the channel.

When using the name of the channel it tries to resolve the channel name to a webhook url, via the environment variables configuration.

The example below expects these two environment variables:

  • AC_SLACK_CHANNEL_GENERAL="https://hooks.slack.com/...
  • AC_SLACK_CHANNEL_ANOTHER_CHANNEL="https://hooks.slack.com/...
note

Annotations at the method level always take precedence over class level annotations.

Example of overriding the channel at method level
@Message.Channel("general")
class MyTest {

fun GeneralTest() {
//this sends to general
}

@Message.Channel("another_channel")
fun SpecificTest() {
//this send to another_channel
}
}

@Message.Platform

Alerticorn will try to send the message to the default platform, or if not configured, to the first notifier it detects.

If there are notifiers on the classpath for multiple platforms, the annotation can be used to override the default platform for a specific class or method.

Example of overriding the platform at method level
@Message.Platform("slack")
class MyTest {

fun GeneralTest() {
//this sends to general
}

@Message.Platform("teams")
fun SpecificTest() {
//this sends to another_channel
}
}

@Message.Template

In some cases you may feel limited by the possibilities of the Message annotation. You can create template functions that construct an AlerticornMessage.

Example of using a template to build a message
    @Test
@Message(title = "Template test")
@Message.Template("myFavTemplate")
fun testt() {
throw Error("something failed")
}

companion object {
@JvmStatic
@BeforeAll
fun setup(): Unit {
MessageProvider.store("myFavTemplate") { title, throwable ->
AlerticornMessage(
title = "The title was $title",
body = "This went wrong: ${throwable?.message}",
details = mapOf("fun" to "times"),
links = mapOf("google" to "https://www.google.com"),
throwable,
)
}
}
}

@Message.Events

If you do not want to send messages for any test event, but only in restricted cases, you can use @Message.Event. You can specify one or more of: ANY, EXCEPTION, FAIL, PASS, SKIP, ABORTED, DISABLED.

Example to send a message only when the test fails
    @Test
@Message(title = "The test")
@Message.Events([Event.FAIL])
fun theTest() {
assertEquals(true, false)
}
ANY and EXCEPTION

When using a test framework extension, a failed test is also considered an exception. You will be notified for any failed test when using EXCEPTION.

ANY encompasses all events and is considered equivalent to NOT using the Message.Events annotation (i.e. ANY is the default).