企业网站服务器跟域名都需要交钱吗做推广的公司
web/
2025/9/26 14:47:58/
文章来源:
企业网站服务器跟域名都需要交钱吗,做推广的公司,邯郸市人社局,梦幻西游官方网站昨天我在办公室里#xff0c;和我的一位同事谈论测试#xff0c;他对编写单元测试有些不服气。 他使用的原因之一是有些测试似乎毫无意义#xff0c;这使我想到了什么是单元测试#xff0c;什么也不需要打扰。 考虑下面一个简单的不可变的Name Bean#xff0c;其中包含一… 昨天我在办公室里和我的一位同事谈论测试他对编写单元测试有些不服气。 他使用的原因之一是有些测试似乎毫无意义这使我想到了什么是单元测试什么也不需要打扰。 考虑下面一个简单的不可变的Name Bean其中包含一个构造函数和一堆getter。 在这个示例中我将让代码说明一切因为我希望任何测试都是毫无意义的。 public class Name {private final String firstName;private final String middleName;private final String surname;public Name(String christianName, String middleName, String surname) {this.firstName christianName;this.middleName middleName;this.surname surname;}public String getFirstName() {return firstName;}public String getMiddleName() {return middleName;}public String getSurname() {return surname;}
} ……只是为了强调这一点这是毫无意义的测试代码 public class NameTest {private Name instance;Beforepublic void setUp() {instance new Name(John, Stephen, Smith);}Testpublic void testGetFirstName() {String result instance.getFirstName();assertEquals(John, result);}Testpublic void testGetMiddleName() {String result instance.getMiddleName();assertEquals(Stephen, result);}Testpublic void testGetSurname() {String result instance.getSurname();assertEquals(Smith, result);}
} 测试此类毫无意义的原因是该代码不包含任何逻辑。 但是当您向Name类添加如下内容时 public String getFullName() {if (isValidString(firstName) isValidString(middleName) isValidString(surname)) {return firstName middleName surname;} else {throw new RuntimeException(Invalid Name Values);}}private boolean isValidString(String str) {return isNotNull(str) str.length() 0;}private boolean isNotNull(Object obj) {return obj ! null;} …然后整个情况发生了变化。 以if语句的形式添加一些逻辑会生成大量测试 Testpublic void testGetFullName_with_valid_input() {instance new Name(John, Stephen, Smith);final String expected John Stephen Smith;String result instance.getFullName();assertEquals(expected, result);}Test(expected RuntimeException.class)public void testGetFullName_with_null_firstName() {instance new Name(null, Stephen, Smith);instance.getFullName();}Test(expected RuntimeException.class)public void testGetFullName_with_null_middleName() {instance new Name(John, null, Smith);instance.getFullName();}Test(expected RuntimeException.class)public void testGetFullName_with_null_surname() {instance new Name(John, Stephen, null);instance.getFullName();}Test(expected RuntimeException.class)public void testGetFullName_with_no_firstName() {instance new Name(, Stephen, Smith);instance.getFullName();}Test(expected RuntimeException.class)public void testGetFullName_with_no_middleName() {instance new Name(John, , Smith);instance.getFullName();}Test(expected RuntimeException.class)public void testGetFullName_with_no_surname() {instance new Name(John, Stephen, );instance.getFullName();} 因此鉴于我刚刚说过您不需要测试不包含任何逻辑语句的对象并且在逻辑 语句列表中我将包含if并与所有运算符一起切换 -*- 以及可能发生变化和对象状态的一整套事物。 在此前提下我建议在我前两篇博客中一直在讨论的Address项目中为地址数据访问对象DAO编写单元测试毫无意义。 DAO由AddressDao接口定义并由JdbcAddress类实现 public class JdbcAddress extends JdbcDaoSupport implements AddressDao {/*** This is an instance of the query object thatll sort out the results of* the SQL and produce whatever values objects are required*/private MyQueryClass query;/** This is the SQL with which to run this DAO */private static final String sql select * from addresses where id ?;/*** A class that does the mapping of row data into a value object.*/class MyQueryClass extends MappingSqlQueryaddress {public MyQueryClass(DataSource dataSource, String sql) {super(dataSource, sql);this.declareParameter(new SqlParameter(Types.INTEGER));}/*** This the implementation of the MappingSqlQuery abstract method. This* method creates and returns a instance of our value object associated* with the table / select statement.* * param rs* This is the current ResultSet* param rowNum* The rowNum* throws SQLException* This is taken care of by the Spring stuff...*/Overrideprotected Address mapRow(ResultSet rs, int rowNum) throws SQLException {return new Address(rs.getInt(id), rs.getString(street),rs.getString(town), rs.getString(post_code),rs.getString(country));}}/*** Override the JdbcDaoSupport method of this name, calling the super class* so that things get set-up correctly and then create the inner query* class.*/Overrideprotected void initDao() throws Exception {super.initDao();query new MyQueryClass(getDataSource(), sql);}/*** Return an address object based upon its id*/Overridepublic Address findAddress(int id) {return query.findObject(id);}} 在上面的代码中接口中的唯一方法是 Overridepublic Address findAddress(int id) {return query.findObject(id);} ……这实际上是一种简单的吸气方法。 在我看来这是可以的因为DAO中确实不应包含属于AddressService的任何业务逻辑该业务逻辑应具有大量的单元测试。 您可能要决定是否要为MyQueryClass编写单元测试。 对我来说这是一个临界情况所以我期待任何评论…… 我猜想有人会不同意这种方法说您应该测试JdbcAddress对象这是真的我亲自为其编写了一个集成测试以确保我使用的数据库可以正常运行并且可以理解我的数据库SQL并且两个实体DAO和数据库可以互相通信但是我不会打扰对其进行单元测试。 总而言之单元测试必须是有意义的并且对“有意义”的良好定义是被测对象必须包含一些独立的逻辑。 参考 您应该对什么进行单元测试 – Captain Debug博客上的 JCG合作伙伴 Roger Hughes的 测试技术3 相关文章 测试技巧–不编写测试 端到端测试的滥用–测试技术2 常规单元测试和存根–测试技术4 使用模拟的单元测试–测试技术5 为旧版代码创建存根–测试技术6 有关为旧版代码创建存根的更多信息–测试技术7 为什么要编写单元测试–测试技巧8 一些定义–测试技术9 使用FindBugs产生更少的错误代码 在云中开发和测试 翻译自: https://www.javacodegeeks.com/2011/11/what-should-you-unit-test-testing.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/81495.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!