Repeatable Generation

OBJECTO

Objecto offers a sophisticated mechanism for controlling the randomness of object generation through seed specification at various levels. The correct priority order of these levels ensures that developers can finely tune how objects are generated, based on the specificity of the seed placement. The priorities, from highest to lowest, are as follows:

1. Factory Method Level

The highest priority. Seeds specified directly on a factory method cannot be overridden. This allows for the most granular control over the randomness of individual object generations.

@Seed(100L)
Dto createDto();

2. Factory Instance Level

The second-highest priority. A seed specified at the factory instance level applies to all factory methods unless a method-specific seed is defined. This level of seed specification provides a default seed for all object generations from that instance.

private final DtoFactory factory = Objecto.create(DtoFactory.class, 100L);

3. Factory Interface/Class Level

The third priority. Seeds specified at this level apply to all object generations within the factory unless overridden by a factory instance or method-specific seed. It acts as a default seed for all methods in the absence of higher-priority seeds.

@Seed(100L)
private interface DtoFactory {
    // Factory methods
}

4. Test Method Level

The lowest priority. Seeds specified on test methods are used only if no other seeds are specified at higher levels. This allows for a default level of randomness control in tests, which can be overridden for more specific needs.

For this feature to work, the test class must be extended with ObjectoExtension, which allows Objecto to intercept the test execution and apply the specified seed.

@ExtendWith(ObjectoExtension.class)
class ObjectoExtensionTest {

    private final DtoFactory factory = Objecto.create(DtoFactory.class);

    @Test
    @Seed(100L)
    void testWithMethodLevelSeed() {
        final Dto dto = factory.createDto();
        // test 
    }
}

Reproducing Failed Tests

An integral part of ensuring the reliability of tests is the ability to reproduce failed test cases. When using ObjectoExtension, Objecto enhances this capability by automatically logging the seed used in a test if that test fails. This feature is especially useful in scenarios where randomness is controlled by seeds, as it allows developers to exactly replicate the conditions under which the test failed.

When a test fails, Objecto reports the seed into the console. This makes it straightforward for developers to debug the test by rerunning it with the same seed, ensuring the same random generation sequence is used. The logging is done as follows:

log.info(
    "Test method '{}' failed with seed: {}", 
    extensionContext.getRequiredTestMethod().getName(), seed
);

This log statement provides clear and immediate feedback on the seed value associated with the failed test, identified by the test method's name. By offering this level of transparency, Objecto significantly aids in the debugging process, allowing developers to focus on resolving the issues rather than spending time trying to reproduce the test failure conditions.

To take advantage of this feature, ensure that your test classes are extended with ObjectoExtension:

@ExtendWith(ObjectoExtension.class)
class YourTestClass {
    // Test methods
}

This addition to your testing workflow not only improves the efficiency of debugging failed tests but also reinforces the overall robustness of your testing strategy by ensuring that every test failure can be accurately and quickly addressed.

Last updated