Skip to content

Commit

Permalink
base controller testing
Browse files Browse the repository at this point in the history
  • Loading branch information
commel committed Oct 5, 2023
1 parent cdb6e44 commit 4a94fba
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
23 changes: 22 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,20 @@
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- Tests -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>

<!-- Messaging -->
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-spring -->
Expand Down Expand Up @@ -345,6 +353,19 @@
</gitDescribe>
</configuration>
</plugin>
<!-- Workaround for InaccessibleObjectException in Java 21 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.base/java.time=ALL-UNNAMED
--add-opens java.base/java.time.format=ALL-UNNAMED
--add-opens java.base/java.util=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
<!-- Junit 5 Runner -->
<!-- <plugin>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/de/holarse/web/controller/WelcomeControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package de.holarse.web.controller;

import de.holarse.backend.db.ApiUser;
import de.holarse.backend.db.repositories.ApiUserRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import static org.mockito.Mockito.when;
import org.mockito.MockitoAnnotations;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

public class WelcomeControllerTest {

@Mock
ApiUserRepository apiUserRepositoryMock;

WelcomeController controller;

@BeforeEach
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
controller = new WelcomeController();
controller.apiUserRepository = apiUserRepositoryMock;
}

@Test
public void textIndexPage() throws Exception {
when(apiUserRepositoryMock.findByLogin("dummy")).thenReturn(Mockito.mock(ApiUser.class));


MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
mockMvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.view().name("layouts/landing"));
}

}

0 comments on commit 4a94fba

Please sign in to comment.