В тестах я могу добавить любой компонент (используя вложенный класс конфигурации stati c). но как я могу добавить репозитории данных Spring? Я хочу использовать свой репозиторий для сохранения своих данных. Вот код для класса репозитория клиента ----
package com.wealthvantage.proposal.data.repositories;
import java.util.List;
import java.util.Optional;
import javax.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.wealthvantage.proposal.data.models.Client;
import com.wealthvantage.proposal.data.models.accountopening.ClientDocument;
import com.wealthvantage.proposal.data.models.accountopening.Custodian;
@Repository
public interface ClientRepository extends JpaRepository<Client, Long> {
Client findByEmail(String email);
@Query("SELECT client FROM Client client LEFT JOIN FETCH client.clientRiskQuestionAnswers "
+ "WHERE client.clientId = :clientId")
Optional<Client> findByIdWithRiskQuestionAnswers(@Param("clientId") Long clientId);
@Query("SELECT client FROM Client client LEFT JOIN FETCH client.clientCustodian custodian "
+ "LEFT JOIN FETCH custodian.custodianDocuments WHERE client.clientId = :clientId")
Optional<Client> findByIdWithClientCustodian(@Param("clientId") Long clientId);
List<Client> findByClientRiskScoreNotNull();
@Transactional
@Modifying
@Query("UPDATE Client client SET client.clientCustodian.custodianId = :custodianId WHERE client.clientId = :clientId")
void setClientCustodianByClientId(@Param("clientId") Long clientId, @Param("custodianId") Long custodianId);
@Query("SELECT document FROM Client client JOIN client.clientDocuments document WHERE client.clientId = :clientId")
List<ClientDocument> findClientDocumentsByClientId(@Param("clientId") Long clientId);
List<Client> findByClientCustodian(Custodian clientCustodian);
}
## Вот код конфигурации предложения ## Я хочу использовать этот класс, чтобы использовать мой репозиторий и сохранить
package com.wealthvantage.selenium.components;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.wealthvantage.proposal.data.models.AssetClass;
import com.wealthvantage.proposal.data.models.Client;
import com.wealthvantage.proposal.data.models.ObjectivesModel;
import com.wealthvantage.proposal.data.models.StrategyModel;
import com.wealthvantage.proposal.data.repositories.ClientRepository;
import com.wealthvantage.proposal.data.repositories.ObjectivesRepository;
import com.wealthvantage.proposal.data.repositories.StrategyRepository;
@Component
public class ProposalSetupConfiguration {
@Autowired
private ClientRepository clientRepository;
@Autowired
private ObjectivesRepository objectivesRepository;
@Autowired
private StrategyRepository strategyRepository;
public void addClient(String email, String name, String country, Double clientRiskScore) {
Client client = new Client();
client.setEmail(email);
client.setClientName(name);
client.setCountry(country);
client.setTaxDomicile(country);
client.setClientRiskScore(clientRiskScore);
clientRepository.save(client);
}
public void addObjective(String objectiveName, String shortDescription, String description) {
ObjectivesModel objective = new ObjectivesModel();
objective.setObjectiveName(objectiveName);
objective.setShortDescription(shortDescription);
objective.setDescription(description);
objectivesRepository.save(objective);
}
public void addStrategy(String objectiveName, String currencyName, String strategyName, Double clientMinimumScore, String assetClassName) {
StrategyModel strategy = new StrategyModel ();
AssetClass assetClass = new AssetClass ();
strategy.getObjective();
strategy.setClientMinimumScore(clientMinimumScore);
strategy.setStrategyName(strategyName);
strategy.setCurrencyName(currencyName);
assetClass.setAssetClassName(assetClassName);
strategyRepository.save(strategy);
}
}
## Вот код для предложения создать класс ## этот класс, который я хочу запустить, используя мой репозиторий и сохранить данные в моей БД.
package com.wealthvantage.selenium.tests;
import static org.junit.Assert.assertTrue;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import com.wealthvantage.proposal.data.repositories.ClientRepository;
import com.wealthvantage.selenium.base.IntegrationTestBase;
import com.wealthvantage.selenium.components.ProposalSetupConfiguration;
import com.wealthvantage.selenium.components.SeleniumTestActions;
@ContextConfiguration(classes = { ProposalSetupConfiguration.class, SeleniumTestActions.class} )
public class ProposalIT extends IntegrationTestBase {
private static String name = "easir";
private static String email = "easir@gmail.com";
private static String country = "Bangladesh";
private static double clientRiskScore = 50;
private static String objectiveName = "Test Growth";
private static String shortDescription = "High Risk, High Return";
private static String description = "Larger portion of investments in equities and in alternative investments - this is where the alpha is generated. A small portion of investments is in fixed income to add some stability to the portfolio.";
private static String currencyName = "USD";
private static String strategyName = "StrategyTest";
private static double clientMinimumScore = 50;
private static String assetClassName = "Liquid";`
@Autowired
ProposalSetupConfiguration config;
@Mock
private ClientRepository clientRepository;
boolean updated = false;
@Before
public void loginAndAccessClients() {
try {
if (!updated) {
config.addClient(email, name, country, clientRiskScore);
config.addObjective(objectiveName, shortDescription, description);
config.addStrategy(objectiveName, currencyName, strategyName, clientMinimumScore, assetClassName);
updated = true;
}
seleniumActions.loginPageAction(driver, baseUrl, username, password);
} catch (Exception e) {
e.printStackTrace();
assertTrue(false);
}
}