如何测试这部分代码

发布于 2024-08-24 23:22:47 字数 204 浏览 1 评论 0原文

您好,我正在编写 junit 测试,如何测试此方法..这只是此方法的一部分:

public MyClass{

public void myMethod(){
List<myObject> list = readData();
}
}

我将如何对此进行测试? ReadData 是 MyClass 中的私有方法吗?

Hello I'm writting junit test how can I test this method .. this is only part of this method :

public MyClass{

public void myMethod(){
List<myObject> list = readData();
}
}

How will I make the test for this? ReadData is a private method inside MyClass?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给不了的爱 2024-08-31 23:22:48

正如所写,测试 myMethod() 没有意义,除非 readData() 按照 Frank Grimm 提到的方式更改实例状态。要做的一件事是更改 myMethod() ,以便将 list 放入 List 实例变量中。然后您可能会这样做:

@Test
public void testThatReadDataReturnsACorrectList(){
   MyClass inst = new MyClass(); // Add args to ctor call if needed - maybe a file path that readData() will use?
   inst.myMethod();

   // Create a list of MyClasses that match what you expect readData() to return:
   List<MyClass> expectedList = new List<>();
   expectedList.Add(new MyClass(/* Some arguments */));
   expectedList.Add(new MyClass(/* Some more arguments */));
   expectedList.Add(new MyClass(/* Some other arguments */));

   // Assert that the list you created matches the list you get back from 
   assertArrayEquals("Did not get the list expected", expectedList.ToArray(), inst.getList().ToArray());

}

您仍然需要编写 MyClass.getList() 来返回 List 实例变量。

为了保持健壮性,您可以使 MyClass 构造函数接受一个实现 IMyReadInterface 等接口的对象。 readData() 将使用该对象。然后在测试中,您可以实例化一个也实现 IMyReadInterface 的模拟,配置该模拟以提供所需的数据,以便 readData() 正常工作,并构造 inst 与该模拟。

As written, it doesn't make sense to test myMethod() unless readData() changes the instance state the way Frank Grimm mentioned. One thing to do would be to change myMethod() so that it puts list into a List instance variable. Then you might do something like this:

@Test
public void testThatReadDataReturnsACorrectList(){
   MyClass inst = new MyClass(); // Add args to ctor call if needed - maybe a file path that readData() will use?
   inst.myMethod();

   // Create a list of MyClasses that match what you expect readData() to return:
   List<MyClass> expectedList = new List<>();
   expectedList.Add(new MyClass(/* Some arguments */));
   expectedList.Add(new MyClass(/* Some more arguments */));
   expectedList.Add(new MyClass(/* Some other arguments */));

   // Assert that the list you created matches the list you get back from 
   assertArrayEquals("Did not get the list expected", expectedList.ToArray(), inst.getList().ToArray());

}

You'd still have to write MyClass.getList() to return the List instance variable.

To be robust, you could make the MyClass constructor accept an object that implements an interface like IMyReadInterface. readData() would use that object. Then in your test, you could instantiate a mock that also implements IMyReadInterface, configure the mock to provide the data needed so that readData() works correctly, and construct inst with that mock.

荆棘i 2024-08-31 23:22:48

除非我们更多地了解该方法,否则您真正需要测试的是 readData 返回的格式是否适合您的通用列表。否则,如果不了解您的私有方法中发生的情况,就很难推荐任何东西。

Unless we know more about the method, all you really need to test is that the return from readData is in a format that fits into your generic list. Otherwise, its hard to recommend anything without knowing more about whats going on in your private method.

仄言 2024-08-31 23:22:47

您始终可以测试 List 对象以查看它是否包含 readData() 应该插入到列表中的所有元素。创建一个返回列表的公共方法,您可以将该列表中的长度和元素与您期望的长度和元素进行比较。

You can always test the List object to see if it contains all of the elements that readData() is supposed to insert into the list. Make a public method that returns the list and you can compare the length and elements in that list to what you are expecting to be in there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文