junit中针对list的测试

junit中的不少测试的好方法,比如针对LIST的测试,可以使用
hamcrest-library类库,首先在POM.XML中加入:


<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- This will get hamcrest-core automatically -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>


1) 测试LIST,字符串

public class ListTest {

@Test
public void testAssertList() {

List<String> actual = Arrays.asList("a", "b", "c");
List<String> expected = Arrays.asList("a", "b", "c");

//All passed / true

//1. Test equal.
assertThat(actual, is(expected));

//2. If List has this value?
assertThat(actual, hasItems("b"));

//3. Check List Size
assertThat(actual, hasSize(3));

assertThat(actual.size(), is(3));

//4. List order

// Ensure Correct order
assertThat(actual, contains("a", "b", "c"));

// Can be any order
assertThat(actual, containsInAnyOrder("c", "b", "a"));

//5. check empty list
assertThat(actual, not(IsEmptyCollection.empty()));

assertThat(new ArrayList<>(), IsEmptyCollection.empty());

}

}



2) 测试INTEGER

@Test
public void testAssertList() {

List<Integer> actual = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> expected = Arrays.asList(1, 2, 3, 4, 5);

//All passed / true

//1. Test equal.
assertThat(actual, is(expected));

//2. Check List has this value
assertThat(actual, hasItems(2));

//3. Check List Size
assertThat(actual, hasSize(4));

assertThat(actual.size(), is(5));

//4. List order

// Ensure Correct order
assertThat(actual, contains(1, 2, 3, 4, 5));

// Can be any order
assertThat(actual, containsInAnyOrder(5, 4, 3, 2, 1));

//5. check empty list
assertThat(actual, not(IsEmptyCollection.empty()));

assertThat(new ArrayList<>(), IsEmptyCollection.empty());

//6. Test numeric comparisons
assertThat(actual, everyItem(greaterThanOrEqualTo(1)));

assertThat(actual, everyItem(lessThan(10)));

}


}


3 测试LIST中有复杂对象


@Test
public void testAssertList() {

List<Fruit> list = Arrays.asList(
new Fruit("Banana", 99),
new Fruit("Apple", 20)
);

//Test equals
assertThat(list, hasItems(
new Fruit("Banana", 99),
new Fruit("Apple", 20)
));

assertThat(list, containsInAnyOrder(
new Fruit("Apple", 20),
new Fruit("Banana", 99)
));

//Test class property, and its value
assertThat(list, containsInAnyOrder(
hasProperty("name", is("Apple")),
hasProperty("name", is("Banana"))
));

}

public class Fruit {

public Fruit(String name, int qty) {
this.name = name;
this.qty = qty;
}

private String name;
private int qty;

public int getQty() {
return qty;
}

public void setQty(int qty) {
this.qty = qty;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

//Test equal, override equals() and hashCode()
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Fruit fruit = (Fruit) o;
return qty == fruit.qty &&
Objects.equals(name, fruit.name);
}

@Override
public int hashCode() {
return Objects.hash(name, qty);
}
}


}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要配置SpringBoot单元测试,可以按照以下步骤进行操作: 1. 引入依赖:在项目的pom.xml文件添加Spring Boot测试的依赖项。可以使用以下代码片段将依赖项添加到pom.xml文件: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> ``` 这些依赖项将提供执行单元测试所需的必要库和工具。 2. 添加注解:在需要进行测试的类上添加注解。使用`@RunWith`注解将测试运行器设置为SpringRunner,并使用`@SpringBootTest`注解指定要启动的Spring Boot应用程序的入口类。以下是一个示例: ```java import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes={YourApplication.class}) public class YourTestClass { // 测试方法 } ``` 在上述示例,`YourApplication.class`应替换为你的Spring Boot应用程序的实际入口类。 3. 编写测试方法:在测试编写测试方法,并使用`@Test`注解标记这些方法作为测试方法。你可以在这些方法编写针对不同功能的测试用例。 这样配置后,你可以使用IDE或者Maven等工具运行单元测试。Spring Boot会自动加载应用程序上下文并执行测试方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringBoot单元测试之配置流程](https://blog.csdn.net/qq_38058674/article/details/123058717)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值