首页 \ 问答 \ 单元测试返回Connection对象的方法(Unit Test for a method that returns a Connection object)

单元测试返回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
更新时间:2024-05-03 19:05

最满意答案

创建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.

相关问答

更多

相关文章

更多

最新问答

更多
  • 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)
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的
  • SimplePie问题(SimplePie Problem)
  • 在不同的任务中,我们可以同时使用多少“上下文”?(How many 'context' we can use at a time simultaneously in different tasks?)
  • HTML / Javascript:从子目录启用文件夹访问(HTML/Javascript: Enabling folder access from a subdirectory)
  • 为什么我会收到链接错误?(Why do I get a linker error?)
  • 如何正确定义析构函数(How to properly define destructor)
  • 垂直切换菜单打开第3级父级。(Vertical toggle menu 3rd level parent stay opened. jQuery)
  • 类型不匹配 - JavaScript(Type mismatch - JavaScript)
  • 为什么当我将模型传递给我的.Net MVC 4控制器操作时,它坚持在部分更新中使用它?(Why is it that when I pass a Model to my .Net MVC 4 Controller Action it insists on using it in the Partial Update?)
  • 在使用熊猫和statsmodels时拉取变量名称(Pulling variable names when using pandas and statsmodels)
  • 如何开启mysql计划事件
  • 检查数组的总和是否大于最大数,反之亦然javascript(checking if sum of array is greater than max number and vice versa javascript)
  • 使用OpenGL ES绘制轮廓(Drawing Outline with OpenGL ES)
  • java日历格式(java Calendar format)
  • Python PANDAS:将pandas / numpy转换为dask数据框/数组(Python PANDAS: Converting from pandas/numpy to dask dataframe/array)
  • 如何搜索附加在elasticsearch索引中的文档的内容(How to search a content of a document attached in elasticsearch index)
  • LinQ to Entities:做相反的查询(LinQ to Entities: Doing the opposite query)
  • 从ExtJs 4.1商店中删除记录时会触发哪些事件(Which events get fired when a record is removed from ExtJs 4.1 store)
  • 运行javascript后如何截取网页截图[关闭](How to take screenshot of a webpage after running javascript [closed])
  • 如何使用GlassFish打印完整的堆栈跟踪?(How can I print the full stack trace with GlassFish?)
  • 如何获取某个exe应用程序的出站HTTP请求?(how to get the outbound HTTP request of a certain exe application?)
  • 嗨,Android重叠背景片段和膨胀异常(Hi, Android overlapping background fragment and inflate exception)
  • Assimp详细说明typedef(Assimp elaborated type refers to typedef)
  • 初始化继承类中不同对象的列表(initialize list of different objects in inherited class)
  • 使用jquery ajax在gridview行中保存星级评分(Save star rating in a gridview row using jquery ajax)
  • Geoxml3 groundOverlay zIndex(Geoxml3 groundOverlay zIndex)