-
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.
* Unit test HomeController * Run unit tests in github actions * Generate jacoco coverage reports * Unit test main classes * Update all unit tests
- Loading branch information
Showing
7 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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,13 @@ | ||
package org.example; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class DemoApplicationTest { | ||
|
||
@Test | ||
public void main() { | ||
// Act | ||
DemoApplication.main(new String[] {}); | ||
} | ||
|
||
} |
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,51 @@ | ||
package org.example; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.security.oauth2.core.oidc.user.OidcUser; | ||
import org.springframework.ui.Model; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.*; | ||
|
||
@SpringBootTest | ||
class HomeControllerTest { | ||
|
||
private HomeController homeController; | ||
|
||
@Mock | ||
private Model model; | ||
|
||
@Mock | ||
private OidcUser principal; | ||
|
||
@BeforeEach | ||
public void beforeEach(){ | ||
homeController = new HomeController(); | ||
} | ||
|
||
@Test | ||
public void testHomeModel(){ | ||
// Act | ||
String returnValue = homeController.home(model, principal); | ||
|
||
// Assert | ||
verify(model, times(1)).addAttribute(eq("profile"), Mockito.any()); | ||
assertEquals("index", returnValue); | ||
} | ||
|
||
@Test | ||
public void testHomeModelNullPrincipal(){ | ||
// Act | ||
String returnValue = homeController.home(model, null); | ||
|
||
// Assert | ||
verify(model, never()).addAttribute(eq("profile"), Mockito.any()); | ||
assertEquals("index", returnValue); | ||
} | ||
|
||
} |
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,74 @@ | ||
package org.example; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.ObjectWriter; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.slf4j.Logger; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.security.oauth2.core.oidc.user.OidcUser; | ||
import org.springframework.test.util.ReflectionTestUtils; | ||
import org.springframework.ui.Model; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.*; | ||
|
||
@SpringBootTest | ||
public class ProfileControllerTest { | ||
|
||
private ProfileController profileController; | ||
|
||
@Mock | ||
private Model model; | ||
|
||
@Mock | ||
private OidcUser oidcUser; | ||
|
||
@BeforeEach | ||
public void beforeEach(){ | ||
profileController = new ProfileController(); | ||
} | ||
|
||
@Test | ||
public void testProfileModel() { | ||
// Act | ||
String returnValue = profileController.profile(model, oidcUser); | ||
|
||
// Assert | ||
verify(model, times(1)).addAttribute(eq("profile"), Mockito.any()); | ||
verify(model, times(1)).addAttribute(eq("profileJson"), Mockito.any()); | ||
assertEquals("profile", returnValue); | ||
} | ||
|
||
@Test | ||
public void testProfileModel_Exception() throws JsonProcessingException { | ||
// Prepare | ||
// Mock Logger | ||
Logger mockedLogger = Mockito.mock(Logger.class); | ||
ReflectionTestUtils.setField(profileController, "log", mockedLogger); | ||
|
||
// Mocked ObjectWriter calls writeValueAsString() and throws JsonProcessingException | ||
ObjectWriter mockedWriter = Mockito.mock(ObjectWriter.class); | ||
when(mockedWriter.writeValueAsString(Mockito.any())).thenThrow(new JsonProcessingException("Error"){}); | ||
|
||
// Mocked ObjectMapper returns mocked ObjectWriter | ||
ObjectMapper mockedMapper = Mockito.mock(ObjectMapper.class); | ||
when(mockedMapper.writerWithDefaultPrettyPrinter()).thenReturn(mockedWriter); | ||
|
||
// Set mocked ObjectMapper on class through reflection | ||
ReflectionTestUtils.setField(profileController, "mapper", mockedMapper); | ||
|
||
// Act | ||
String returnValue = profileController.profile(model, oidcUser); | ||
|
||
// Assert | ||
verify(model, times(1)).addAttribute(eq("profile"), Mockito.any()); | ||
verify(model, times(1)).addAttribute(eq("profileJson"), Mockito.any()); | ||
assertEquals("profile", returnValue); | ||
} | ||
|
||
} |
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,8 @@ | ||
package org.example; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
@Profile("test") | ||
@Configuration | ||
class SecurityConfig {} |