0
package johnny.me.controller;

import com.sparkpost.Client;
import com.sparkpost.model.responses.Response;
import com.sparkpost.exception.SparkPostException;
import org.springframework.stereotype.Service;

/**
* Sparkpost class sends the email using
* the sendmessage methood
*/

@Service public class SparkPostController {

    String API_KEY = "API KEY";
         Client client = new Client(API_KEY);
         private static String from_email = "me@yahoo.com";
         private static String subject = "Time equation";
         private static String text = "";

    public Response sendMessage(String recipient, String message) throws SparkPostException {
         return client.sendMessage(from_email,recipient,subject,
                 text,message);
    }

}

This my sparkpost controller class. Trying to make a unit test/ mock to send message. Also known as regression test Please be very specific fairly new to unit testing. Have my class made under the testing folder.Thats all i got.

Johnny
  • 1
  • 1
  • 2

1 Answers1

0

I am the author / maintainer of the SparkPost java library you mentioned above.

The usage case you've shown above is the "simplest" usage of the SparkPost Library. For code like yours it is common for folks to create an interface with the "sendMessage(...)" function you have defined. When unit testing have spring (or the test) inject your "stub" version of the class instead of the production one. But in production or integrated load the one you are showing above.

If you really want to load the SparkPost library there are several ways to test against third party libraries. One way I do that is to use JMock to test against the library. Or you can set a Stub IRestConnection on the clients concrete instance (this is a more advanced solution).

Depending on your needs I think the first suggestion above is your best bet and will be very common with your usage of other 3rd party libraries too.

The files in the "App" directory are essential example usage/integration examples. Feel free to add an issue to this project for adding demo app unit tests: https://github.com/SparkPost/java-sparkpost

The files in "Lib" is the actual SparkPost library.

You can find the existing library unit tests here which may help you to see how it was done there. Essentially there is a stub implementation for "IRestConnection". Here is an example where the StubRestConnection is created in used instead of the default real RestConnection.

I hop this helps.

Yepher
  • 1,465
  • 12
  • 25