Skip to content

Commit

Permalink
Fix tests catching nested exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
hpoettker committed Jun 17, 2022
1 parent 08f8c75 commit 735f3ea
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 22 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -35,6 +35,7 @@
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.ScopeNotActiveException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -117,7 +118,9 @@ public void testIntentionallyBlowUpOnMissingContextWithProxyTargetClass() throws
SimpleHolder value = context.getBean(SimpleHolder.class);
assertEquals("JOB", value.call());
});
assertTrue(expectedException.getMessage().contains("job scope"));
assertTrue(expectedException instanceof ScopeNotActiveException);
String message = expectedException.getCause().getMessage();
assertTrue(message.contains("job scope"));
}

@Test
Expand All @@ -128,7 +131,9 @@ public void testIntentionallyBlowupWithForcedInterface() throws Exception {
SimpleHolder value = context.getBean(SimpleHolder.class);
assertEquals("JOB", value.call());
});
assertTrue(expectedException.getMessage().contains("job scope"));
assertTrue(expectedException instanceof ScopeNotActiveException);
String message = expectedException.getCause().getMessage();
assertTrue(message.contains("job scope"));
}

@Test
Expand All @@ -148,7 +153,9 @@ public void testIntentionallyBlowUpOnMissingContextWithInterface() throws Except
Callable<String> value = context.getBean(Callable.class);
assertEquals("JOB", value.call());
});
assertTrue(expectedException.getMessage().contains("job scope"));
assertTrue(expectedException instanceof ScopeNotActiveException);
String message = expectedException.getCause().getMessage();
assertTrue(message.contains("job scope"));
}

public void init(Class<?>... config) throws Exception {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,6 +28,7 @@
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.ScopeNotActiveException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -116,7 +117,9 @@ public void testIntentionallyBlowUpOnMissingContextWithProxyTargetClass() throws
SimpleHolder value = context.getBean(SimpleHolder.class);
assertEquals("STEP", value.call());
});
assertTrue(expectedException.getMessage().contains("step scope"));
assertTrue(expectedException instanceof ScopeNotActiveException);
String message = expectedException.getCause().getMessage();
assertTrue(message.contains("step scope"));
}

@Test
Expand All @@ -127,7 +130,9 @@ public void testIntentionallyBlowupWithForcedInterface() throws Exception {
SimpleHolder value = context.getBean(SimpleHolder.class);
assertEquals("STEP", value.call());
});
assertTrue(expectedException.getMessage().contains("step scope"));
assertTrue(expectedException instanceof ScopeNotActiveException);
String message = expectedException.getCause().getMessage();
assertTrue(message.contains("step scope"));
}

@Test
Expand All @@ -148,7 +153,9 @@ public void testIntentionallyBlowUpOnMissingContextWithInterface() throws Except
Callable<String> value = context.getBean(Callable.class);
assertEquals("STEP", value.call());
});
assertTrue(expectedException.getMessage().contains("step scope"));
assertTrue(expectedException instanceof ScopeNotActiveException);
String message = expectedException.getCause().getMessage();
assertTrue(message.contains("step scope"));
}

public void init(Class<?>... config) throws Exception {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -180,7 +180,7 @@ public void testProcessorNonTransactionalNotAllowedWithTransactionalReader() thr
fail("Expected BeanCreationException");
}
catch (BeanCreationException e) {
String msg = e.getMessage();
String msg = e.getRootCause().getMessage();
assertTrue("Wrong message: " + msg,
msg.contains("The field 'processor-transactional' cannot be false if 'reader-transactional"));
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2014 the original author or authors.
* Copyright 2009-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -58,7 +58,7 @@ public void testNextOutOfScope() {
fail("Error expected");
}
catch (BeanCreationException e) {
String message = e.getMessage();
String message = e.getRootCause().getMessage();
assertTrue("Wrong message: " + message, message
.matches(".*Missing state for \\[StateTransition: \\[state=.*s2, pattern=\\*, next=.*s3\\]\\]"));
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -622,8 +622,8 @@ public void write(List<? extends String> item) throws Exception {
StepExecution stepExecution = new StepExecution(step.getName(), jobExecution);
repository.add(stepExecution);
step.execute(stepExecution);
String message = stepExecution.getFailureExceptions().get(0).getMessage();
assertTrue("Wrong message: " + message, message.contains("Write error - planned but not skippable."));
String message = stepExecution.getFailureExceptions().get(0).getCause().getMessage();
assertEquals("Wrong message: " + message, "Write error - planned but not skippable.", message);

List<String> expectedOutput = Arrays.asList(StringUtils.commaDelimitedListToStringArray(""));
assertEquals(expectedOutput, written);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2021 the original author or authors.
* Copyright 2006-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,7 +48,7 @@ public void testSetMaxMessageLength() {
}
catch (MailException e) {
String msg = e.getMessage();
assertTrue("Wrong message: " + msg, msg.matches(".*SimpleMailMessage: f;.*"));
assertTrue("Wrong message: " + msg, msg.endsWith("SimpleMailMessage: f"));
}
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2021 the original author or authors.
* Copyright 2021-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -79,7 +79,7 @@ public class ChunkMessageItemWriterIntegrationTests {

@Before
public void setUp() throws Exception {
EmbeddedDatabase embeddedDatabase = new EmbeddedDatabaseBuilder()
EmbeddedDatabase embeddedDatabase = new EmbeddedDatabaseBuilder().generateUniqueName(true)
.addScript("/org/springframework/batch/core/schema-drop-hsqldb.sql")
.addScript("/org/springframework/batch/core/schema-hsqldb.sql").build();
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(embeddedDatabase);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -91,7 +91,7 @@ public void testNoReply() {
fail();
}
catch (MessagingException e) {
String message = e.getMessage();
String message = e.getCause().getMessage();
assertTrue("Wrong message: " + message, message.contains("replyChannel"));
}
Message<JobExecution> executionMessage = (Message<JobExecution>) responseChannel.receive(1000);
Expand Down
@@ -1,3 +1,18 @@
/*
* Copyright 2008-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.integration.launch;

import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -57,7 +72,7 @@ public void testNoReply() {
requestChannel.send(trigger);
}
catch (MessagingException e) {
String message = e.getMessage();
String message = e.getCause().getMessage();
assertTrue("Wrong message: " + message, message.contains("replyChannel"));
}
Message<JobExecution> executionMessage = (Message<JobExecution>) responseChannel.receive(1000);
Expand Down

0 comments on commit 735f3ea

Please sign in to comment.