首页 \ 问答 \ 想要了解@Tested如何与JMockit一起使用(Wants to understand how @Tested works with JMockit)

想要了解@Tested如何与JMockit一起使用(Wants to understand how @Tested works with JMockit)

我很久以来一直在使用JMockit。我想了解@Tested是如何工作的。

今天我试图在我的Test课程中使用它。 我的理解是无论我们想要测试哪个类,我们都可以将它标记为@Tested。

令我困惑的一件事就是当我尝试在@ Before.Below中设置一些东西时,我的查询就是这样。

我想要编写测试用例的类

public  Class A{

    public A(){}

}

Test class

public class ATest {

    @Tested
    private A a;

    @Before
    public void setUp(){
        a.setSomething();
    }

    @Test
        public void testA(){
    }
}

在这种情况下,我得到NPE。 但是如果我直接在我的测试方法中使用相同的代码块就可以正常工作。任何人都可以帮助我理解@Tested的行为。 我正在使用Jmockit版本1.17

我还检查了GitHub上的帖子如下: https//github.com/jmockit/jmockit1/issues/168我只想确认它是否也解决了我的问题?


I am using JMockit since long.I would like to understand how @Tested works.

Today i was trying to use it within my Test class. What i understand is Whatever class we wants to test we can mark it as @Tested.

One thing which was confusing me about the behaviur of this is when i try to set something in @Before.Below is my query.

My Class for which i want to write Test case

public  Class A{

    public A(){}

}

Test class

public class ATest {

    @Tested
    private A a;

    @Before
    public void setUp(){
        a.setSomething();
    }

    @Test
        public void testA(){
    }
}

In this case i get NPE. But if i use the same block of code in my test method directly that just works fine.Can anybody help me to understand the behavior of @Tested. I am using Jmockit version 1.17

I have also checked the post on GitHub as below: https://github.com/jmockit/jmockit1/issues/168 i just wanted to confirm is it also fixing my problem?


原文:https://stackoverflow.com/questions/30868690
更新时间:2022-11-02 14:11

最满意答案

由于已将symbolObject添加到列表中并稍后销毁相同的symbolObject,因此当您尝试移动时,添加的对象不再存在(尽管引用保留在列表中)。

GameObject():GameObject在层次结构中创建一个新的GameObject并实例化(original:Object):Object克隆一个对象。 创建或克隆GameObject时,它也将出现在层次结构中。

要使代码正常工作,请将实例化对象添加到列表中,然后销毁游戏对象。

symbol.Add (Instantiate (symbol[symbol.Count-1], newPos, Quaternion.identity));
Destroy (symbolObject);

另一种方法是完全跳过实例化步骤,因为每次都会创建一个新的游戏对象。

但是,我建议您将游戏对象保留为预制件并从资源文件夹加载它并在所述对象上使用Instantiate。 阅读folloiwng链接,了解如何使用资源加载文件夹(第二个示例还说明了如何使用Instantiate) http://docs.unity3d.com/ScriptReference/Resources.Load.html


Since you have added the symbolObject to a list and later destroy the same symbolObject, the added object no longer exists when you try to move (although the reference stays in the list).

GameObject():GameObject creates a new GameObject in the hierarchy and Instantiate(original:Object):Object clones an object. When a GameObject is created or cloned, it will also appear in the hierarchy.

To get your code working, add the instantiated object to the list and then destroy the game object.

symbol.Add (Instantiate (symbol[symbol.Count-1], newPos, Quaternion.identity));
Destroy (symbolObject);

an alternative would be to skip the Instantiate step completely since you create a new game object every time.

However, I would recommend that you keep your game object as a prefab and load it from the resource folder and use Instantiate on said object. Read the folloiwng link for how to use the resources load folder (2nd example also shows how to use Instantiate for this) http://docs.unity3d.com/ScriptReference/Resources.Load.html

相关问答

