{"action":"created","issue":{"url":"https://api.github.com/repos/junit-team/junit5/issues/1575","repository_url":"https://api.github.com/repos/junit-team/junit5","labels_url":"https://api.github.com/repos/junit-team/junit5/issues/1575/labels{/name}","comments_url":"https://api.github.com/repos/junit-team/junit5/issues/1575/comments","events_url":"https://api.github.com/repos/junit-team/junit5/issues/1575/events","html_url":"https://github.com/junit-team/junit5/issues/1575","id":356160326,"node_id":"MDU6SXNzdWUzNTYxNjAzMjY=","number":1575,"title":"TestInstance.Lifecycle on enclosing class not inherited by @Nested test class","user":{"login":"seanf","id":54155,"node_id":"MDQ6VXNlcjU0MTU1","avatar_url":"https://avatars0.githubusercontent.com/u/54155?v=4","gravatar_id":"","url":"https://api.github.com/users/seanf","html_url":"https://github.com/seanf","followers_url":"https://api.github.com/users/seanf/followers","following_url":"https://api.github.com/users/seanf/following{/other_user}","gists_url":"https://api.github.com/users/seanf/gists{/gist_id}","starred_url":"https://api.github.com/users/seanf/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/seanf/subscriptions","organizations_url":"https://api.github.com/users/seanf/orgs","repos_url":"https://api.github.com/users/seanf/repos","events_url":"https://api.github.com/users/seanf/events{/privacy}","received_events_url":"https://api.github.com/users/seanf/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-09-01T01:28:33Z","updated_at":"2018-09-01T11:55:41Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"**JUnit version:** \r\n`master` branch, commit 16ac9ac4de (git describe: r5.3.0-RC1-47-g16ac9ac4de).\r\n\r\n**Expected behaviour:** \r\n\r\n- Configured `Lifecycle.PER_CLASS` is inherited in Nested test class from the outer class.\r\n- `@Nested` class cannot override enclosing class's `Lifecycle`\r\n- Ref: https://github.com/junit-team/junit5/issues/1566#issuecomment-416966563\r\n\r\n**Actual behaviour:**\r\n\r\n- `@Nested` test class has `Lifecycle.PER_METHOD`\r\n- `@Nested` test class can override enclosing class's `Lifecycle`\r\n\r\n**Test case:**\r\n```java\r\nimport java.util.Optional;\r\n\r\nimport org.junit.jupiter.api.Nested;\r\nimport org.junit.jupiter.api.Test;\r\nimport org.junit.jupiter.api.TestInstance;\r\nimport org.junit.jupiter.api.extension.BeforeEachCallback;\r\nimport org.junit.jupiter.api.extension.ExtendWith;\r\nimport org.junit.jupiter.api.extension.ExtensionContext;\r\nimport org.junit.jupiter.api.extension.TestInstanceFactory;\r\nimport org.junit.jupiter.api.extension.TestInstanceFactoryContext;\r\nimport org.junit.jupiter.api.extension.TestInstantiationException;\r\n\r\nimport static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;\r\nimport static org.junit.platform.commons.util.ReflectionUtils.newInstance;\r\n\r\n/**\r\n * @author Sean Flanigan <a href=\"mailto:sflaniga@redhat.com\">sflaniga@redhat.com</a>\r\n */\r\n\r\n@ExtendWith(BeforeEachLogger.class)\r\n// if you enable this, it show that InnerTestCase really is constructed twice:\r\n//@ExtendWith(CustomTestInstanceFactory.class)\r\n@TestInstance(PER_CLASS)\r\nclass OuterTestCase {\r\n\r\n    @Test\r\n    void outerTest() {\r\n        System.out.println(\"executing outerTest\");\r\n    }\r\n\r\n    @Nested\r\n    // Unless you specify PER_CLASS again, the InnerTestCase will be\r\n    // constructed more than once, despite the PER_CLASS on OuterTestCase\r\n//    @TestInstance(PER_CLASS)\r\n    class InnerTestCase {\r\n\r\n        @Test\r\n        void innerTest1() {\r\n            System.out.println(\"executing innerTest1\");\r\n        }\r\n\r\n        @Nested\r\n        class InnerInnerTestCase {\r\n\r\n            @Test\r\n            void innerTest2() {\r\n                System.out.println(\"executing innerTest2\");\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\nclass BeforeEachLogger implements BeforeEachCallback {\r\n    @Override\r\n    public void beforeEach(ExtensionContext context) throws Exception {\r\n        System.out.println(\"beforeEach() called for: \" + context.getRequiredTestMethod());\r\n        // It APPEARS that getTestInstanceLifecycle is always present in beforeEach\r\n        // but this seems to be undocumented.\r\n        System.out.println(\"beforeEach() lifecycle: \" + context.getTestInstanceLifecycle().get());\r\n    }\r\n}\r\n\r\nclass CustomTestInstanceFactory implements TestInstanceFactory {\r\n    @Override\r\n    public Object createTestInstance(\r\n            TestInstanceFactoryContext factoryContext,\r\n            ExtensionContext extensionContext)\r\n            throws TestInstantiationException {\r\n        try {\r\n            Optional<Object> outerInstance = factoryContext.getOuterInstance();\r\n            Class<?> testClass = factoryContext.getTestClass();\r\n            if (outerInstance.isPresent()) {\r\n                System.out.println(\"createTestInstance() called for inner class: \" + testClass.getSimpleName());\r\n                return newInstance(testClass, outerInstance.get());\r\n            }\r\n            else {\r\n                System.out.println(\"createTestInstance() called for outer class: \" + testClass.getSimpleName());\r\n                return newInstance(testClass);\r\n            }\r\n        }\r\n        catch (Exception e) {\r\n            throw new TestInstantiationException(e.getMessage(), e);\r\n        }\r\n    }\r\n}\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/junit-team/junit5/issues/comments/417854460","html_url":"https://github.com/junit-team/junit5/issues/1575#issuecomment-417854460","issue_url":"https://api.github.com/repos/junit-team/junit5/issues/1575","id":417854460,"node_id":"MDEyOklzc3VlQ29tbWVudDQxNzg1NDQ2MA==","user":{"login":"sbrannen","id":104798,"node_id":"MDQ6VXNlcjEwNDc5OA==","avatar_url":"https://avatars0.githubusercontent.com/u/104798?v=4","gravatar_id":"","url":"https://api.github.com/users/sbrannen","html_url":"https://github.com/sbrannen","followers_url":"https://api.github.com/users/sbrannen/followers","following_url":"https://api.github.com/users/sbrannen/following{/other_user}","gists_url":"https://api.github.com/users/sbrannen/gists{/gist_id}","starred_url":"https://api.github.com/users/sbrannen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sbrannen/subscriptions","organizations_url":"https://api.github.com/users/sbrannen/orgs","repos_url":"https://api.github.com/users/sbrannen/repos","events_url":"https://api.github.com/users/sbrannen/events{/privacy}","received_events_url":"https://api.github.com/users/sbrannen/received_events","type":"User","site_admin":false},"created_at":"2018-09-01T11:55:41Z","updated_at":"2018-09-01T11:55:41Z","author_association":"MEMBER","body":"As of this moment in time, `ExtensionContext.getTestInstanceLifecycle()` should not be _empty_ except in the **root** `ExtensionContext`.\r\n\r\nIf we were to introduce additional nodes in the execution hierarchy (e.g., for packages), `ExtensionContext.getTestInstanceLifecycle()` would almost certainly be _empty_ in those cases as well."}}
{"actor":{"display_login":"sbrannen"}}