45176

How to compose variable name dynamically?

Question:

I need to generate a list,and name it's items based on for-loop index number, like this:

for(int i=0;i<someNumber;i++){ Model m_{$i}=Mock() //but this doesn't work ...... models.add(i,m_{$i}) }

then they can be distinguished by name when debugging test code(shame to tell this) within eclipse,but it doesn't work, so how to make it work?

update:add image to tell why I want to append for-loop index to variable name <img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/1aoEk.png" data-original="https://i.stack.imgur.com/1aoEk.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Answer1:

You can also add some property to your Mock class at runtime thanks to Groovy's <a href="http://groovy.codehaus.org/Per-Instance+MetaClass" rel="nofollow">MetaClass</a>. Take a look at this sample snippet:

class myClass { String someProperty } def models = [] 10.times { it -> def instance = new myClass(someProperty: "something") instance.metaClass.testId = it models.add(instance) } // delete some println "Removing object with testId = " + models.remove(4).testId println "Removing object with testId = " + models.remove(7).testId def identifiersOfObjectsAfterRemoves = models.collect { it.testId } def removedObjectsIdentifiers = (0..9) - identifiersOfObjectsAfterRemoves println "Identifiers of removed objects: " + removedObjectsIdentifiers

Recommend

  • Use multiple datastore connections with Sequelize
  • What is the datatype bytea and when would I use it?
  • Is there a way to wait for THREE.TextureLoader.load() to finish?
  • C# Java-like inline extension of classes?
  • How can I replace a null value in a json document by a specific value with jq?
  • Configuring mod_proxy for Apache to reject wrong domain requests
  • Google App Engine - SSL InsecurePlatformWarning
  • Calling a constructor through reflection in scala 2.10
  • WCF service runs in Debug mode but not in Release
  • What is the Linux Equivalent of Kernel32.dll?
  • CS1703: In Xamarin.Droid, should I use the .Net Standard windowsruntime.dll located in Mono.Framewor
  • How do I add a File Type Association in a Windows Phone 8.1 app manifest?
  • Wrapping text in spinner android
  • runtime error when linking ffmpeg libraries in qt creator
  • Any nice way to generate a timeline view of commits from subversion?
  • Local Development, Apache vs Developer - file permissions
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Detecting null parameter in preprocessor macro
  • Debugging VB6 Code From Visual Studio 2010
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • debug library loaded with ctypes using gdb
  • Debugging ASP.NET on a built-in web server suddenly stops
  • Deselecting radio buttons while keeping the View Model in synch
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • JSON with duplicate key names losing information when parsed
  • Jquery - Jquery Wysiwyg return html as a string
  • retrieve vertices with no linked edge in arangodb
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to get icons for entities from eclipse?
  • Acquiring multiple attributes from .xml file in c#
  • Understanding cpu registers
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Add sale price programmatically to product variations
  • unknown Exception android
  • Checking variable from a different class in C#
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal