请给出以下代码的junit白盒测试代码package service; import dao.TypeDao; import model.Type; import java.sql.SQLException; import java.util.List; public class TypeService { TypeDao tDao=new TypeDao(); public List<Type> GetAllType() { List<Type> list=null; try { list=tDao.GetAllType(); } catch (SQLException e) { e.printStackTrace(); } return list; } public Type selectTypeNameByID(int typeid) { Type type=null; try { type=tDao.selectTypeNameByID(typeid); } catch (SQLException e) { e.printStackTrace(); } return type; } public Type select(int id) { Type t=null; try { t = tDao.select(id); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return t; } public void insert(Type t) { try { tDao.insert(t); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void update(Type t) { try { tDao.update(t); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public boolean delete(int id) { try { tDao.delete(id); return true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } }

时间: 2023-06-17 11:06:17 浏览: 40
以下是给出的Junit测试代码: ``` package service; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import dao.TypeDao; import model.Type; public class TypeServiceTest { @Mock private TypeDao typeDao; @InjectMocks private TypeService typeService; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); } @Test public void testGetAllType() throws SQLException { List<Type> expectedTypes = new ArrayList<Type>(); Type type1 = new Type(); type1.setId(1); type1.setName("Type 1"); Type type2 = new Type(); type2.setId(2); type2.setName("Type 2"); expectedTypes.add(type1); expectedTypes.add(type2); // Mocking the DAO's behavior when(typeDao.GetAllType()).thenReturn(expectedTypes); List<Type> actualTypes = typeService.GetAllType(); assertNotNull(actualTypes); assertEquals(expectedTypes.size(), actualTypes.size()); assertTrue(actualTypes.containsAll(expectedTypes)); } @Test public void testSelectTypeNameByID() throws SQLException { int typeId = 1; Type expectedType = new Type(); expectedType.setId(typeId); expectedType.setName("Type 1"); // Mocking the DAO's behavior when(typeDao.selectTypeNameByID(typeId)).thenReturn(expectedType); Type actualType = typeService.selectTypeNameByID(typeId); assertNotNull(actualType); assertEquals(expectedType, actualType); } @Test public void testSelect() throws SQLException { int id = 1; Type expectedType = new Type(); expectedType.setId(id); expectedType.setName("Type 1"); // Mocking the DAO's behavior when(typeDao.select(id)).thenReturn(expectedType); Type actualType = typeService.select(id); assertNotNull(actualType); assertEquals(expectedType, actualType); } @Test public void testInsert() throws SQLException { Type type = new Type(); type.setId(1); type.setName("Type 1"); typeService.insert(type); // Verifying that the DAO's insert method was called with the correct parameter verify(typeDao).insert(type); } @Test public void testUpdate() throws SQLException { Type type = new Type(); type.setId(1); type.setName("Type 1"); typeService.update(type); // Verifying that the DAO's update method was called with the correct parameter verify(typeDao).update(type); } @Test public void testDelete() throws SQLException { int id = 1; // Mocking the DAO's behavior when(typeDao.delete(id)).thenReturn(true); boolean result = typeService.delete(id); // Verifying that the DAO's delete method was called with the correct parameter verify(typeDao).delete(id); assertTrue(result); } } ``` 以上测试代码使用了Mockito框架来模拟TypeDao类的行为,在测试TypeService类的方法时,通过Mockito提供的when和verify方法来验证TypeService类的行为是否正确。测试代码中包含了对GetAllType、selectTypeNameByID、select、insert、update和delete方法的测试。

相关推荐

最新推荐

recommend-type

解决 java.lang.NoSuchMethodError的错误

主要介绍了解决 java.lang.NoSuchMethodError的错误的相关资料,需要的朋友可以参考下
recommend-type

Junit单元测试的实验报告.docx

简单介绍了Junit的安装过程与实例应用。应用的问题是软件测试中的佣兵问题,整个文档中有代码及测试结果,可以更好地帮助学生了解Junit单元测试中的作用。
recommend-type

XP 单元测试工具Junit 源代码学习.doc

Package framework Class & Interface Hierachy Interface Test 主要方法 countTestCases:统计TestCases 数目 run:运行测试并将结果返回到指定的TestResult 中 Class Assert 首先,Assert 提供的public 方法都可以...
recommend-type

软件测试技术实验报告.doc

1.1黑盒测试问题描述 假设商店货品价格(R) 都不大于100元(且为整数),若顾客付款(P)在100元内,现有一个程序能在每位顾客付款后给出找零钱的最佳组合(找给顾客货币张数最少)。 假定此商店的货币面值只包括:50元...
recommend-type

简易计算器做单元测试.docx

myeclipse软件作为编程软件,用junit4完成一个简易计算器测试,实现加减乘除,写的很详细,按着步骤来就行了,最后一个参数测试我没有写,你可以加上
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。