FlexTradeUKLtd / jfixture

JFixture is an open source library based on the popular .NET library, AutoFixture
MIT License
105 stars 22 forks source link

Ability to customise properties of composite objects #4

Closed richkeenan closed 7 years ago

richkeenan commented 10 years ago

AutoFixture allows you to customise particular properties of objects using this syntax

fixture.Build<Order>().With(x => x.Id, "ABC-123").Create();

This works because C# is able to evaluate the lambda expression to pull out the string "Id".

As a first step I'd like JFixture to be able to do this using a stringly typed API, so something along the lines of

fixture.customise().properyOf(Order.class, "Id", "ABC-123");
waliahimanshu commented 8 years ago

Hi, Absolutely loved the library, is this feature implemented yet, If not can I offer any help ?

richkeenan commented 8 years ago

Hi @waliahimanshu , thanks for the kind words!

I've not worked on this in ages to be honest - I'm a C# dev at work and so this library hasn't been my focus lately, although I should be looking at more often TBH.

Last time I was looking at this issue I was trying to think about the best way it would work under-the-hood, but couldn't come with anything I was happy with, but here's what I was thinking 2 years ago (that I can remember!):

I've also been thinking of spinning out V3 and only supporting Java 8 (with V2 for Java 6/7). So if Java 8 offers a type-safe way of getting the name of a getter/setter I'd be super happy to include that in V3.

Do you know if Java has a similar mechanism nowadays for doing this? Thanks

waliahimanshu commented 8 years ago

I recently started with Android(Java), was doing .net before and using AutoFixtures a lot for TDD. I will explore the options java 8 has to offer.

Keen to look at the first point. Will let you know if I have some questions or when I know more around this.

TWiStErRob commented 7 years ago

+1 for this

Java 8 has method references, but their name cannot be queried just by using the platform. Even if it worked you couldn't do the same with fields (there're no field references in the language). See http://stackoverflow.com/q/19845213/253468 for more. One of the answers uses Mockito, that might be the same as your point about the proxy library.

Love the idea of the name "stringly typed API", never heard that before :)

richkeenan commented 7 years ago

@TWiStErRob thanks for the info, that reinforces my understanding. I think the best thing here is to implement a stringed API so people can start using it immediately, then figure out a strongly typed API that will just call down to the string version. Probably using the concept of a proxy (you're right that this is how Mockito does it). Java has a built in mechanism for this that I'll explore too.

TWiStErRob commented 7 years ago

Awesome! Thanks for the effort.

(note: comment on commit)