单元测试返回Connection对象的方法(Unit Test for a method that returns a Connection object)
我已经实现了一个返回Connection对象的方法。
现在我想对该方法进行单元测试,并在我做出典型时
assertEquals(expectedConnection, actualConnection);
它失败并带有错误代码:
java.lang.AssertionError:expected [org.sqlite.SQLiteConnection@1a968a59]但找到[org.sqlite.SQLiteConnection@4667ae56]
我希望测试通过,即使对象不相同(这就是为什么我没有使用assertSame),它们具有相同的特性(以相同的方式构建,具有相同的类属性)...是有没有办法测试Connection对象?
注意:我对返回语句的方法的单元测试有同样的问题
谢谢你的帮助!!
I have implemented a method that returns a Connection object.
Now I want to unit test that method and when I make the typical
assertEquals(expectedConnection, actualConnection);
it fails with the error code:
java.lang.AssertionError: expected [org.sqlite.SQLiteConnection@1a968a59] but found [org.sqlite.SQLiteConnection@4667ae56]
I hoped the test passed as, even when the objects are not the same (that's why I haven't used assertSame), they have the same characteristics (have been built in the same way, with the same class atributes)... Is there any way to test Connection objects?
NB: I have the same issue with the unit test of a method that returns a statement
Thanks for your help!!
原文:https://stackoverflow.com/questions/36438078
最满意答案
创建View的子类。 实现三个View构造函数。 实现onSizeChanged()和onDraw() 。 可选择实现onMeasure() , onLayout() , onTouchEvent()等。
实际上,您最好的方法是从骨架模板开始并扩展它。 这就是我经常做的事情:
/** * $Id: custom.hlp,v 1.15 2013-10-24 23:20:01 falk Exp $ * * Template.java - Template widget * * Author: Edward A. Falk * efalk@users.sourceforge.net * * Date: May 2009 * * This code demonstrates the creation of a custom widget, * with some custom resources. * * Copy and paste this to your own class. * Delete the parts you don't need. */ package com.example.template; import android.view.View; import android.content.Context; import android.content.res.Resources; import android.util.AttributeSet; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.util.Log; public class Template extends View { private static final String TAG = "Template"; private Context ctx; private Resources res; private Paint paint; // Some XML resources: private int function; private String label; /** * Constructor used when view is constructed in code. */ public Template(Context ctx) { super(ctx); TemplateInit(ctx, null); } /** * Constructor used when view is constructed from XML */ public Template(Context ctx, AttributeSet attrs) { super(ctx, attrs); TemplateInit(ctx, attrs); } /** * Same, with class-specific base style */ public Template(Context ctx, AttributeSet attrs, int defStyle) { super(ctx, attrs, defStyle); TemplateInit(ctx, attrs); } private void TemplateInit(Context ctx, AttributeSet attrs) { // Handle whatever common initialization is appropriate for // this widget. this.ctx = ctx; res = getResources(); paint = new Paint(); paint.setAntiAlias(true); paint.setDither(false); } @Override protected void onSizeChanged(int w, int h, int ow, int oh) { // Deal with size change } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setColor(Color.BLACK); canvas.drawText(label, x, y, paint); } /** * Receive touch events. Leave this out if the default * behavior is ok. */ @Override public final boolean onTouchEvent(MotionEvent event) { // Return false to let some other view have it // Return true to say we handled it // Or let the superclass deal with it return super.onTouchEvent(event); } /** * Determine how large we want to be. We can skip implementing * this method if the superclass will handle it for us. */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int wid = View.MeasureSpec.getSize(widthMeasureSpec); int hgt = View.MeasureSpec.getSize(heightMeasureSpec); int wmode = View.MeasureSpec.getMode(widthMeasureSpec); int hmode = View.MeasureSpec.getMode(heightMeasureSpec); final int hpad = getPaddingLeft() + getPaddingRight(); final int vpad = getPaddingTop() + getPaddingBottom(); // Overview: Measure our contents plus internal padding. // Pass this information to setMeasuredDimensions(). Whatever // values we pass, we can assume that's what our final size // will be. If the parent doesn't like it, it will call this // method again, with more restrictions. // What happens now depends on the measure spec mode. If // it's EXACTLY, we ignore our own needs and return the // size part of the measurespec. If the mode is UNSPECIFIED, // we ignore the size part of the measurespec and return // anything we want. If the mode is AT_MOST, we might return // the lesser of our own needs and the size part of // measurespec, or we might just treat this the same // as EXACTLY. // Easiest implementation is to use setMinimumWidth()/Height() // to establish our minimum required size, and then let // getSuggestedMinimumWidth()/Height() and getDefaultSize() // do the heavy lifting. These may be overridden if needed, // to customize behavior. setMinimumWidth(hpad + yaddayadda); setMinimumHeight(vpad + yaddayadda); setMeasuredDimension( getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); } /** * Parent has made final decision and is laying us out. Here * is where we do any internal layout we need. */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { } }
您甚至可以定义和实现自定义属性,但它很复杂,我建议不要这样做。
Make a subclass of View. Implement the three View constructors. Implement onSizeChanged() and onDraw(). Optionally implement onMeasure(), onLayout(), onTouchEvent(), and so forth.
Truly, your best approach is to start with a skeletal template and expand it. That's what I always do:
/** * $Id: custom.hlp,v 1.15 2013-10-24 23:20:01 falk Exp $ * * Template.java - Template widget * * Author: Edward A. Falk * efalk@users.sourceforge.net * * Date: May 2009 * * This code demonstrates the creation of a custom widget, * with some custom resources. * * Copy and paste this to your own class. * Delete the parts you don't need. */ package com.example.template; import android.view.View; import android.content.Context; import android.content.res.Resources; import android.util.AttributeSet; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.util.Log; public class Template extends View { private static final String TAG = "Template"; private Context ctx; private Resources res; private Paint paint; // Some XML resources: private int function; private String label; /** * Constructor used when view is constructed in code. */ public Template(Context ctx) { super(ctx); TemplateInit(ctx, null); } /** * Constructor used when view is constructed from XML */ public Template(Context ctx, AttributeSet attrs) { super(ctx, attrs); TemplateInit(ctx, attrs); } /** * Same, with class-specific base style */ public Template(Context ctx, AttributeSet attrs, int defStyle) { super(ctx, attrs, defStyle); TemplateInit(ctx, attrs); } private void TemplateInit(Context ctx, AttributeSet attrs) { // Handle whatever common initialization is appropriate for // this widget. this.ctx = ctx; res = getResources(); paint = new Paint(); paint.setAntiAlias(true); paint.setDither(false); } @Override protected void onSizeChanged(int w, int h, int ow, int oh) { // Deal with size change } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paint.setColor(Color.BLACK); canvas.drawText(label, x, y, paint); } /** * Receive touch events. Leave this out if the default * behavior is ok. */ @Override public final boolean onTouchEvent(MotionEvent event) { // Return false to let some other view have it // Return true to say we handled it // Or let the superclass deal with it return super.onTouchEvent(event); } /** * Determine how large we want to be. We can skip implementing * this method if the superclass will handle it for us. */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int wid = View.MeasureSpec.getSize(widthMeasureSpec); int hgt = View.MeasureSpec.getSize(heightMeasureSpec); int wmode = View.MeasureSpec.getMode(widthMeasureSpec); int hmode = View.MeasureSpec.getMode(heightMeasureSpec); final int hpad = getPaddingLeft() + getPaddingRight(); final int vpad = getPaddingTop() + getPaddingBottom(); // Overview: Measure our contents plus internal padding. // Pass this information to setMeasuredDimensions(). Whatever // values we pass, we can assume that's what our final size // will be. If the parent doesn't like it, it will call this // method again, with more restrictions. // What happens now depends on the measure spec mode. If // it's EXACTLY, we ignore our own needs and return the // size part of the measurespec. If the mode is UNSPECIFIED, // we ignore the size part of the measurespec and return // anything we want. If the mode is AT_MOST, we might return // the lesser of our own needs and the size part of // measurespec, or we might just treat this the same // as EXACTLY. // Easiest implementation is to use setMinimumWidth()/Height() // to establish our minimum required size, and then let // getSuggestedMinimumWidth()/Height() and getDefaultSize() // do the heavy lifting. These may be overridden if needed, // to customize behavior. setMinimumWidth(hpad + yaddayadda); setMinimumHeight(vpad + yaddayadda); setMeasuredDimension( getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); } /** * Parent has made final decision and is laying us out. Here * is where we do any internal layout we need. */ @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { } }
You can even define and implement custom attributes, but it's complicated and I recommend against it.
相关问答
更多-
让res/layout/view_layout.xml以
元素而不是LinearLayout开头。 Have res/layout/view_layout.xml start with a element instead of a LinearLayout. -
RecyclerView可能是要走的路。 定义不同的视图类型 实际的标题, 部分标题,和 列表项 如此设置将使您获得除 “今天”标记之外的整个屏幕。 要添加该标记,您需要在recyclerview中的视图上绘制。 有2个选项 只需在recyclerview的顶部添加另一个视图,您可以在其中绘制文本和行,这可能但不是最干净的方法,或者 使用RecyclerView.ItemDecoration ,在标题后面的第一个项目的顶部绘制“今天”文本,并使用垂直线装饰(绘制)下面的每个后续视图。 RecyclerVie ...
-
好吧,那个命令没有任何选择。 您可以在官方文档中查看所有可用选项。 但是如果你真的需要那么你必须覆盖SensioGeneratorBundle,你可以看到如何在这里扩展任何bundle。 然后覆盖php类vendor / sensio / generator-bundle / Generator / DoctrineCrudGenerator.php,然后找到动作“generate”并找到这一行: $dir = sprintf('%s/Resources/views/%s', $this->rootDir, ...
-
在Interface Builder中创建和使用框架中的自定义视图(Create and use Custom Views from a Framework in Interface Builder)[2021-09-13]
您应该能够在Interface Builder中添加任何UIView对象,并将其更改为Identity Inspector的自定义控件类型 无论它是否看起来像一个灰色框或更具描述性的内容,都@IBDesignable框架以及它是否实现了@IBDesignable 。 有关@IBDesignable更多信息,请查看此处: http : @IBDesignable 在-xcode中-6- You should be able to add any UIView object in Interface Build ... -
正如@Paulw所说,你需要实现MKMapViewDelegate函数mapView(_:viewFor:)来返回相应的视图。 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { if annotation is MKUserLocation { return nil } if /* condition */ { let ...
-
自定义视图绘制其他视图(Custom view draws over other views)[2022-11-08]
用这个如何创建自定义视图(How to create Custom Views)[2022-10-23]
创建View的子类。 实现三个View构造函数。 实现onSizeChanged()和onDraw() 。 可选择实现onMeasure() , onLayout() , onTouchEvent()等。 实际上,您最好的方法是从骨架模板开始并扩展它。 这就是我经常做的事情: /** * $Id: custom.hlp,v 1.15 2013-10-24 23:20:01 falk Exp $ * * Template.java - Template widget * * Author: Edwa ...是的,这是CBV要掌握的主要问题:当事情运行时,执行的顺序是什么(参见http://lukeplant.me.uk/blog/posts/djangos-cbvs-were-a-mistake/ )。 简而言之,每个基于类的视图都有一个运行顺序,每个顺序都有自己的方法。 CBV为每个执行步骤都有一个专用方法。 您可以从运行要从中调用自定义方法的步骤的方法调用自定义方法。 如果您想在视图发现表单有效后运行您的方法,则执行以下操作: Class InvoiceCreateView(LoginRequiredMi ...你可以看看这个。 你的问题似乎很容易解决。 http://www.raywenderlich.com/87004/getting-started-with-os-x-and-swift-tutorial-part-3 You can maybe take a look on this. You problem seems easy to solve. http://www.raywenderlich.com/87004/getting-started-with-os-x-and-swift-tutorial ...请查看有关CompoundControls的android文档。 Please view the android documentation regarding CompoundControls.相关文章
更多- Object Oriented Programming
- The connection to adb is down, and a severe error has occured.
- Solr: a custom Search RequestHandler
- [转]So You Want To Be A Producer
- Spring Data: a new perspective of data operations
- Drupal Forums instead of phpBB or vBulletin: A casestudy
- Hibernate 异常之:associate a collection with two ...
- Guava学习笔记:复写的Object常用方法
- Becoming a data scientist
- 按钮样式
最新问答
更多- python的访问器方法有哪些
- 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
- 透明度错误IE11(Transparency bug IE11)
- linux的基本操作命令。。。
- 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
- 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
- NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
元素上的盒子阴影行为(box-shadow behaviour on elements) - Laravel检查是否存在记录(Laravel Checking If a Record Exists)
- 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
- 想学Linux 运维 深圳有哪个培训机构好一点
- 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
- 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
- AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
- 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
- 等待EC2实例重启(Wait for an EC2 instance to reboot)
- 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
- 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
- 江民杀毒软件的KSysnon.sys模块是什么东西?
- 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
- sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
- 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
- AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
- SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
- PerSession与PerCall(PerSession vs. PerCall)
- C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
- 平衡一个精灵(Balancing a sprite)
- n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
- Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
- 湖北京山哪里有修平板计算机的