Skip to content
Snippets Groups Projects
Commit 7e0a1f55 authored by David Hammond's avatar David Hammond
Browse files

update

parent 61a46797
Branches main
Tags Finished
No related merge requests found
No preview for this file type
package com.controllers;
import com.app.CrowdFundingApplication;
import com.app.controllers.ContributorController;
import com.app.models.Contributor;
import com.app.repositories.ContributorRepository;
import com.app.repositories.FundSeekerRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@ContextConfiguration(classes = CrowdFundingApplication.class)
@WebMvcTest(ContributorController.class)
public class ContributorControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private ContributorRepository contributorRepository;
@MockBean
private FundSeekerRepository fundSeekerRepository;
@Test
public void showContributionForm_ShouldReturnContributeView() throws Exception {
mockMvc.perform(get("/contribute/{fundSeekerId}", 1L))
.andExpect(status().isOk())
.andExpect(view().name("contribute"))
.andExpect(model().attributeExists("contributor"))
.andExpect(model().attribute("contributor", org.hamcrest.Matchers.hasProperty("fundSeekerId", org.hamcrest.Matchers.equalTo(1L))));
}
@Test
public void contribute_WithValidAmount_ShouldRedirectToFundSeekers() throws Exception {
Contributor contributor = new Contributor();
contributor.setFundSeekerId(1L);
contributor.setAmount(100.0);
when(contributorRepository.save(any(Contributor.class))).thenReturn(contributor);
mockMvc.perform(post("/contribute")
.param("fundSeekerId", "1")
.flashAttr("contributor", contributor))
.andExpect(status().is3xxRedirection())
.andExpect(redirectedUrl("/fundseekers"));
}
@Test
public void contribute_WithInvalidAmount_ShouldReturnContributeView() throws Exception {
Contributor contributor = new Contributor();
contributor.setFundSeekerId(1L);
contributor.setAmount(-100.0);
mockMvc.perform(post("/contribute")
.param("fundSeekerId", "1")
.flashAttr("contributor", contributor))
.andExpect(status().isOk())
.andExpect(view().name("contribute"))
.andExpect(model().attributeExists("error"));
}
}
package com.controllers;
import com.app.CrowdFundingApplication;
import com.app.controllers.FundSeekerController;
import com.app.models.Contributor;
import com.app.models.FundSeeker;
import com.app.repositories.ContributorRepository;
import com.app.repositories.FundSeekerRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import java.util.Arrays;
import java.util.Collections;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@ContextConfiguration(classes = CrowdFundingApplication.class)
@WebMvcTest(FundSeekerController.class)
public class FundSeekerControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private FundSeekerRepository fundSeekerRepository;
@MockBean
private ContributorRepository contributorRepository;
@Test
public void showRegistrationForm_ShouldReturnRegisterView() throws Exception {
mockMvc.perform(get("/register"))
.andExpect(status().isOk())
.andExpect(view().name("register"))
.andExpect(model().attributeExists("fundSeeker"));
}
@Test
public void registerFundSeeker_WithExistingFriendlyUrl_ShouldReturnRegisterViewWithError() throws Exception {
when(fundSeekerRepository.findByFriendlyUrl(any(String.class))).thenReturn(new FundSeeker());
mockMvc.perform(post("/register")
.param("friendlyUrl", "existing-url"))
.andExpect(status().isOk())
.andExpect(view().name("register"))
.andExpect(model().attributeExists("error"));
}
@Test
public void showFundSeeker_WithExistingUrl_ShouldReturnFundSeekerView() throws Exception {
FundSeeker fundSeeker = new FundSeeker();
fundSeeker.setId(1L);
fundSeeker.setFriendlyUrl("existing-url");
Contributor contributor = new Contributor();
contributor.setAmount(100.0);
contributor.setFundSeekerId(1L);
when(fundSeekerRepository.findByFriendlyUrl("existing-url")).thenReturn(fundSeeker);
when(contributorRepository.findByFundSeekerId(1L)).thenReturn(Collections.singletonList(contributor));
mockMvc.perform(get("/fundseeker/existing-url"))
.andExpect(status().isOk())
.andExpect(view().name("fundseeker"))
.andExpect(model().attributeExists("fundSeeker"))
.andExpect(model().attributeExists("contributors"))
.andExpect(model().attributeExists("totalAmount"));
}
@Test
public void listFundSeekers_ShouldReturnFundSeekersView() throws Exception {
FundSeeker fundSeeker1 = new FundSeeker();
fundSeeker1.setFriendlyUrl("url1");
FundSeeker fundSeeker2 = new FundSeeker();
fundSeeker2.setFriendlyUrl("url2");
when(fundSeekerRepository.findAll()).thenReturn(Arrays.asList(fundSeeker1, fundSeeker2));
mockMvc.perform(get("/fundseekers"))
.andExpect(status().isOk())
.andExpect(view().name("fundseekers"))
.andExpect(model().attributeExists("fundSeekers"));
}
}
package com.repositories;
import com.app.CrowdFundingApplication;
import com.app.models.Contributor;
import com.app.repositories.ContributorRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SpringExtension.class)
@DataJpaTest
@ContextConfiguration(classes = CrowdFundingApplication.class)
public class ContributorRepositoryTest {
@Autowired
private ContributorRepository contributorRepository;
@Test
public void testFindByFundSeekerId() {
// Given
Contributor contributor = new Contributor();
contributor.setAmount(100.0);
contributor.setContributorName("John Doe");
contributor.setContributorEmail("johndoe@example.com");
contributor.setCardNumber(1234567812345678L);
contributor.setExpiryDate("12/25");
contributor.setCvv(123);
contributor.setFundSeekerId(1L);
contributorRepository.save(contributor);
// When
List<Contributor> contributors = contributorRepository.findByFundSeekerId(1L);
// Then
assertThat(contributors).isNotEmpty();
assertThat(contributors).extracting(Contributor::getContributorName).contains("John Doe");
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment