@@ -11,8 +11,8 @@ import java.lang.reflect.ParameterizedType
11
11
* A factory is a special class which is able to generate new valid objects, for testing purposes.
12
12
* These objects can be randomized by using a faker.
13
13
*
14
- * @param < M > The type of the generated object
15
- * @param < F > The type of the faker of this factory. This allows to override the faker with a custom implementation.
14
+ * @param < M > The type of the generated object
15
+ * @param < F > The type of the faker of this factory. This allows to override the faker with a custom implementation.
16
16
*/
17
17
abstract class BaseFactory <M, F extends Faker > extends Definition<M> {
18
18
/**
@@ -94,6 +94,9 @@ abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
94
94
95
95
/**
96
96
* Returns a new instance that is not saved.
97
+ * <p >
98
+ * In normal usage, this method should not be overriden. If you want to change how the object is built, use
99
+ * {@link #onAfterBuild} or {@link #internalBuild}.
97
100
*
98
101
* @param traits A list of traits to apply to new object. A trait is basically a collection of attribute/relation
99
102
* updates, meant to create an object representing a certain state. The possible traits are specified in the factory.
@@ -103,10 +106,27 @@ abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
103
106
build([:], traits)
104
107
}
105
108
109
+ /**
110
+ * Returns a new instance that is not saved.
111
+ * <p >
112
+ * In normal usage, this method should not be overriden. If you want to change how the object is built, use
113
+ * {@link #onAfterBuild} or {@link #internalBuild}.
114
+ *
115
+ * @param traits An array of traits to apply to new object. A trait is basically a collection of attribute/relation
116
+ * updates, meant to create an object representing a certain state. The possible traits are specified in the factory.
117
+ * @return The new instance.
118
+ */
119
+ M build (String ... traits ) {
120
+ build([:], traits. toList())
121
+ }
122
+
106
123
/**
107
124
* Returns the passed object, after it is saved.
108
125
* <p >
109
126
* This method exists so it is possible to completely override a relation by passing your own instance, or null.
127
+ * <p >
128
+ * To persist the object, the current context (@link FactoryManager#currentContext} is temporarily set to {@link FactoryManager#createContext}.
129
+ * This context specifies the strategy to use to persist the object.
110
130
*
111
131
* @param object The custom instance of the object.
112
132
* @return The passed object, after is saved.
@@ -118,8 +138,8 @@ abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
118
138
/**
119
139
* Returns a new instance that is saved.
120
140
* <p >
121
- * In normal usage, this method should not be overriden. If you want to change how the object is built, use
122
- * { @link #onAfterBuild} or { @link #internalBuild} .
141
+ * To persist the object, the current context ( @link FactoryManager#currentContext} is temporarily set to { @link FactoryManager#createContext}.
142
+ * This context specifies the strategy to use to persist the object .
123
143
*
124
144
* @return The new saved instance.
125
145
*/
@@ -130,8 +150,8 @@ abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
130
150
/**
131
151
* Returns a new instance that is saved.
132
152
* <p >
133
- * In normal usage, this method should not be overriden. If you want to change how the object is built, use
134
- * { @link #onAfterBuild} or { @link #internalBuild} .
153
+ * To persist the object, the current context ( @link FactoryManager#currentContext} is temporarily set to { @link FactoryManager#createContext}.
154
+ * This context specifies the strategy to use to persist the object. .
135
155
*
136
156
* @param overrides Additional overrides to use when building a new object.
137
157
* Build parameters allow to define custom values for attributes and relations.
@@ -143,6 +163,34 @@ abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
143
163
doInCreateContext { build(overrides, traits) }
144
164
}
145
165
166
+ /**
167
+ * Returns a new instance that is saved.
168
+ * <p >
169
+ * To persist the object, the current context (@link FactoryManager#currentContext} is temporarily set to {@link FactoryManager#createContext}.
170
+ * This context specifies the strategy to use to persist the object..
171
+ *
172
+ * @param traits A list of traits to apply to new object. A trait is basically a collection of attribute/relation
173
+ * updates, meant to create an object representing a certain state. The possible traits are specified in the factory.
174
+ * @return The new saved instance.
175
+ */
176
+ M create (List<String > traits ) {
177
+ doInCreateContext { build(traits) }
178
+ }
179
+
180
+ /**
181
+ * Returns a new instance that is saved.
182
+ * <p >
183
+ * To persist the object, the current context (@link FactoryManager#currentContext} is temporarily set to {@link FactoryManager#createContext}.
184
+ * This context specifies the strategy to use to persist the object..
185
+ *
186
+ * @param traits An array of traits to apply to new object. A trait is basically a collection of attribute/relation
187
+ * updates, meant to create an object representing a certain state. The possible traits are specified in the factory.
188
+ * @return The new saved instance.
189
+ */
190
+ M create (String ... traits ) {
191
+ doInCreateContext { build(traits) }
192
+ }
193
+
146
194
/**
147
195
* Returns a list of new instances that are not saved.
148
196
*
@@ -263,7 +311,7 @@ abstract class BaseFactory<M, F extends Faker> extends Definition<M> {
263
311
*/
264
312
private M applyAfterBuildHooks (M object , List<String > traits = null ) {
265
313
onAfterBuild(object)
266
- if (traits) traits. each { findTrait(it). onAfterBuild(object) }
314
+ if (traits) traits. each { findTrait(it). onAfterBuild(object) }
267
315
object
268
316
}
269
317
0 commit comments