Migrate from ExpectedException rule to AssertJ
Replace ExpectedException JUnit rules with AssertJ exception assertions. Closes gh-14336
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
* Copyright 2012-2018 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.
|
||||
@@ -17,9 +17,7 @@
|
||||
package sample.propertyvalidation;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
@@ -27,6 +25,7 @@ import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
/**
|
||||
* Tests for {@link SamplePropertyValidationApplication}.
|
||||
@@ -36,9 +35,6 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
public class SamplePropertyValidationApplicationTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
||||
@After
|
||||
@@ -62,17 +58,17 @@ public class SamplePropertyValidationApplicationTests {
|
||||
this.context.register(SamplePropertyValidationApplication.class);
|
||||
TestPropertyValues.of("sample.host:xxxxxx", "sample.port:9090")
|
||||
.applyTo(this.context);
|
||||
this.thrown.expect(BeanCreationException.class);
|
||||
this.thrown.expectMessage("Failed to bind properties under 'sample'");
|
||||
this.context.refresh();
|
||||
assertThatExceptionOfType(BeanCreationException.class)
|
||||
.isThrownBy(() -> this.context.refresh())
|
||||
.withMessageContaining("Failed to bind properties under 'sample'");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bindNullHost() {
|
||||
this.context.register(SamplePropertyValidationApplication.class);
|
||||
this.thrown.expect(BeanCreationException.class);
|
||||
this.thrown.expectMessage("Failed to bind properties under 'sample'");
|
||||
this.context.refresh();
|
||||
assertThatExceptionOfType(BeanCreationException.class)
|
||||
.isThrownBy(() -> this.context.refresh())
|
||||
.withMessageContaining("Failed to bind properties under 'sample'");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user