gpt4 book ai didi

java - JUnit测试Spring示例中的一些疑问

转载 作者:行者123 更新时间:2023-12-01 12:12:13 25 4
gpt4 key购买 nike

我正在学习 Spring Core 认证,对于 Spring 中的 Junit 使用有以下疑问。

我有一个示例,为我提供以下 RewardNetworkTests

package rewards;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;

import common.money.MonetaryAmount;

/**
* A system test that verifies the components of the RewardNetwork application
* work together to reward for dining successfully. Uses Spring to bootstrap the
* application for use in a test environment.
*/

public class RewardNetworkTests {

/**
* The object being tested.
*/
private RewardNetwork rewardNetwork;

/**
* Need this to enable clean shutdown at the end of the application
*/
private AbstractApplicationContext context;

@Before
public void setUp() {
// Create the test configuration for the application from one file
context = new AnnotationConfigApplicationContext(
TestInfrastructureConfig.class);
// Get the bean to use to invoke the application
rewardNetwork = context.getBean(RewardNetwork.class);
}

@After
public void tearDown() throws Exception {
// simulate the Spring bean destruction lifecycle:
if (context != null)
context.close();
}

@Test
public void testRewardForDining() {
// create a new dining of 100.00 charged to credit card
// '1234123412341234' by merchant '123457890' as test input
Dining dining = Dining.createDining("100.00", "1234123412341234",
"1234567890");

// call the 'rewardNetwork' to test its rewardAccountFor(Dining) method
RewardConfirmation confirmation = rewardNetwork
.rewardAccountFor(dining);

// assert the expected reward confirmation results
assertNotNull(confirmation);
assertNotNull(confirmation.getConfirmationNumber());

// assert an account contribution was made
AccountContribution contribution = confirmation
.getAccountContribution();
assertNotNull(contribution);

// the contribution account number should be '123456789'
assertEquals("123456789", contribution.getAccountNumber());

// the total contribution amount should be 8.00 (8% of 100.00)
assertEquals(MonetaryAmount.valueOf("8.00"), contribution.getAmount());

// the total contribution amount should have been split into 2
// distributions
assertEquals(2, contribution.getDistributions().size());

// each distribution should be 4.00 (as both have a 50% allocation)
assertEquals(MonetaryAmount.valueOf("4.00"), contribution
.getDistribution("Annabelle").getAmount());
assertEquals(MonetaryAmount.valueOf("4.00"), contribution
.getDistribution("Corgan").getAmount());
}
}

因此这个测试类包含 setUp() 方法,该方法从 TestInfrastructConfig.class 配置类开始创建上下文:

@Before
public void setUp() {
// Create the test configuration for the application from one file
context = new AnnotationConfigApplicationContext(
TestInfrastructureConfig.class);
// Get the bean to use to invoke the application
rewardNetwork = context.getBean(RewardNetwork.class);
}

这是TestInfrastructConfig.class配置类的内容:

礼包奖励;

导入org.springframework.context.annotation.Configuration;导入org.springframework.context.annotation.Import;

导入config.RewardsConfig;

@Configuration
@Import({
TestInfrastructureDevConfig.class,
TestInfrastructureProductionConfig.class,
RewardsConfig.class })
public class TestInfrastructureConfig {

public LoggingBeanPostProcessor loggingBean(){
return new LoggingBeanPostProcessor();
}
}

所以在教程中它对我说,如果我删除 setUp() 方法,并且在执行此操作后我尝试执行我的测试,我将获得一个红色条因为rewardNetwork字段为空

好吧,我认为这可能取决于以下事实:删除 setUp() 方法时,我没有从 TestInfrastructConfig.class 配置类中获取上下文,该类在导入 RewardsConfig.class 配置类,其中使用以下内容声明组件扫描:

@ComponentScan("rewards")

因此,应用程序不能使用在 rewards 包的子包中声明的 RewardNetworkImpl 类(实现 RewardNetwork 接口(interface)),并且它注释为@Service:

@Service("rewardNetwork")
public class RewardNetworkImpl implements RewardNetwork {
...................................
...................................
...................................
}

好吧,我认为这很清楚(或者我错过了什么?)

我的疑问是。回到之前的测试方法:

