diff --git a/java/java-tests/testData/codeInsight/surroundWith/BlockedByPSI.java b/java/java-tests/testData/codeInsight/surroundWith/BlockedByPSI.java
new file mode 100644
index 000000000000..819f1ba69778
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/surroundWith/BlockedByPSI.java
@@ -0,0 +1,6 @@
+class Plin {
+ public static void write(int startBitInUnit, int bitSize) {
+ startBitInUnit + bitSize > 0
+ }
+
+}
diff --git a/java/java-tests/testData/codeInsight/surroundWith/BlockedByPSI_after.java b/java/java-tests/testData/codeInsight/surroundWith/BlockedByPSI_after.java
new file mode 100644
index 000000000000..353c6a8ab89f
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/surroundWith/BlockedByPSI_after.java
@@ -0,0 +1,9 @@
+class Plin {
+ public static void write(int startBitInUnit, int bitSize) {
+ if (startBitInUnit + bitSize > 0) {
+
+ } else {
+ }
+ }
+
+}
diff --git a/java/java-tests/testData/codeInsight/surroundWith/Runnable.java b/java/java-tests/testData/codeInsight/surroundWith/Runnable.java
new file mode 100644
index 000000000000..47eea96987cd
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/surroundWith/Runnable.java
@@ -0,0 +1,31 @@
+class External {
+ private int member;
+
+ External() {
+ member = 0;
+ }
+
+ void testMethod(int param1, int param2) {
+ int local1 = 0;
+ int local2 = 123;
+ int local3 = local1;
+
+ {
+ int innerLocal1 = 0;
+ int innerLocal2 = 7;
+ int innerLocal3 = 0;
+ int innerLocal4 = 7;
+ String incorrect;
+
+ int insideRunnable1 = param1 + ((++innerLocal2) << param2);
+ param2 = local1 * insideRunnable1;
+ local3 = param2;
+ int insideRunnable2 = local2;
+ innerLocal1--;
+ --innerLocal3;
+ innerLocal4++;
+ incorrect = "misplaced initialization";
+ local2 = local3;
+ }
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/surroundWith/Runnable_after.java b/java/java-tests/testData/codeInsight/surroundWith/Runnable_after.java
new file mode 100644
index 000000000000..35837c5082d1
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/surroundWith/Runnable_after.java
@@ -0,0 +1,35 @@
+class External {
+ private int member;
+
+ External() {
+ member = 0;
+ }
+
+ void testMethod(final int param1, int param2) {
+ final int local1 = 0;
+ int local2 = 123;
+ int local3 = local1;
+
+ {
+ int innerLocal1 = 0;
+ int innerLocal2 = 7;
+ int innerLocal3 = 0;
+ int innerLocal4 = 7;
+ String incorrect;
+
+ Runnable runnable = new Runnable() {
+ public void run() {
+ int insideRunnable1 = param1 + ((++innerLocal2) << param2);
+ param2 = local1 * insideRunnable1;
+ local3 = param2;
+ int insideRunnable2 = local2;
+ innerLocal1--;
+ --innerLocal3;
+ innerLocal4++;
+ incorrect = "misplaced initialization";
+ }
+ };
+ local2 = local3;
+ }
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/surroundWith/TryCatch1.java b/java/java-tests/testData/codeInsight/surroundWith/TryCatch1.java
new file mode 100644
index 000000000000..a56ecab4ceb3
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/surroundWith/TryCatch1.java
@@ -0,0 +1,5 @@
+class A{
+ void foo(){
+ f();
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/surroundWith/TryCatch1_after.java b/java/java-tests/testData/codeInsight/surroundWith/TryCatch1_after.java
new file mode 100644
index 000000000000..f138446a46d3
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/surroundWith/TryCatch1_after.java
@@ -0,0 +1,9 @@
+class A{
+ void foo(){
+ try {
+ f();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/JavaSurroundWithTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/JavaSurroundWithTest.java
new file mode 100644
index 000000000000..e21463047534
--- /dev/null
+++ b/java/java-tests/testSrc/com/intellij/codeInsight/JavaSurroundWithTest.java
@@ -0,0 +1,42 @@
+// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
+package com.intellij.codeInsight;
+
+import com.intellij.JavaTestUtil;
+import com.intellij.codeInsight.generation.surroundWith.JavaWithIfElseExpressionSurrounder;
+import com.intellij.codeInsight.generation.surroundWith.JavaWithRunnableSurrounder;
+import com.intellij.codeInsight.generation.surroundWith.JavaWithTryCatchSurrounder;
+import com.intellij.codeInsight.generation.surroundWith.SurroundWithHandler;
+import com.intellij.lang.surroundWith.Surrounder;
+import com.intellij.testFramework.LightJavaCodeInsightTestCase;
+import org.jetbrains.annotations.NotNull;
+
+public class JavaSurroundWithTest extends LightJavaCodeInsightTestCase {
+ @Override
+ protected @NotNull String getTestDataPath() {
+ return JavaTestUtil.getJavaTestDataPath();
+ }
+
+ public void testTryCatch1() {
+ doTest(new JavaWithTryCatchSurrounder());
+ }
+
+ public void testRunnable() {
+ doTest(new JavaWithRunnableSurrounder());
+ }
+
+ public void testBlockedByPSI() {
+ doTest(new JavaWithIfElseExpressionSurrounder());
+ }
+
+ private void doTest(Surrounder handler) {
+ String baseName = getBaseName();
+ configureByFile(baseName + "." + "java");
+ SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), handler);
+ checkResultByFile(baseName + "_after." + "java");
+ }
+
+ private String getBaseName() {
+ return "/codeInsight/surroundWith/" + getTestName(false);
+ }
+}
+