Обновлен метод isVanityURL. См. Ниже оригинальный вопрос и код, основанный на рекомендации Шахида. Также смотрим на класс Path, предложенный assylias.Junit не проходит, хотя он должен
public static boolean isVanityPath(String resourcePath) {
String resPath = resourcePath;
if (resPath == null) {
return false;
} else {
resPath = resPath.trim();
if (!resPath.equalsIgnoreCase(StringPool.BLANK)) {
int len = resPath.length();
String startChar = resPath.substring(0, 1);
if (startChar.equals(StringPool.FORWARD_SLASH) && len > 1) {
resPath = resPath.substring(1, len--);
}else{
return false;
}
int lastIndexOfSlash = resPath.lastIndexOf(StringPool.FORWARD_SLASH);
int slashIndex = resPath.indexOf(StringPool.FORWARD_SLASH);
if (slashIndex != -1)
return slashIndex == lastIndexOfSlash && lastIndexOfSlash == len - 1;
else
return true;
} else {
return false;
}
}
Это Обновленное JUnit Test
@Before
public void setUp() {
vu = Mockito.mock(ResourcePathUtil.class);
}
@Test
public void testVanityURLWhenRoot() {
Assert.assertFalse(ResourcePathUtil.isVanityPath("/"));
}
@Test
public void testVanityURLWhenNull() {
Assert.assertFalse(ResourcePathUtil.isVanityPath(null));
}
@Test
public void testVanityURLWhenValidVanity() {
Assert.assertTrue(ResourcePathUtil.isVanityPath("/vanitycode"));
}
@Test
public void testVanityURLWhenValidVanityWithTrailingSlash() {
boolean retValue = ResourcePathUtil.isVanityPath("/vanitycode/");
Assert.assertFalse("Returned True", retValue);
}
@Test
public void testVanityURLWhenInvalidVanityWithTrailingSlash() {
Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode/invalidwithslash/"));
}
@Test
public void testVanityURLWhenInvalidVanity() {
Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode/justinvalid"));
}
@Test
public void testVanityURLWhenBlank() {
Assert.assertFalse(ResourcePathUtil.isVanityPath(""));
}
У меня есть следующий класс (ResourcePathUtil) со статическим методом. Я хотел бы протестировать его с помощью JUnit (URLTest). Однако некоторые из тестов (testVanityURLWhenRoot, testVanityURLWhenValidVanity), похоже, не проходят, хотя и должны. Любые указания на то, что я делаю неправильно?
public class ResourcePathUtil {
/**
*
* @param url
* @param data
* @return result
*/
public static boolean isVanityPath(String resourcePath) {
String resPath = resourcePath;
if (resPath == null) {
return false;
} else {
resPath = resPath.trim();
if (!resPath.equalsIgnoreCase(StringPool.BLANK)) {
int len = resPath.length();
String startChar = resPath.substring(0, 1);
if (startChar.equals(StringPool.FORWARD_SLASH)) {
resPath = resPath.substring(1, len--);
}
int lastIndexOfSlash = resPath.lastIndexOf(StringPool.FORWARD_SLASH);
int slashIndex = resPath.indexOf(StringPool.FORWARD_SLASH);
if (slashIndex != -1)
return slashIndex == lastIndexOfSlash && lastIndexOfSlash == len - 1;
else
return true;
} else {
return false;
}
}
}
}
JUnit класс ниже
import junit.framework.Assert;
import org.mockito.Mockito;
import org.junit.Before;
import org.junit.Test;
public class URLTest {
@Before
public void setUp() {
vu = Mockito.mock(ResourcePathUtil.class);
}
@Test
public void testVanityURLWhenRoot() {
Assert.assertFalse(ResourcePathUtil.isVanityPath("/"));
}
@Test
public void testVanityURLWhenNull() {
Assert.assertFalse(ResourcePathUtil.isVanityPath(null));
}
@Test
public void testVanityURLWhenValidVanity() {
Assert.assertTrue(!ResourcePathUtil.isVanityPath("/vanitycode"));
}
@Test
public void testVanityURLWhenValidVanityWithTrailingSlash() {
boolean retValue = ResourcePathUtil.isVanityPath("/vanitycode/");
Assert.assertTrue("Returned False", !retValue);
}
@Test
public void testVanityURLWhenInvalidVanityWithTrailingSlash() {
Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode/invalidwithslash/"));
}
@Test
public void testVanityURLWhenInvalidVanity() {
Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode/justinvalid"));
}
@Test
public void testVanityURLWhenBlank() {
Assert.assertFalse(ResourcePathUtil.isVanityPath(""));
}
}
Строка бассейн класса ниже
public class StringPool {
public static final String BLANK = "";
public static final String FORWARD_SLASH = "/";
}
Почему вы не используете класс 'Path'? – assylias
Какой тест не удается? Все они? – nasukkin
@assylias, вы имеете в виду [это] (https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html) – Pragrammer