2016-09-16 6 views
0

Обновлен метод 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 = "/"; 

} 
+0

Почему вы не используете класс 'Path'? – assylias

+0

Какой тест не удается? Все они? – nasukkin

+0

@assylias, вы имеете в виду [это] (https://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html) – Pragrammer

ответ

0
@Test 
public void testVanityURLWhenRoot() { 
    // expecting isVanityPath() to return false 
    Assert.assertFalse(ResourcePathUtil.isVanityPath("/")); 
} 

@Test 
public void testVanityURLWhenValidVanity() { 
    // expecting isVanityPath() to return false 
    Assert.assertTrue(!ResourcePathUtil.isVanityPath("/vanitycode")); 
} 

В обоих testVanityURLWhenRoot и testVanityURLWhenValidVanity, вы ожидаете false. Но вы получаете true. Причина кроется в методе isVanityPath().

В обоих случаях значение slashIndex is -1. И вы возвращаете true, когда slashIndex равен -1. Вот почему в обоих случаях вы получаете true результат, хотя вы ожидаете false.

if (slashIndex != -1) { 
    return slashIndex == lastIndexOfSlash && lastIndexOfSlash == len - 1; 
} else { // it executes when slashIndex == -1 
    return true; 
} 

Предложение:

Вместо того, чтобы:

Assert.assertTrue(!ResourcePathUtil.isVanityPath("/vanitycode")); 

записи:

Assert.assertFalse(ResourcePathUtil.isVanityPath("/vanitycode")); 

Последнее более читаемым.

+0

Спасибо за исправление testVanityURLWhenRoot и testVanityURLWhenValidVanity в методе isVanityURL. Теперь стоит проблема с testVanityURLWhenValidVanityWithTrailingSlash. – Pragrammer