This commit is contained in:
Rob Winch 2020-10-21 15:32:21 -05:00
parent 657adb29fe
commit 790516c50b
4 changed files with 13 additions and 11 deletions

View File

@ -16,11 +16,11 @@
package example; package example;
import java.util.List;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
/** /**
* A repository that integrates with Spring Security for accessing {@link Message}s. * A repository that integrates with Spring Security for accessing {@link Message}s.
* *

View File

@ -22,6 +22,8 @@ import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
/** /**
* A user.
*
* @author Rob Winch * @author Rob Winch
*/ */
@Entity @Entity
@ -40,7 +42,7 @@ public class User {
private String password; private String password;
public Long getId() { public Long getId() {
return id; return this.id;
} }
public void setId(Long id) { public void setId(Long id) {

View File

@ -16,18 +16,18 @@
package example; package example;
import java.lang.reflect.Method;
import java.util.List;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -45,7 +45,7 @@ public class SecurityMessageRepositoryTests {
void findAllOnlyToCurrentUser() { void findAllOnlyToCurrentUser() {
Method method = ReflectionUtils.findMethod(SecurityMessageRepositoryTests.class, "findAllOnlyToCurrentUser"); Method method = ReflectionUtils.findMethod(SecurityMessageRepositoryTests.class, "findAllOnlyToCurrentUser");
WithMockCustomUser withMockCustomUser = AnnotationUtils.findAnnotation(method, WithMockCustomUser.class); WithMockCustomUser withMockCustomUser = AnnotationUtils.findAnnotation(method, WithMockCustomUser.class);
List<Message> messages = repository.findAll(); List<Message> messages = this.repository.findAll();
assertThat(messages).hasSize(3); assertThat(messages).hasSize(3);
for (Message m : messages) { for (Message m : messages) {
assertThat(m.getTo().getId()).isEqualTo(withMockCustomUser.id()); assertThat(m.getTo().getId()).isEqualTo(withMockCustomUser.id());

View File

@ -16,11 +16,11 @@
package example; package example;
import org.springframework.security.test.context.support.WithSecurityContext;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import org.springframework.security.test.context.support.WithSecurityContext;
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory.class) @WithSecurityContext(factory = WithMockCustomUserSecurityContextFactory.class)
public @interface WithMockCustomUser { public @interface WithMockCustomUser {