Java 如何对接收和返回对象列表的方法进行单元测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24773416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to UnitTest a method that recieve and return a list of objects
提问by Hyman
My class is as following, it receives a string and using that create an arraylist of objects. I am not sure how to test it.
我的课程如下,它接收一个字符串并使用它创建一个对象数组列表。我不知道如何测试它。
public class Company{
private String id;
private String name;
....
}
public List<Company> retrieve(String factories) {
String[] fac = factories.split(",");
List<Company> comp;
comp = new ArrayList<>();
for (int i = 0; i < fac.length - 1; i++) {
comp.add(new Company(fac[i], fac[i]));
}
return comp;
}
JUnitTest
JUnit测试
@Test
public void testRetrieve() {
System.out.println("retrieve");
String factories = "a,b,c";
Transact instance = new Transact();
List<Company> expResult = null; //<<<how to define this?
List<Company> result = instance.retrieveData(factories);
assertEquals(expResult, result);
}
采纳答案by Ruchira Gayan Ranaweera
Define your expected result
定义您的预期结果
List<Company> expResult = null;// it shouldn't be null
It should be what will return from your method.
它应该是您的方法返回的内容。
Let's say when you pass
让我们说当你通过
String factories = "some sting";
should return List x
from your method, Then create that List x
应该List x
从你的方法返回 ,然后创建List x
You can try something like this.
你可以尝试这样的事情。
@Test
public void testRetrieve() {
System.out.println("retrieve");
String factories = "a,b,c";
Transact instance = new Transact();
List<Company> expResult = new ArrayList<>();
Company com1=new Company("a","a");
Company com2=new Company("b","b");
Company com3=new Company("c","c");
expResult.add(com1);
expResult.add(com2);
expResult.add(com3);
List<Company> result = instance.retrieveData(factories);
assertEquals(expResult, result);
}
I think now you can write your own test case.
我想现在你可以编写自己的测试用例了。
回答by Cameron
I like to create a different method for every scenario. You've tested the "empty list" scenario. Here are some others:
我喜欢为每个场景创建不同的方法。您已经测试了“空列表”方案。以下是其他一些:
// The happy path
@Test
public void testMultipleCompanies() {
Transact instance = new Transact();
List<Company> companies = instance.retrieveData("firstCompany,secondCompany");
assertTrue(companies.size() == 2);
assertEquals(companies.get(0).getId().equals("firstCompany");
assertEquals(companies.get(0).getName().equals("firstCompany");
assertEquals(companies.get(1).getId().equals("secondCompany");
assertEquals(companies.get(1).getName().equals("secondCompany");
}
// The null string scenario
@Test(expected=NullPointerException.class)
public void testNull() {
Transact instance = new Transact();
instance.retrieveData(null);
}
回答by Mark Bramnik
You should test the actual result against the expected results in unit tests.
您应该根据单元测试中的预期结果测试实际结果。
So you "assume" that if your class is written correctly (that's what you check), then you are supposed to get a list of Company classes as you expect, that's the result.
因此,您“假设”如果您的类编写正确(这就是您检查的内容),那么您应该按预期获得 Company 类的列表,这就是结果。
I'll give you an easier example. Lets suppose you are about to check with unit tests the method that makes a sum of two numbers:
我会给你一个更简单的例子。让我们假设您要检查单元测试的方法,使两个数字的总和:
int sum(int a, int b) {
return a + b;
}
So the unit test should supply an input and verify the expected output against the actual output.
所以单元测试应该提供一个输入并根据实际输出验证预期输出。
So here you go:
所以给你:
// unit test
int expectedOutput = 10;
int actualOutput = sum(3,7);
assertEquals(expectedOutput, actualOutput);
In other words you "make sure" that the actualOutput that the method has produced based on your inputs matches your expectation. This is how you check that the methods is implemented correctly.
换句话说,您“确保”该方法根据您的输入生成的实际输出符合您的期望。这是您检查方法是否正确实施的方式。
Now let's get back to your example. In fact you have to verify that the list of "Company" classes is correct.
现在让我们回到你的例子。事实上,您必须验证“公司”类列表是否正确。
So your expected Output is also a list, and the question is actually how you compare the actual list of companies with the expected list of companies.
因此,您的预期输出也是一个列表,问题实际上是您如何将实际公司列表与预期公司列表进行比较。
In Java, given you've defined the "equals" method on the class "Company", you can safely check with assertEquals (it should work for lists, given you're using stuff like ArrayList)
在 Java 中,假设您已经在类“Company”上定义了“equals”方法,您可以安全地使用 assertEquals 进行检查(它应该适用于列表,因为您使用的是 ArrayList 之类的东西)
The expected result can be generated like this:
可以像这样生成预期的结果:
List<Company> expectedResult = Arrays.asList(new Company(...), new Company(...));
Hope this helps and happy unit testing :)
希望这会有所帮助和快乐的单元测试:)