我正在尝试使用Spring的自动配置来启动嵌入式LDAP服务器并使用spring-data-ldap访问它。但是,自动装配字段,存储库(LdapRepository的一个实例)和ldapTemplate(LdapTemplate的一个实例)为空。
例如,
spring.ldap.model.UserTest > testSaveUser FAILED
java.lang.NullPointerException at UserTest.java:32
我想念什么?
build.gradle
plugins {
id 'org.springframework.boot' version '2.0.6.RELEASE' apply false
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
repositories {
jcenter()
}
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
dependencies {
compile 'org.springframework.data:spring-data-ldap'
compile 'com.unboundid:unboundid-ldapsdk'
testCompile 'org.springframework.boot:spring-boot-starter-data-ldap'
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
在src / main / java / spring / ldap / model下:
Config.java
包spring.ldap.model;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.ldap.repository.config.EnableLdapRepositories;
@Configuration
@EnableLdapRepositories
public class Config { }
User.java
package spring.ldap.model;
import javax.naming.Name;
import org.springframework.ldap.odm.annotations.Attribute;
import org.springframework.ldap.odm.annotations.DnAttribute;
import org.springframework.ldap.odm.annotations.Entry;
import org.springframework.ldap.odm.annotations.Id;
@Entry(objectClasses = { "person", "top" }, base="dc=spring,dc=ldap,dc=model" )
public class User {
@Id private Name dn;
@Attribute(name = "cn") @DnAttribute(value = "cn", index = 0) private String userName;
@Attribute(name = "sn") private String fullName;
public Name getDn() { return dn; }
public void setDn(Name dn) { this.dn = dn; }
public String getUsername() { return userName; }
public void setUsername(String userName) { this.userName = userName; }
public String getFullName() { return fullName; }
public void setFullName(String fullName) { this.fullName = fullName; }
}
UserRepository.java
package spring.ldap.model;
import org.springframework.data.ldap.repository.LdapRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends LdapRepository<User> { }
在src / test / java / spring / ldap / model下:
UserTest.java
package spring.ldap.model;
import static org.junit.Assert.assertNotNull;
import javax.naming.Name;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.ldap.DataLdapTest;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.support.LdapNameBuilder;
import org.springframework.test.context.ContextConfiguration;
@DataLdapTest()
@ContextConfiguration(classes = Config.class)
public class UserTest {
@Autowired UserRepository repository;
@Autowired LdapTemplate ldapTemplate;
@Before
public void SetUp() {
LdapNameBuilder builder = LdapNameBuilder.newInstance();
builder.add("dc", "model");
builder.add("dc", "ldap");
builder.add("dc", "spring");
Name dn = builder.build();
DirContextAdapter context = new DirContextAdapter(dn);
context.setAttributeValues("objectclass", new String[] { "top", "domain", "extensibleObject" });
context.setAttributeValue("dc", "spring");
ldapTemplate.bind(context);
}
@Test public void testSaveUser() {
User user = new User();
user.setUsername("ncornish");
user.setFullName("Nicholas Cornish");
repository.save(user);
assertNotNull("Id was not generated!", user.getDn());
}
}
在src / test / resources下:
application.properties
spring.ldap.embedded.base-dn=dc=spring,dc=ldap,dc=model
spring.ldap.embedded.credential.username=uid=admin
spring.ldap.embedded.credential.password=secret
答案 0 :(得分:0)
感谢JB Nezit,您的评论
您的测试没有在Spring上下文中运行,因为它没有注释 使用@RunWith(SpringRunner.class)。因此,不会自动接线 里面的任何东西。
解决了问题。即。将以下内容添加到测试类中
@RunWith(SpringRunner.class)
答案 1 :(得分:0)
必须将这些注释放在测试类上下文中:
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.4
亲切的问候。