0
これはテストしたいコードです。リソースにdependency.xmlがあるので、コードは正常に動作します(どこにあるべきか)。それは正しく実行されます。Mockito Javaテスト常に合格 - 何が間違っていますか?
@Component
public class ProjectBuilderBean {
public List<String> getDependencyList() {
List<String> listDeps = new ArrayList<String>();
try {
ClassLoader classLoader = getClass().getClassLoader();
File xmlFile = new File(classLoader.getResource("dependency.xml").getFile());
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
NodeList nList = doc.getElementsByTagName("dependency");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String dependency = eElement.getElementsByTagName("artifactId").item(0).getTextContent();
listDeps.add(dependency);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return listDeps;
}
}
これは私が書いたテストであり、何らかの理由で常に合格となります。私はなぜ、どのようにそれが合格するのか理解していませんが、私はそれがすべきではないという事実を知っています。私はリストに何も追加していません。それを追加しても何とかしてパスします。
@WebAppConfiguration
public class ProjectBuilderBeanTest {
@Mock
private ProjectBuilderController projectBuilderBeanMock;
//Decleration of the Class Instance
@Mock
ProjectBuilderBean projectBuilderBean;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
//Initialise the mocking of the class
projectBuilderBean = Mockito.mock(ProjectBuilderBean.class);
}
@Test
public void getDependencyListTest() throws Exception {
ArrayList<String> result = new ArrayList<String>();
result.add("a");
result.add("b");
when(projectBuilderBean.getDependencyList()).thenReturn(result);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
projectBuilderBean = null;
}
}
dependency.xmlファイルによって生成されたリストの一貫性をテストするだけです。ただ、コメント投稿をエコーするhttp://screenshot.net/3qoe4s0
テストメソッドが実際に何かをテストしているかどうかはわかりません。メソッド呼び出しで返さなければならないものを嘲笑したオブジェクトに伝えます。 – Berger
また、あなたのコードはうまくありません。 jarファイルにパッケージ化されると、すぐには動作しなくなります。 ClassLoaderによってロードされるリソースはファイルではありません。ファイルではなく、uriまたはInputStreamをparse()に渡します。 –
あなたは少なくとも私を@Bergerに始めてもらえる方向に向けることができますか? –