{"record":{"id":"8bcf6f5ce099db18","repo":"apache/beam","slug":"not-expected-to-access-dofn-startbundlecontext-from","errorCode":null,"errorMessage":"Not expected to access DoFn.StartBundleContext from @ProcessElement","messagePattern":"Not expected to access DoFn\\.StartBundleContext from @ProcessElement","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/DoFnTester.java","lineNumber":287,"sourceCode":"            @Override\n            public CausedByDrain causedByDrain(DoFn<InputT, OutputT> doFn) {\n              return processContext.causedByDrain();\n            }\n\n            @Override\n            public PaneInfo paneInfo(DoFn<InputT, OutputT> doFn) {\n              return processContext.pane();\n            }\n\n            @Override\n            public PipelineOptions pipelineOptions() {\n              return getPipelineOptions();\n            }\n\n            @Override\n            public DoFn<InputT, OutputT>.StartBundleContext startBundleContext(\n                DoFn<InputT, OutputT> doFn) {\n              throw new UnsupportedOperationException(\n                  \"Not expected to access DoFn.StartBundleContext from @ProcessElement\");\n            }\n\n            @Override\n            public DoFn<InputT, OutputT>.FinishBundleContext finishBundleContext(\n                DoFn<InputT, OutputT> doFn) {\n              throw new UnsupportedOperationException(\n                  \"Not expected to access DoFn.FinishBundleContext from @ProcessElement\");\n            }\n\n            @Override\n            public DoFn<InputT, OutputT>.ProcessContext processContext(DoFn<InputT, OutputT> doFn) {\n              return processContext;\n            }\n\n            @Override\n            public InputT element(DoFn<InputT, OutputT> doFn) {\n              return processContext.element();","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/DoFnTester.java#L269-L305","documentation":"DoFnTester emulates @ProcessElement execution, in which the StartBundleContext is not available; if user code inside @ProcessElement requests DoFn.StartBundleContext via a context parameter, the tester's context provider throws UnsupportedOperationException. Start bundle context is only valid within @StartBundle methods.","triggerScenarios":"A DoFn whose @ProcessElement method declares a DoFn.StartBundleContext parameter, run under DoFnTester (test with .processElement(...) invoked).","commonSituations":"Reusing a DoFn that accesses bundle lifecycle contexts in processElement, then unit testing with the legacy DoFnTester.","solutions":["Remove the StartBundleContext parameter from @ProcessElement; move bundle-level state access into @StartBundle/@FinishBundle","Access pipeline options via ProcessContext instead if that's the underlying need","Test with TestPipeline (running the real runner) instead of DoFnTester if context semantics must be exercised"],"exampleFix":"// before\n@ProcessElement public void processElement(ProcessContext c, StartBundleContext sbc) {...}\n// after\n@StartBundle public void startBundle(StartBundleContext sbc) {...}\n@ProcessElement public void processElement(ProcessContext c) {...}","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Only inject StartBundleContext in @StartBundle methods\nboolean inStartBundle = method.isAnnotationPresent(StartBundle.class);","tryCatchPattern":"try {\n  tester.processElement();\n} catch (UnsupportedOperationException e) {\n  // context requested in @ProcessElement; refactor DoFn\n}","preventionTips":["Never declare bundle lifecycle contexts as @ProcessElement parameters","Prefer TestPipeline over DoFnTester for DoFns using lifecycle contexts"],"tags":["apache-beam","java","testing","dofn"],"backgroundTag":"unsupported-operation","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}