Lazy init prototype field returns new instance each time field requested

Viewed 40

I have problem using prototype lazy initialized field in my tests.

Here's my annotation wrapping @Lazy, @Scope and @Component:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Lazy
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public @interface PageObject {
}

Here's my component class:

@Getter
@PageObject
public class DashboardPage extends BasePage {
...
}

BaseTest class:

@SpringJUnitConfig(classes = {SpringConfig.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@Extensions({
        @ExtendWith(SpringExtension.class),
        @ExtendWith(ReportPortalExtension.class),
        @ExtendWith(CustomAfterTestExecutionCallback.class)
})
public class BaseTest {

    @Autowired @Lazy
    public DashboardPage dashboardPage;
...
}

Test code:

public class TransactionHistorySmokeTest extends BaseTest {

    @Test
    void transactionHistorySmokeTest() {

        dashboardPage.toggleTransactionHistoryWindow();

        dashboardPage.toggleTransactionHistoryWindow();
    }

Problem description

The problem is each toggleTransactionHistoryWindow() method reflects to different DashboardPage instance. Even though it's prototype scope I understand it should be new instance per injection. In this case I get new instance per object reference operation. Any tips what am I missing?

0 Answers
Related