Commit 728b5223 authored by Phillip Webb's avatar Phillip Webb

Include scope support on ApplicationContextAssert

Update `ApplicationContextAssert` with support for scopes. Allows
tests to consider the all ancestors, or limit assertions to just the
current context.

Fixes gh-12015
parent d6858ae1
/*
* 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.
......@@ -16,10 +16,13 @@
package org.springframework.boot.test.context.assertj;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.boot.test.context.assertj.ApplicationContextAssert.Scope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
......@@ -36,10 +39,25 @@ public class ApplicationContextAssertTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
private StaticApplicationContext context = new StaticApplicationContext();
private StaticApplicationContext parent;
private StaticApplicationContext context;
private RuntimeException failure = new RuntimeException();
@Before
public void setup() {
this.parent = new StaticApplicationContext();
this.context = new StaticApplicationContext();
this.context.setParent(this.parent);
}
@After
public void cleanup() {
this.context.close();
this.parent.close();
}
@Test
public void createWhenApplicationContextIsNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class);
......@@ -110,6 +128,22 @@ public class ApplicationContextAssertTests {
assertThat(getAssert(this.failure)).hasSingleBean(Foo.class);
}
@Test
public void hasSingleBeanWhenInParentShouldFail() {
this.parent.registerSingleton("foo", Foo.class);
this.context.registerSingleton("bar", Foo.class);
this.thrown.expect(AssertionError.class);
this.thrown.expectMessage("but found:");
assertThat(getAssert(this.context)).hasSingleBean(Foo.class);
}
@Test
public void hasSingleBeanWithLimitedScopeWhenInParentShouldPass() {
this.parent.registerSingleton("foo", Foo.class);
this.context.registerSingleton("bar", Foo.class);
assertThat(getAssert(this.context)).hasSingleBean(Foo.class, Scope.NO_ANCESTORS);
}
@Test
public void doesNotHaveBeanOfTypeWhenHasNoBeanOfTypeShouldPass() {
assertThat(getAssert(this.context)).doesNotHaveBean(Foo.class);
......@@ -132,6 +166,21 @@ public class ApplicationContextAssertTests {
assertThat(getAssert(this.failure)).doesNotHaveBean(Foo.class);
}
@Test
public void doesNotHaveBeanOfTypeWhenInParentShouldFail() {
this.thrown.expect(AssertionError.class);
this.thrown.expectMessage("but found");
this.parent.registerSingleton("foo", Foo.class);
assertThat(getAssert(this.context)).doesNotHaveBean(Foo.class);
}
@Test
public void doesNotHaveBeanOfTypeWithLimitedScopeWhenInParentShouldPass() {
this.parent.registerSingleton("foo", Foo.class);
assertThat(getAssert(this.context)).doesNotHaveBean(Foo.class,
Scope.NO_ANCESTORS);
}
@Test
public void doesNotHaveBeanOfNameWhenHasNoBeanOfTypeShouldPass() {
assertThat(getAssert(this.context)).doesNotHaveBean("foo");
......@@ -204,6 +253,36 @@ public class ApplicationContextAssertTests {
assertThat(getAssert(this.failure)).getBean(Foo.class);
}
@Test
public void getBeanOfTypeWhenInParentShouldReturnBeanAssert() {
this.parent.registerSingleton("foo", Foo.class);
assertThat(getAssert(this.context)).getBean(Foo.class).isNotNull();
}
@Test
public void getBeanOfTypeWhenInParentWithLimtedScopeShouldReturnNullAssert() {
this.parent.registerSingleton("foo", Foo.class);
assertThat(getAssert(this.context)).getBean(Foo.class, Scope.NO_ANCESTORS)
.isNull();
}
@Test
public void getBeanOfTypeWhenHasMultipleBeansIncludingParentShouldFail() {
this.parent.registerSingleton("foo", Foo.class);
this.context.registerSingleton("bar", Foo.class);
this.thrown.expect(AssertionError.class);
this.thrown.expectMessage("but found");
assertThat(getAssert(this.context)).getBean(Foo.class);
}
@Test
public void getBeanOfTypeWithLimitedScopeWhenHasMultipleBeansIncludingParentShouldReturnBeanAssert() {
this.parent.registerSingleton("foo", Foo.class);
this.context.registerSingleton("bar", Foo.class);
assertThat(getAssert(this.context)).getBean(Foo.class, Scope.NO_ANCESTORS)
.isNotNull();
}
@Test
public void getBeanOfNameWhenHasBeanShouldReturnBeanAssert() {
this.context.registerSingleton("foo", Foo.class);
......@@ -274,6 +353,22 @@ public class ApplicationContextAssertTests {
assertThat(getAssert(this.failure)).getBeans(Foo.class);
}
@Test
public void getBeansShouldIncludeBeansFromParentScope() {
this.parent.registerSingleton("foo", Foo.class);
this.context.registerSingleton("bar", Foo.class);
assertThat(getAssert(this.context)).getBeans(Foo.class).hasSize(2)
.containsKeys("foo", "bar");
}
@Test
public void getBeansWithLimitedScopeShouldNotIncludeBeansFromParentScope() {
this.parent.registerSingleton("foo", Foo.class);
this.context.registerSingleton("bar", Foo.class);
assertThat(getAssert(this.context)).getBeans(Foo.class, Scope.NO_ANCESTORS)
.hasSize(1).containsKeys("bar");
}
@Test
public void getFailureWhenFailedShouldReturnFailure() {
assertThat(getAssert(this.failure)).getFailure().isSameAs(this.failure);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment