Alexa Skills Testing with Java
Tags: java, alexa, test, bdd
Published: 2021-02-21 13:13:44

---
title: Alexa Skills Testing with Java
published: true
description: Test your Java Alexa skills with real requests.
tags: ["java", "alexa", "test", "bdd"]
cover_image: https://source.unsplash.com/k1osF_h2fzA
---

## Test your Java Alexa skills

This tutorial will help you write automated test cases for Alexa skills written in Java. Using real-life JSON requests and verifying multi-turn interactions without deploying or hosting the skill.</br>

- [Alexa Skills Tester on Github][1]
- [Alexa Skills Test on Maven Central][2]
- [Sources for this article][3]

To use the [alexa-skills-tester][2], just add the following to your project:</br>
(replace _VERSION_ with the [current release][4]).

```xml
<dependency>
  <groupId>info.tomfi.alexa</groupId>
  <artifactId>alexa-skills-tester</artifactId>
  <version>VERSION</version>
</dependency>
```

## Example usage

Using a basic `Nice to meet you` custom skill:</br>
When launching the skill, it asks for the user name and then replies with a `Nice to meet you #name` prompt, ending the session.</br>

Let's take a look at the code for the skill.

### A launch request handler

```Java
public final class LaunchRequestHandlerImpl implements LaunchRequestHandler {
  @Override
  public boolean canHandle(final HandlerInput input, final LaunchRequest request) {
    return true;
  }

  @Override
  public Optional<Response> handle(final HandlerInput input, final LaunchRequest request) {
    return input.getResponseBuilder()
        .withSpeech("What is your name?")
        .withReprompt("Please tell me your name.")
        .withShouldEndSession(false)
        .build();
  }
}
```

### An intent request handler handling `MyNameIntent` with a `nameSlot`

```Java
public final class MyNameIntentRequestHandler implements IntentRequestHandler {
  @Override
  public boolean canHandle(final HandlerInput input, final IntentRequest request) {
    return request.getIntent().getName().equals("MyNameIntent");
  }

  @Override
  public Optional<Response> handle(final HandlerInput input, final IntentRequest request) {
    var name = request.getIntent().getSlots().get("nameSlot").getValue();
    return input.getResponseBuilder()
        .withSpeech(String.format("Nice to meet you %s!", name))
        .withShouldEndSession(true)
        .build();
  }
}
```

### A session ended request handler

```Java
public final class SessionEndedRequestHandlerImpl implements SessionEndedRequestHandler {
  @Override
  public boolean canHandle(final HandlerInput input, final SessionEndedRequest request) {
    return true;
  }

  @Override
  public Optional<Response> handle(final HandlerInput input, final SessionEndedRequest request) {
    return Optional.empty();
  }
}
```

### A utility class for constructing the skill

```Java
class NiceToMeetYouSkill {
  public static Skill getSkill() {
    return Skills.standard()
        .addRequestHandler(new LaunchRequestHandlerImpl())
        .addRequestHandler(new MyNameIntentRequestHandler())
        .addRequestHandler(new SessionEndedRequestHandlerImpl())
        .build();
  }
}
```

### Now, let's test the skill

> You should get familiarized with [Alexa's Request and Response JSON Reference][5]. This is, of course, should be considered common knowledge for skill developers.</br>

Let's create a couple of fake request JSON files to use in our tests.

#### launch_request.json

```json
{
  "version": "1.0",
  "session": {
    "new": true
  },
  "context": {
    "System": {}
  },
  "request": {
    "type": "LaunchRequest",
    "requestId": "amzn1.echo-api.request.fake-request-id",
    "timestamp": "2021-02-11T15:30:00Z",
    "locale": "en-US"
  }
}

```

#### my_name_intent.json

```json
{
  "version": "1.0",
  "session": {
    "new": false
  },
  "context": {
    "System": {}
  },
  "request": {
    "type": "IntentRequest",
    "requestId": "amzn1.echo-api.request.fake-request-id",
    "timestamp": "2021-02-11T15:31:00Z",
    "locale": "en-US",
    "intent": {
      "name": "MyNameIntent",
      "slots": {
        "nameSlot": {
          "name": "nameSlot",
          "value": "master"
        }
      }
    }
  }
}
```

#### session_ended.json

```json
{
    "version": "1.0",
    "session": {
        "new": false
    },
    "context": {
        "System": {}
    },
    "request": {
        "type": "SessionEndedRequest",
        "requestId": "amzn1.echo-api.request.fake-request-id",
        "timestamp": "2021-02-11T15:32:00Z",
        "locale": "en-US"
    }
}
```

Now, let's create some test cases leveraging the above JSON files to verify the interaction with the skill.

#### Test cases testing the `Nice to meet you` skill

```Java
final class SkillInteractionTest {
  @Test
  void verify_greeting_for_my_name_intent_as_a_followup_to_a_launch_request() throws IOException {
    givenSkill(NiceToMeetYouSkill.getSkill())
        .whenRequestIs(getClass().getClassLoader().getResourceAsStream("launch_request.json").readAllBytes())
        .thenResponseShould()
            .waitForFollowup()
            .haveOutputSpeechOf("What is your name?")
            .haveRepromptSpeechOf("Please tell me your name.")
        .followingUpWith(getClass().getClassLoader().getResourceAsStream("my_name_intent.json").readAllBytes())
        .thenResponseShould()
            .haveOutputSpeechOf("Nice to meet you master!")
            .and()
            .notWaitForFollowup();
  }

  @Test
  void verify_empty_response_for_session_ended_requests() throws IOException {
    givenSkill(NiceToMeetYouSkill.getSkill())
      .whenRequestIs(getClass().getClassLoader().getResourceAsStream("launch_request.json").readAllBytes())
      .thenResponseShould()
          .waitForFollowup()
          .haveOutputSpeechOf("What is your name?")
          .haveRepromptSpeechOf("Please tell me your name.")
      .followingUpWith(getClass().getClassLoader().getResourceAsStream("session_ended.json").readAllBytes())
      .thenResponseShould().beEmpty();
  }
}
```

That's it!</br>
I hope you'll find this tool useful.</br>

[1]: https://github.com/TomerFi/alexa-skills-tester
[2]: https://search.maven.org/artifact/info.tomfi.alexa/alexa-skills-tester
[3]: https://github.com/TomerFi/alexa-skills-tester-tutorial
[4]: https://github.com/TomerFi/alexa-skills-tester/releases
[5]: https://developer.amazon.com/en-US/docs/alexa/custom-skills/request-and-response-json-reference.html