更多
  • 有几种不同的方式可以做到这一点。 1)如果他们都在同一父母身边,并且没有其他物体,那么你可以循环穿过孩子并改变他们的颜色 GameObject Parent = GameObject.Find("ParentObject"); for( int x = 0; x > Parent.transform.childCount; x++); { Parent.transform.getChild(x).GetComponent().material.color = Color.red; } ...
  • 由于已将symbolObject添加到列表中并稍后销毁相同的symbolObject,因此当您尝试移动时,添加的对象不再存在(尽管引用保留在列表中)。 GameObject():GameObject在层次结构中创建一个新的GameObject并实例化(original:Object):Object克隆一个对象。 创建或克隆GameObject时,它也将出现在层次结构中。 要使代码正常工作,请将实例化对象添加到列表中,然后销毁游戏对象。 symbol.Add (Instantiate (symbol[symb ...
  • 是的,它通过在键盘中单击0来实例化rigibodies。 并且碰撞没有检测到 有关更多信息,请继续 http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html http://docs.unity3d.com/Documentation/ScriptReference/Physics.IgnoreCollision.html Yes it instantiate rigibodies by clicking 0 i ...
  • 您应该将所有元素的RectTransform.position.z设置为相同的值(例如0)。 RectTransform.position = new Vector3(RectTransfarm.position.x, RectTransform.position.y, 0.0f); 您也可以在创建对象时执行此操作。 将代码发布到创建对象的位置以获取示例。 You should set RectTransform.position.z of all elements to same value ( ...
  • Unity旨在与组件体系结构一起使用,而不是面向对象的设计。 这意味着,大多数情况下,您将在场景中创建GameObjects,然后将脚本附加到它们,添加行为或数据。 数据也可以存储在项目中的ScriptableObject实例中,或存储在文本文件或纹理等任何其他资源中。 由于Unity中没有主要功能或任何特定程序启动,因此您必须在第一个场景中选择一个根管理器GameObject,它具有MonoBehaviour脚本和一个在开始时触发的Unity回调(例如Awake或Start )。 此脚本现在可以像常规应用 ...
  • 如果您的依赖注入要求很小(从它的声音来看,它们是),您可能需要考虑尝试MEF 。 这是轻量级且易于使用的,并且具有直接在.NET 4中处于框架中的优点,这意味着如果您迁移到.NET 4,则无需额外部署。 在此期间,它通过Codeplex站点在3.5上得到支持。 MEF很好用,因为它可以在任何类型上工作,而不仅仅是接口。 话虽这么说,如果你想让插件使用你的应用程序提供的“库”,你很可能总是需要这个库可供你的插件使用和引用(或至少一组基类或接口用于API)。 If your dependency injecti ...
  • 你不能在编辑器之外这样做。 当你使用public UnityEngine.Object[] scenes; 为了保存场景,Unity将使用SceneAsset作为占位符来保存场景。 与此相关的问题是SceneAsset来自UnityEditor命名空间,这意味着您的代码只能在具有AssetDatabase 的编辑器中使用。 您无法构建它以在任何独立平台上运行。 如果您尝试构建项目,则会出现此错误。 这是如何获取所有场景并将它们存储在数组中(应该在编辑器和构建中工作): public Scene[] scen ...
  • 好吧,你无法获得所有对象的列表,因为对象没有注册,类型是。 Unity 2.0添加了UnityContainer.Registrations属性,您可以使用它来查看容器中的内容。 如果您注册了打开的泛型,导致Registrations属性抛出异常,请立即警告已知的错误。 Well, you can't get a list of all objects, because objects aren't registered, types are. Unity 2.0 added the UnityContai ...
  • 我感到困惑的问题是,无论我在哪里看,都说你不应该在你的MonoBehaviour派生的类中使用构造函数。 Unity希望充当从UnityEngine.Object继承的所有对象的工厂, UnityEngine.Object将包括所有GameObjects,组件,行为脚本等。 在许多情况下,与之交互的.NET对象更多地是引擎实际使用的底层C ++对象的垫片。 直接调用其中一个构造函数将创建一个从C ++内部孤立的.NET对象。 所有这些都隐藏在视野之外,因此初学者可以更容易地使用Unity。 它们不是说“嘿, ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)