【发布时间】:2016-09-11 11:36:03
【问题描述】:
我对 Spring Boot 和测试有点陌生。我想测试这个@Service 类:
@Service
public class TransactionService {
private MessagingService messagingService;
private CustomerRepository customerRepository;
private static final Logger log = LoggerFactory.getLogger(TransactionService.class);
@Autowired
public TransactionService(MessagingService messagingService, CustomerRepository customerRepository){
Assert.notNull(messagingService, "MessagingService cannot be null");
this.messagingService = messagingService;
Assert.notNull(customerRepository, "CustomerRepository cannot be null");
this.customerRepository = customerRepository;
}
public void makeTransactionFromSenderToReceiver(Customer sender, Customer receiver, int amount) {
if (sender.getBalance() >= amount) {
sender.setBalance(sender.getBalance() - amount);
receiver.setBalance(receiver.getBalance() + amount);
customerRepository.save(sender);
customerRepository.save(receiver);
}
else {
throw new CustomerBalanceExceededException();
}
}
}
在我的武器库中,我有 spring-boot-starter-test maven 依赖项进行测试。我设法像这样测试了我的 Customer 类:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class CustomerTests
{
@Autowired
private CustomerRepository repository;
@Test
public void testCreate()
{
final Customer customer = new Customer();
customer.setName("CoolGuy");
Assert.notNull(repository.save(customer));
}
}
我目前不知道如何测试上面定义的@Service 类的方法的功能。任何提示都非常感谢。
编辑:我试过写一个测试,但我认为这不是测试这个逻辑的正确方法。任何帮助将不胜感激:
TransactionServiceTests.class
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class TransactionServiceTests {
@Autowired
private CustomerRepository repository;
@Autowired
private TransactionService transactionService;
@Test
public void testTransactionBetweenCustomersAndBalanceOfReceiver() {
int AMOUNT = 50;
Customer customerOksana = repository.findByName("Oksana");
Customer customerDeniss = repository.findByName("Deniss");
transactionService.makeTransactionFromSenderToReceiver(customerDeniss, customerOksana, AMOUNT);
assertThat(customerOksana.getBalance()).isEqualTo(customerOksana.getBalance());
}
@Test
public void testTransactionBetweenCustomersAndBalanceOfSender() {
int AMOUNT = 40;
Customer customerOksana = repository.findByName("Oksana");
Customer customerDeniss = repository.findByName("Deniss");
transactionService.makeTransactionFromSenderToReceiver(customerDeniss, customerOksana, AMOUNT);
assertThat(customerDeniss.getBalance()).isEqualTo(customerDeniss.getBalance());
}
}
【问题讨论】:
-
您是要进行单元测试、集成还是两者兼而有之?
-
两项测试都需要。
-
这不能回答您的问题,但您的 TransactionService 中的 Assert 类型似乎不正确。我认为您正在寻找
assertdocs.oracle.com/javase/7/docs/technotes/guides/language/… 而不是 JUnitAssert类 -
docs.spring.io/spring/docs/current/javadoc-api/org/…我认为在这种情况下是正确的。
-
我要更改的另一件事是将
private static final Logger log = LoggerFactory.getLogger(Application.class);更改为private static final Logger log = LoggerFactory.getLogger(TransactionService.class);一般做法是将调用类用作记录器,以便您可以更好地理解和过滤来自代码特定部分的日志记录。您可以为记录器使用不同的类,但我认为这不是一般做法。
标签: java testing spring-boot