Romans
Romans
Explore posts from servers
JCHJava Community | Help. Code. Learn.
Created by Romans on 10/24/2024 in #java-help
[Sping Boot/Mockito/Junit] Lombok Coverege testing
Hello! I'm doing Tests with Mockito, Spring Boot and Lombok. Someone knows how to test a simple class base with only attributes, that is marked with @Data?
@Data
public clòass MyCLass{
private final UUID id = UUID.randomUID();
private String name;
private String surname;
private List<Strings> groups;
}
@Data
public clòass MyCLass{
private final UUID id = UUID.randomUID();
private String name;
private String surname;
private List<Strings> groups;
}
- I've put all the setters and getters for all attributes (except id). - Ive set the toString(). - then created 3 instances and set for eachone a different name. - se the .equals from then (the first with itself), first with second, second with third, first with third. - the put for each instance the .hashCode(). - put the .canEqual as the equals. Running the coverage it still gives Line: 100% 9/9 Branch: 30% 16/52 And the @ Data is marked as "Partial" Someone have tips on how could i fill the Branch? (if i remove all the equals, can equal and hascode it goes 0/52) Thank you so much
4 replies
JCHJava Community | Help. Code. Learn.
Created by Romans on 10/7/2024 in #java-help
Spring(Boot) Security preauthorize auth return 200 on "false" instead of 403
Hello! Hope this is the right section .I've a "problem" with spring boot and spring security. In controllers i've the classic @PreAuthorize("hasAnyAuthority(....)"). I would like to receive the error 403 (in order to redirect to the 403.ftlh in case of no authentication). Just now instead if i open the page that does not pass the @PreAuthorize, returns the page with status 200, but blank, for because any api in the controlled is executed (correctly). I tried to put in the security config an http.executionHandling() but it won't execute anyway. The basic requests i've is:
http.authorizeHttpRequest(authorize -> authorize.anymatchers("/img/","js/",etc).permitAll()
.anyMatchers("/","/logout","/error/**").permitAll()
.anyRequest.authenticated())
http.authorizeHttpRequest(authorize -> authorize.anymatchers("/img/","js/",etc).permitAll()
.anyMatchers("/","/logout","/error/**").permitAll()
.anyRequest.authenticated())
But still get 200 (and blank page) opening that page
10 replies
JCHJava Community | Help. Code. Learn.
Created by Romans on 7/16/2024 in #java-help
Spring Boot @Mapping gives no read accessor error
Hello! I'm trying to use @mapping to map a DTo from entities My Main DTO is
public class MainDto {
private ProjectDTO project;
private TaskDTO task;
}
public class MainDto {
private ProjectDTO project;
private TaskDTO task;
}
DTOs:
public class ProjectDTO {
private UUID projectId;
private Second second;
private String name;
private String desc;
}
public class ProjectDTO {
private UUID projectId;
private Second second;
private String name;
private String desc;
}
public class SecondDTO {
private UUID secondId;
private String name;
private String desc;
}
public class SecondDTO {
private UUID secondId;
private String name;
private String desc;
}
public class TaskDTO {
private UUID taskId;
private String taskName;
}
public class TaskDTO {
private UUID taskId;
private String taskName;
}
Entities:
@Entity
public class ProjectEntity {
@Id
@Column(name = "project_id")
private UUID projectId;

@Column(name = "second_id")
private UUID secondId;

.... etc
}
@Entity
public class ProjectEntity {
@Id
@Column(name = "project_id")
private UUID projectId;

@Column(name = "second_id")
private UUID secondId;

.... etc
}
@Entity
public class SecondEntity {
@Id
@Column(name = "second_id")
private UUID secondId;

.... etc
}
@Entity
public class SecondEntity {
@Id
@Column(name = "second_id")
private UUID secondId;

.... etc
}
@Entity
public class TaskEntity {
...
}
@Entity
public class TaskEntity {
...
}
On an other file i set ans use the entities on repos, then i want to return the MainDTO:
public class main {
ProjectEntity projEntity = ...
SecondEntity sENtity = ...
TaskEntity tEntity = ...
....
}
public class main {
ProjectEntity projEntity = ...
SecondEntity sENtity = ...
TaskEntity tEntity = ...
....
}
then i got problems on mapping them: - If in main i've: MainDTO mDTO(ProjectEntity projEntity, SecondEntity sEntity, TaskEntity tEntity); it returns correctly filled the ProjectDTO and TaskDTO, except for the ProjectDTO Second, that is null. - If i try
@Mapping(target = "project.second", source = "sEntity")
MainDTO mDTO(ProjectEntity projEntity, SecondEntity sEntity, TaskEntity tEntity);
@Mapping(target = "project.second", source = "sEntity")
MainDTO mDTO(ProjectEntity projEntity, SecondEntity sEntity, TaskEntity tEntity);
works, it fill the Second, but the rest of projectDTO is empty (TaskDTO is filled). - If i try something like
@Mapping(target = "project", source = "projEntity")
@Mapping(target = "project.second", source = "sEntity")
MainDTO mDTO(ProjectEntity projEntity, SecondEntity sEntity, TaskEntity tEntity);
@Mapping(target = "project", source = "projEntity")
@Mapping(target = "project.second", source = "sEntity")
MainDTO mDTO(ProjectEntity projEntity, SecondEntity sEntity, TaskEntity tEntity);
or
@Mapping(target = "project.description", source = "projEntity")
@Mapping(target = "project.second", source = "sEntity")
MainDTO mDTO(ProjectEntity projEntity, SecondEntity sEntity, TaskEntity tEntity);
@Mapping(target = "project.description", source = "projEntity")
@Mapping(target = "project.second", source = "sEntity")
MainDTO mDTO(ProjectEntity projEntity, SecondEntity sEntity, TaskEntity tEntity);
it gives the error: java: no read accessor for property "project" in tagret type Someone has tips for this? THank you so much
8 replies