**Build scan:**
https://gradle-enterprise.elastic.co/s/zx4fmb447h4wo/tests/:server:test/org.elasticsearch.indices.recovery.MultiFileWriterTests/testFailsToWriteFileWithIncorrectChecksum

**Reproduction line:**
```
./gradlew ':server:test' --tests "org.elasticsearch.indices.recovery.MultiFileWriterTests.testFailsToWriteFileWithIncorrectChecksum" -Dtests.seed=9388857843028FC5 -Dtests.locale=pl -Dtests.timezone=America/Vancouver -Druntime.java=17
```

**Applicable branches:**
main

**Reproduces locally?:**
Yes

**Failure history:**
https://gradle-enterprise.elastic.co/scans/tests?tests.container=org.elasticsearch.indices.recovery.MultiFileWriterTests&tests.test=testFailsToWriteFileWithIncorrectChecksum

**Failure excerpt:**
```
org.mockito.exceptions.base.MockitoException: 
Cannot mock/spy class org.apache.lucene.tests.store.RawDirectoryWrapper
Mockito cannot mock/spy because :
 - final class

  at __randomizedtesting.SeedInfo.seed([9388857843028FC5:8DEE8C2C936F739F]:0)
  at org.elasticsearch.indices.recovery.MultiFileWriterTests.setUp(MultiFileWriterTests.java:46)
  at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(null:-1)
  at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:568)
  at com.carrotsearch.randomizedtesting.RandomizedRunner.invoke(RandomizedRunner.java:1758)
  at com.carrotsearch.randomizedtesting.RandomizedRunner$9.evaluate(RandomizedRunner.java:980)
  at com.carrotsearch.randomizedtesting.RandomizedRunner$10.evaluate(RandomizedRunner.java:996)
  at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
  at org.apache.lucene.tests.util.TestRuleSetupTeardownChained$1.evaluate(TestRuleSetupTeardownChained.java:44)
  at org.apache.lucene.tests.util.AbstractBeforeAfterRule$1.evaluate(AbstractBeforeAfterRule.java:43)
  at org.apache.lucene.tests.util.TestRuleThreadAndTestName$1.evaluate(TestRuleThreadAndTestName.java:45)
  at org.apache.lucene.tests.util.TestRuleIgnoreAfterMaxFailures$1.evaluate(TestRuleIgnoreAfterMaxFailures.java:60)
  at org.apache.lucene.tests.util.TestRuleMarkFailure$1.evaluate(TestRuleMarkFailure.java:44)
  at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
  at com.carrotsearch.randomizedtesting.ThreadLeakControl$StatementRunner.run(ThreadLeakControl.java:390)
  at com.carrotsearch.randomizedtesting.ThreadLeakControl.forkTimeoutingTask(ThreadLeakControl.java:843)
  at com.carrotsearch.randomizedtesting.ThreadLeakControl$3.evaluate(ThreadLeakControl.java:490)
  at com.carrotsearch.randomizedtesting.RandomizedRunner.runSingleTest(RandomizedRunner.java:955)
  at com.carrotsearch.randomizedtesting.RandomizedRunner$5.evaluate(RandomizedRunner.java:840)
  at com.carrotsearch.randomizedtesting.RandomizedRunner$6.evaluate(RandomizedRunner.java:891)
  at com.carrotsearch.randomizedtesting.RandomizedRunner$7.evaluate(RandomizedRunner.java:902)
  at org.apache.lucene.tests.util.AbstractBeforeAfterRule$1.evaluate(AbstractBeforeAfterRule.java:43)
  at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
  at org.apache.lucene.tests.util.TestRuleStoreClassName$1.evaluate(TestRuleStoreClassName.java:38)
  at com.carrotsearch.randomizedtesting.rules.NoShadowingOrOverridesOnMethodsRule$1.evaluate(NoShadowingOrOverridesOnMethodsRule.java:40)
  at com.carrotsearch.randomizedtesting.rules.NoShadowingOrOverridesOnMethodsRule$1.evaluate(NoShadowingOrOverridesOnMethodsRule.java:40)
  at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
  at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
  at org.apache.lucene.tests.util.TestRuleAssertionsRequired$1.evaluate(TestRuleAssertionsRequired.java:53)
  at org.apache.lucene.tests.util.AbstractBeforeAfterRule$1.evaluate(AbstractBeforeAfterRule.java:43)
  at org.apache.lucene.tests.util.TestRuleMarkFailure$1.evaluate(TestRuleMarkFailure.java:44)
  at org.apache.lucene.tests.util.TestRuleIgnoreAfterMaxFailures$1.evaluate(TestRuleIgnoreAfterMaxFailures.java:60)
  at org.apache.lucene.tests.util.TestRuleIgnoreTestSuites$1.evaluate(TestRuleIgnoreTestSuites.java:47)
  at com.carrotsearch.randomizedtesting.rules.StatementAdapter.evaluate(StatementAdapter.java:36)
  at com.carrotsearch.randomizedtesting.ThreadLeakControl$StatementRunner.run(ThreadLeakControl.java:390)
  at com.carrotsearch.randomizedtesting.ThreadLeakControl.lambda$forkTimeoutingTask$0(ThreadLeakControl.java:850)
  at java.lang.Thread.run(Thread.java:833)

```
server/src/test/java/org/elasticsearch/indices/recovery/MultiFileWriterTests.java
@Override
    public void setUp() throws Exception {
        super.setUp();
        indexShard = newShard(true);
        directory = newFSDirectory(indexShard.shardPath().resolveIndex());
        directorySpy = spy(directory);
        store = createStore(indexShard.shardId(), indexShard.indexSettings(), directorySpy);
    }
    @Override
    public void tearDown() throws Exception {
        super.tearDown();
        directory.close();
        closeShards(indexShard);
    }
    public void testWritesFile() throws IOException {
        var fileWriter = createMultiFileWriter(true);
        var file = createFile("file");
        fileWriter.writeFile(file.metadata, 10, new ByteArrayInputStream(file.bytes));
        verify(directorySpy).createOutput("temp_file", IOContext.DEFAULT);
        verify(directorySpy).sync(Collections.singleton("temp_file"));
    }
    public void testFailsToWriteFileWithIncorrectChecksum() throws IOException {
        var fileWriter = createMultiFileWriter(true);
        var file = createFile("file");
        expectThrows(
            CorruptIndexException.class,
            () -> fileWriter.writeFile(withWrongChecksum(file.metadata), 10, new ByteArrayInputStream(file.bytes))
        );
        verify(directorySpy).createOutput("temp_file", IOContext.DEFAULT);
        verify(directorySpy).deleteFile("temp_file");