From 04ee374e7f3049284b1a9c1cf06884b8c9573103 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 14 Apr 2017 10:51:43 +0200 Subject: [PATCH] Polish "Add slice test annotation for LDAP" Closes gh-8536 --- .../main/asciidoc/spring-boot-features.adoc | 98 +++++++++---------- .../ldap/DataLdapTestIntegrationTests.java | 9 +- 2 files changed, 55 insertions(+), 52 deletions(-) diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index f9fd2fb733..761c69def0 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -5702,55 +5702,6 @@ A list of the auto-configuration that is enabled by `@JdbcTest` can be -[[boot-features-testing-spring-boot-applications-testing-autoconfigured-ldap-test]] -==== Auto-configured Data LDAP tests -`@DataLdapTest` can be used if you want to test LDAP applications. By default, it will -configure an in-memory embedded LDAP (if available), configure a `LdapTemplate`, scan -for `@Entry` classes and configure Spring Data LDAP repositories. Regular -`@Component` beans will not be loaded into the `ApplicationContext`: - -[source,java,indent=0] ----- - import org.junit.runner.RunWith; - import org.springframework.beans.factory.annotation.Autowired; - import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest; - import org.springframework.ldap.core.LdapTemplate; - import org.springframework.test.context.junit4.SpringRunner; - - @RunWith(SpringRunner.class) - @DataLdapTest - public class ExampleDataLdapTests { - - @Autowired - private LdapTemplate ldapTemplate; - - // - } ----- - -In-memory embedded LDAP generally works well for tests since it is fast and doesn't -require any developer installation. If, however, you prefer to run tests against a real -LDAP server you should exclude the embedded LDAP auto-configuration: - -[source,java,indent=0] ----- - import org.junit.runner.RunWith; - import org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration; - import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest; - import org.springframework.test.context.junit4.SpringRunner; - - @RunWith(SpringRunner.class) - @DataLdapTest(excludeAutoConfiguration = EmbeddedLdapAutoConfiguration.class) - public class ExampleDataLdapNonEmbeddedTests { - - } ----- - -A list of the auto-configuration that is enabled by `@DataLdapTest` can be -<>. - - - [[boot-features-testing-spring-boot-applications-testing-autoconfigured-mongo-test]] ==== Auto-configured Data MongoDB tests `@DataMongoTest` can be used if you want to test MongoDB applications. By default, it will @@ -5853,6 +5804,55 @@ A list of the auto-configuration that is enabled by `@DataNeo4jTest` can be +[[boot-features-testing-spring-boot-applications-testing-autoconfigured-ldap-test]] +==== Auto-configured Data LDAP tests +`@DataLdapTest` can be used if you want to test LDAP applications. By default, it will +configure an in-memory embedded LDAP (if available), a `LdapTemplate`, scan for `@Entry` +classes and configure Spring Data LDAP repositories. Regular `@Component` beans will not +be loaded into the `ApplicationContext`: + +[source,java,indent=0] +---- + import org.junit.runner.RunWith; + import org.springframework.beans.factory.annotation.Autowired; + import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest; + import org.springframework.ldap.core.LdapTemplate; + import org.springframework.test.context.junit4.SpringRunner; + + @RunWith(SpringRunner.class) + @DataLdapTest + public class ExampleDataLdapTests { + + @Autowired + private LdapTemplate ldapTemplate; + + // + } +---- + +In-memory embedded LDAP generally works well for tests since it is fast and doesn't +require any developer installation. If, however, you prefer to run tests against a real +LDAP server you should exclude the embedded LDAP auto-configuration: + +[source,java,indent=0] +---- + import org.junit.runner.RunWith; + import org.springframework.boot.autoconfigure.ldap.embedded.EmbeddedLdapAutoConfiguration; + import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest; + import org.springframework.test.context.junit4.SpringRunner; + + @RunWith(SpringRunner.class) + @DataLdapTest(excludeAutoConfiguration = EmbeddedLdapAutoConfiguration.class) + public class ExampleDataLdapNonEmbeddedTests { + + } +---- + +A list of the auto-configuration that is enabled by `@DataLdapTest` can be +<>. + + + [[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-client]] ==== Auto-configured REST clients The `@RestClientTest` annotation can be used if you want to test REST clients. By default diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTestIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTestIntegrationTests.java index 122a10f119..3108522626 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTestIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/data/ldap/DataLdapTestIntegrationTests.java @@ -16,6 +16,8 @@ package org.springframework.boot.test.autoconfigure.data.ldap; +import java.util.Optional; + import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -59,10 +61,11 @@ public class DataLdapTestIntegrationTests { @Test public void testRepository() { LdapQuery ldapQuery = LdapQueryBuilder.query().where("cn").is("Bob Smith"); - ExampleEntry entry = this.exampleRepository.findOne(ldapQuery); - assertThat(entry.getDn()) + Optional entry = this.exampleRepository.findOne(ldapQuery); + assertThat(entry.isPresent()); + assertThat(entry.get().getDn()) .isEqualTo(LdapUtils.newLdapName("cn=Bob Smith,ou=company1,c=Sweden,dc=spring,dc=org")); - assertThat(this.ldapTemplate.findOne(ldapQuery, ExampleEntry.class).getDn()) + assertThat(this.ldapTemplate.findOne(ldapQuery, ExampleEntry.class) .getDn()) .isEqualTo(LdapUtils.newLdapName("cn=Bob Smith,ou=company1,c=Sweden,dc=spring,dc=org")); }