Meine Entity:
und das Repository:
und eine entsprechender Test:
Java:
@Entity
@Table(name="user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@JoinColumn(name = "created_by", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
private User created_by;
@OneToMany(mappedBy = "created_by", fetch = FetchType.LAZY)
private List<User> userList;
@NotNull
@CreationTimestamp
private LocalDateTime created_at;
@OneToOne(cascade = CascadeType.REMOVE, fetch=FetchType.LAZY)
@JoinColumn(name="updated_by")
private User updated_by;
private LocalDateTime updated_at;
@OneToOne(cascade = CascadeType.REMOVE, fetch=FetchType.LAZY)
@JoinColumn(name="deleted_by")
private User deleted_by;
private LocalDateTime deleted_at;
@NotNull
@NotBlank
@Length(min=3, max=255)
private String firstname;
@NotNull
@NotBlank
@Length(min=3, max=255)
private String lastname;
@NotNull
@NotBlank
@Length(min=3, max=255)
@Column(unique=true)
private String email;
@NotNull
@NotBlank
@Length(min=3, max=255)
private String password;
public User() {
}
public User(@NotNull @NotBlank @Length(min = 3, max = 255) String firstname,
@NotNull @NotBlank @Length(min = 3, max = 255) String lastname,
@NotNull @NotBlank @Length(min = 3, max = 255) String email,
@NotNull @NotBlank @Length(min = 3, max = 255) String password) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.password = password;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public LocalDateTime getCreatedAt() {
return created_at;
}
public void setCreatedAt(LocalDateTime created_at) {
this.created_at = created_at;
}
public User getUpdatedBy() {
return updated_by;
}
public void setUpdatedBy(User updated_by) {
this.updated_by = updated_by;
}
public LocalDateTime getUpdatedAt() {
return updated_at;
}
public void setUpdatedAt(LocalDateTime updated_at) {
this.updated_at = updated_at;
}
public User getDeletedBy() {
return deleted_by;
}
public void setDeletedBy(User deleted_by) {
this.deleted_by = deleted_by;
}
public LocalDateTime getDeletedAt() {
return deleted_at;
}
public void setDeletedAt(LocalDateTime deleted_at) {
this.deleted_at = deleted_at;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public User getCreatedBy() {
return created_by;
}
public void setCreatedBy(User created_by) {
this.created_by = created_by;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((created_at == null) ? 0 : created_at.hashCode());
result = prime * result + ((created_by == null) ? 0 : created_by.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((firstname == null) ? 0 : firstname.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((lastname == null) ? 0 : lastname.hashCode());
result = prime * result + ((password == null) ? 0 : password.hashCode());
result = prime * result + ((updated_at == null) ? 0 : updated_at.hashCode());
result = prime * result + ((userList == null) ? 0 : userList.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (created_at == null) {
if (other.created_at != null)
return false;
} else if (!created_at.equals(other.created_at))
return false;
if (created_by == null) {
if (other.created_by != null)
return false;
} else if (!created_by.equals(other.created_by))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (firstname == null) {
if (other.firstname != null)
return false;
} else if (!firstname.equals(other.firstname))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (lastname == null) {
if (other.lastname != null)
return false;
} else if (!lastname.equals(other.lastname))
return false;
if (password == null) {
if (other.password != null)
return false;
} else if (!password.equals(other.password))
return false;
if (updated_at == null) {
if (other.updated_at != null)
return false;
} else if (!updated_at.equals(other.updated_at))
return false;
if (userList == null) {
if (other.userList != null)
return false;
} else if (!userList.equals(other.userList))
return false;
return true;
}
@Override
public String toString() {
return "User [id=" + id + ", created_at=" + created_at + ", updated_at=" + updated_at + ", firstname="
+ firstname + ", lastname=" + lastname + ", email=" + email + ", password=" + password + "]";
}
}
und das Repository:
Code:
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
@Query("SELECT u FROM User AS u WHERE lower(u.email) LIKE lower(CONCAT('%',:email,'%'))")
public boolean existsByEmailRegEx(@Param("email") String email);
}
und eine entsprechender Test:
Code:
@SpringBootTest
class UserRepositoryTest {
@Autowired
UserService userService;
@Autowired
UserRepository userRepository;
@Test
void testExistsEmailIfEmailExists() throws ResourceNotFoundException, DuplicateException {
String firstname = "firstname";
String lastname = "lastname";
String email = "email@email.com";
String password = "password";
User user = new User(
firstname,
lastname,
email,
password
);
User createdUser = userService.createUser(null, user);
Optional<User> foundUser = userService.findUserById(createdUser.getId());
assertTrue( foundUser.isPresent(), "testExistsEmailIfEmailExists(): User (" + user + ") not stored");
// test 1
String testEmail1 = "email@email.com";
assertTrue(userRepository.existsByEmailRegEx(testEmail1), "Test 1 with '" + testEmail1 + "' not exists");
// test 2
String testEmail2 = " email@email.com ";
assertTrue(userRepository.existsByEmailRegEx(testEmail2), "Test 2 with '" + testEmail2 + "' not exists");
// test 3
String testEmail3 = " email@email.com";
assertTrue(userRepository.existsByEmailRegEx(testEmail3), "Test 3 with '" + testEmail3 + "' not exists");
// test 4
String testEmail4 = "email@email.com ";
assertTrue(userRepository.existsByEmailRegEx(testEmail4), "Test 4 with '" + testEmail4 + "' not exists");
// test 5
String testEmail5 = "EMAIL@EMAIL.com";
assertTrue(userRepository.existsByEmailRegEx(testEmail5), "Test 5 with '" + testEmail5 + "' not exists");
// clean up
userService.removeUser(foundUser.get().getId());
}
}
Also wenn ich es richtig verstehe liegt ein Cast-Fehler vor, aber Spring Data exists.... liefert boolean zurück, deshalb verstehe ich den Fehler nicht.java.lang.ClassCastException: class com.tmt.myapp.model.User cannot be cast to class java.lang.Boolean (com.tmt.myapp.model.User is in unnamed module of loader 'app'; java.lang.Boolean is in module java.base of loader 'bootstrap')
at com.sun.proxy.$Proxy118.existsByEmailRegEx(Unknown Source)
at com.tmt.myapp.repositories.UserRepositoryTest.testExistsEmailIfEmailExists(UserRepositoryTest.java:66)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:95)
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:91)
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:60)
at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:98)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:40)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:529)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:756)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)