我该如何编写这个测试用例?

时间:2018-03-08 20:49:03

标签: java spring-mvc junit4

我目前正在我的后端做一个关于spring-mvc的小项目,我正在尝试为我的转换器和引擎创建测试。以下是我的BookmarkEngine.java文件

@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;
    }
}

我将如何为此编写测试?

这是我的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()));
    }
}

getFullUrl()中的某个测试没有运行,但我怎样才能使它工作?

1 个答案:

答案 0 :(得分:0)

您可以进行测试以查看字符串是否匹配,例如

@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");
}