想要了解@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
最满意答案
由于已将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; } ... -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
创建Unity对象列表(Creating a List of Unity Objects)[2022-08-23]
由于已将symbolObject添加到列表中并稍后销毁相同的symbolObject,因此当您尝试移动时,添加的对象不再存在(尽管引用保留在列表中)。 GameObject():GameObject在层次结构中创建一个新的GameObject并实例化(original:Object):Object克隆一个对象。 创建或克隆GameObject时,它也将出现在层次结构中。 要使代码正常工作,请将实例化对象添加到列表中,然后销毁游戏对象。 symbol.Add (Instantiate (symbol[symb ... -
Unity3D对象创建(Unity3D objects creating)[2022-07-27]
是的,它通过在键盘中单击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 )。 此脚本现在可以像常规应用 ...
-
对于不需要界面的简单事物,Unity有什么吗?(Is there anything like Unity for simple things that don't require an interface?)[2022-05-31]
如果您的依赖注入要求很小(从它的声音来看,它们是),您可能需要考虑尝试MEF 。 这是轻量级且易于使用的,并且具有直接在.NET 4中处于框架中的优点,这意味着如果您迁移到.NET 4,则无需额外部署。 在此期间,它通过Codeplex站点在3.5上得到支持。 MEF很好用,因为它可以在任何类型上工作,而不仅仅是接口。 话虽这么说,如果你想让插件使用你的应用程序提供的“库”,你很可能总是需要这个库可供你的插件使用和引用(或至少一组基类或接口用于API)。 If your dependency injecti ... -
如何将Unity场景名称存储为对象数组时,如何获取它(How do I get a Unity scene name when they are stored as an array of Objects)[2022-05-03]
你不能在编辑器之外这样做。 当你使用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。 它们不是说“嘿, ...