Search code examples
javaspring-mvcjunit4

How do I write this test case?


I am currently doing a small project on spring-mvc in my backend and I am trying to create tests for my converters and engines. Below is my BookmarkEngine.java file

@Service
public class BookmarkEngine implements IBookmarkEngine{

    private static final String URL_PREFIX = "http://";

    @Override
    public String getFullUrl(String url) {
        if(!url.startsWith(URL_PREFIX))
        {
            return URL_PREFIX + url;
        }
        return url;
    }
}

How would I go about writing a test for this?

Here is my BookmarkEngineTest:

public class BookmarkEngineTest {

    IBookmarkEngine bookmarkEngine = new BookmarkEngine();
    private ViewBookmark defaultBookmark;

    @Before
    public void setUp() throws Exception {
        defaultBookmark = new ViewBookmark();
        defaultBookmark.setBookmarkId(1L);
        defaultBookmark.setTitle("A sample bookmark");
        defaultBookmark.setUrl("This is a sample bookmark.");
        defaultBookmark.setAuthor(".");
        defaultBookmark.setLastUpdated(1497812309081L);
    }

    @Test
    public void getFullUrl() {
        String result = bookmarkEngine.getFullUrl(defaultBookmark.getUrl());
        assertThat(result.length(), is(defaultBookmark.getUrl().length()));
    }
}

That certain test in getFullUrl() does not run, but how can I can make it work?


Solution

  • You can make a test to see if the strings will match such as

    @Test
    public void getFullUrl() {
    String testurl = "facebook.com";
    String testurl2 = "http://facebook.com";
    assertEquals(bookmarkEngine.getFullUrl(testurl),"http://facebook.com");
    assertEquals(bookmarkEngine.getFullUrl(testurl2),"http://facebook.com");
    }