JUnit Test against an interface without having the implementation yet

chillyistkult

I try to write a test for a given interface like that with JUnit and have no idea how to do that:

public interface ShortMessageService {

    /**
     * Creates a message. A message is related to a topic
     * Creates a date for the message
     * @throws IllegalArgumentException, if the message is longer then 255 characters.
     * @throws IllegalArgumentException, if the message ist shorter then 10 characters.
     * @throws IllegalArgumentException, if the user doesn't exist
     * @throws IllegalArgumentException, if the topic doesn't exist
     * @throws NullPointerException, if one argument is null.
     * @param userName
     * @param message
     * @return ID of the new created message
     */
    Long createMessage(String userName, String message, String topic);

    [...]
}

I tried to mock the interface after I realized that it doesn't make sense at all so I am a bit lost. Maybe someone can give me a good approach I can work with. I also heard about junit parameterized tests but I am not sure if that is what I am looking for.

Many thanks!

Peter

I use the following pattern to write abstract tests against my interface APIs without having any implementations available. You can write whatever tests you require in AbstractShortMessageServiceTest without having to implement them at that point in time.

public abstract class AbstractShortMessageServiceTest
{
    /**
     * @return A new empty instance of an implementation of FooManager.
     */
    protected abstract ShortMessageService getNewShortMessageService();

    private ShortMessageService testService;

    @Before
    public void setUp() throws Exception
    {
        testService = getNewShortMessageService();
    }

    @Test
    public void testFooBar() throws Exception 
    {
        assertEquals("question", testService.createMessage(
                                             "DeepThought", "42", "everything"));
    }
}

When you have an implementation, you can use the test simply by defining a new test class that overrides AbstractShortMessageServiceTest and implements the getNewShortMessageService method.

public class MyShortMessageServiceTest extends AbstractShortMessageServiceTest
{
    protected ShortMessageService getNewShortMessageService()
    {
        return new MyShortMessageService();
    }
}

In addition, if you need the test to be parameterized, you can do that in AbstractShortMessageServiceTest without doing it in each of the concrete tests.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Is it possible to use Java functional interface implementation (such as the Supplier) as a MethodSource in Junit 5?

From Dev

Difference in interface and implementation section having {$R *.DFM} directive

From Dev

Set property (type interface) without implementation

From Dev

Running a WebDriver Test without using ANT, Maven, JUnit or Eclipse

From Dev

Move constructor without implementation, yet it works

From Dev

Test Method Called Without Having Argument In Test Class

From Dev

Auto-generating implementation of interface (proxy without class)

From Dev

Having full atomicity against all the threads without impacting performance or throughputs

From Dev

Run grails functional test without having embedded tomcat start

From Dev

Run JUnit test in IntelliJ IDEA 14 without choosing configuration type

From Dev

Test if object is implementation of interface in Google Closure class framework

From Dev

How are interface methods able to be utilised without having to implement them?

From Dev

StackOverflowException by using explict interface implementation having covariant generic parameter

From Dev

How to make the interface enforce the implementation not having a setter?

From Dev

Junit Test suite without creating empty class

From Dev

Use a setter in JUnit test without having one in class

From Dev

Java: Calling in main implemented Interface Methods from a jUnit - Test

From Dev

JUnit assert that an Activity is implementation of an interface

From Dev

Auto-generating implementation of interface (proxy without class)

From Dev

Is it possible to test an UPS battery without having the UPS nearby?

From Dev

How are interface methods able to be utilised without having to implement them?

From Dev

Adding functionality to implementation classes without changing the implemented Interface

From Dev

@interface and @implementation in same file (no header file, Xcode unit test template)

From Dev

Junit Test generic interface java

From Dev

JUnit Testing: How To Test If Statements Without Missing A Branch?

From Dev

Test interface implementation for simple calculator

From Dev

Calculate time of execution of junit test in jmeter without setUp and tearDown methods?

From Dev

C# Reflection test if member is an interface implementation

From Dev

Mocking two different implementation of an Interface in Controller Mockito Junit test case

Related Related

  1. 1

    Is it possible to use Java functional interface implementation (such as the Supplier) as a MethodSource in Junit 5?

  2. 2

    Difference in interface and implementation section having {$R *.DFM} directive

  3. 3

    Set property (type interface) without implementation

  4. 4

    Running a WebDriver Test without using ANT, Maven, JUnit or Eclipse

  5. 5

    Move constructor without implementation, yet it works

  6. 6

    Test Method Called Without Having Argument In Test Class

  7. 7

    Auto-generating implementation of interface (proxy without class)

  8. 8

    Having full atomicity against all the threads without impacting performance or throughputs

  9. 9

    Run grails functional test without having embedded tomcat start

  10. 10

    Run JUnit test in IntelliJ IDEA 14 without choosing configuration type

  11. 11

    Test if object is implementation of interface in Google Closure class framework

  12. 12

    How are interface methods able to be utilised without having to implement them?

  13. 13

    StackOverflowException by using explict interface implementation having covariant generic parameter

  14. 14

    How to make the interface enforce the implementation not having a setter?

  15. 15

    Junit Test suite without creating empty class

  16. 16

    Use a setter in JUnit test without having one in class

  17. 17

    Java: Calling in main implemented Interface Methods from a jUnit - Test

  18. 18

    JUnit assert that an Activity is implementation of an interface

  19. 19

    Auto-generating implementation of interface (proxy without class)

  20. 20

    Is it possible to test an UPS battery without having the UPS nearby?

  21. 21

    How are interface methods able to be utilised without having to implement them?

  22. 22

    Adding functionality to implementation classes without changing the implemented Interface

  23. 23

    @interface and @implementation in same file (no header file, Xcode unit test template)

  24. 24

    Junit Test generic interface java

  25. 25

    JUnit Testing: How To Test If Statements Without Missing A Branch?

  26. 26

    Test interface implementation for simple calculator

  27. 27

    Calculate time of execution of junit test in jmeter without setUp and tearDown methods?

  28. 28

    C# Reflection test if member is an interface implementation

  29. 29

    Mocking two different implementation of an Interface in Controller Mockito Junit test case

HotTag

Archive