@Test
public void testRewardForDining() {
// create a new dining of 100.00 charged to credit card
// '1234123412341234' by merchant '123457890' as test input
Dining dining = Dining.createDining("100.00", "1234123412341234",
"1234567890");

// call the 'rewardNetwork' to test its rewardAccountFor(Dining) method
RewardConfirmation confirmation = rewardNetwork
.rewardAccountFor(dining);

...................................................
...................................................
...................................................
}

正如所见,在未建立的 rewardNetwork 对象上调用 rewardAccountFor() 方法时会引发异常,但为什么使用调试器时, createDining( ) 方法在 Dinning dinning 对象上调用?

为什么这个对象实例化没有问题?

这是Dinning类代码:

public class Dining {

private MonetaryAmount amount;

private String creditCardNumber;

private String merchantNumber;

private SimpleDate date;

/**
* Creates a new dining, reflecting an amount that was charged to a card by a merchant on the date specified.
* @param amount the total amount of the dining bill
* @param creditCardNumber the number of the credit card used to pay for the dining bill
* @param merchantNumber the merchant number of the restaurant where the dining occurred
* @param date the date of the dining event
*/
public Dining(MonetaryAmount amount, String creditCardNumber, String merchantNumber, SimpleDate date) {
this.amount = amount;
this.creditCardNumber = creditCardNumber;
this.merchantNumber = merchantNumber;
this.date = date;
}

/**
* Creates a new dining, reflecting an amount that was charged to a credit card by a merchant on today's date. A
* convenient static factory method.
* @param amount the total amount of the dining bill as a string
* @param creditCardNumber the number of the credit card used to pay for the dining bill
* @param merchantNumber the merchant number of the restaurant where the dining occurred
* @return the dining event
*/
public static Dining createDining(String amount, String creditCardNumber, String merchantNumber) {
return new Dining(MonetaryAmount.valueOf(amount), creditCardNumber, merchantNumber, SimpleDate.today());
}

/**
* Creates a new dining, reflecting an amount that was charged to a credit card by a merchant on the date specified.
* A convenient static factory method.
* @param amount the total amount of the dining bill as a string
* @param creditCardNumber the number of the credit card used to pay for the dining bill
* @param merchantNumber the merchant number of the restaurant where the dining occurred
* @param month the month of the dining event
* @param day the day of the dining event
* @param year the year of the dining event
* @return the dining event
*/
public static Dining createDining(String amount, String creditCardNumber, String merchantNumber, int month,
int day, int year) {
return new Dining(MonetaryAmount.valueOf(amount), creditCardNumber, merchantNumber, new SimpleDate(month, day,
year));
}

/**
* Returns the amount of this dining--the total amount of the bill that was charged to the credit card.
*/
public MonetaryAmount getAmount() {
return amount;
}

/**
* Returns the number of the credit card used to pay for this dining. For this dining to be eligible for reward,
* this credit card number should be associated with a valid account in the reward network.
*/
public String getCreditCardNumber() {
return creditCardNumber;
}

/**
* Returns the merchant number of the restaurant where this dining occurred. For this dining to be eligible for
* reward, this merchant number should be associated with a valid restaurant in the reward network.
*/
public String getMerchantNumber() {
return merchantNumber;
}

/**
* Returns the date this dining occurred on.
*/
public SimpleDate getDate() {
return date;
}

public boolean equals(Object o) {
if (!(o instanceof Dining)) {
return false;
}
Dining other = (Dining) o;
// value objects are equal if their attributes are equal
return amount.equals(other.amount) && creditCardNumber.equals(other.creditCardNumber)
&& merchantNumber.equals(other.merchantNumber) && date.equals(other.date);
}

public int hashCode() {
return amount.hashCode() + creditCardNumber.hashCode() + merchantNumber.hashCode() + date.hashCode();
}

public String toString() {
return "Dining of " + amount + " charged to '" + creditCardNumber + "' by '" + merchantNumber + "' on " + date;
}
}

Tnx

最佳答案

Dining 似乎是一个类,而 createDining 似乎是一个 static 方法。无需创建 Dining 对象即可调用此类方法。

这相当于做

System.currentTimeMillis();

Spring 不参与静态方法调用。

关于java - JUnit测试Spring示例中的一些疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27205678/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com