-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(jacoco): user rest controller test case add
- Loading branch information
Showing
4 changed files
with
113 additions
and
8 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
testing/jacoco/src/test/java/com/gmoon/jacoco/global/CustomWithMockUser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.gmoon.jacoco.global; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.springframework.core.annotation.AliasFor; | ||
import org.springframework.security.test.context.support.WithSecurityContext; | ||
|
||
import com.gmoon.jacoco.users.domain.Role; | ||
|
||
@Inherited | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@WithSecurityContext(factory = CustomWithMockUserSecurityContextFactory.class) | ||
public @interface CustomWithMockUser { | ||
|
||
@AliasFor(attribute = "userId") | ||
String value() default ""; | ||
|
||
String userId() default ""; | ||
|
||
Role role() default Role.USER; | ||
} |
39 changes: 39 additions & 0 deletions
39
...acoco/src/test/java/com/gmoon/jacoco/global/CustomWithMockUserSecurityContextFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.gmoon.jacoco.global; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.security.authentication.TestingAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.test.context.support.WithSecurityContextFactory; | ||
|
||
import com.gmoon.jacoco.users.domain.Role; | ||
import com.gmoon.jacoco.users.domain.User; | ||
|
||
public class CustomWithMockUserSecurityContextFactory implements WithSecurityContextFactory<CustomWithMockUser> { | ||
|
||
@Override | ||
public SecurityContext createSecurityContext(CustomWithMockUser annotation) { | ||
Authentication authentication = createAuthentication(annotation); | ||
|
||
SecurityContext context = SecurityContextHolder.createEmptyContext(); | ||
context.setAuthentication(authentication); | ||
return context; | ||
} | ||
|
||
private Authentication createAuthentication(CustomWithMockUser annotation) { | ||
String username = annotation.userId(); | ||
Role role = annotation.role(); | ||
|
||
User principal = User.builder() | ||
.id(username) | ||
.username(username) | ||
.role(role) | ||
.build(); | ||
|
||
Authentication authentication = new TestingAuthenticationToken(principal, null, List.of(role)); | ||
authentication.setAuthenticated(true); | ||
return authentication; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
testing/jacoco/src/test/java/com/gmoon/jacoco/users/application/UserServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.gmoon.jacoco.users.application; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class UserServiceTest { | ||
|
||
@Test | ||
void updatePassword() { | ||
UserService service = Mockito.mock(UserService.class); | ||
|
||
service.updatePassword("admin", "password", "newPassword"); | ||
} | ||
} |