这是使用JUnit的服务方法的真实测试代码吗?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了这是使用JUnit的服务方法的真实测试代码吗?相关的知识,希望对你有一定的参考价值。

我有方法,可以增加帐户资金。我是测试的新手,以前从未测试过。

public Optional<Account> addMoneyToBalance(Long id, Float money) {
        Optional<Account> accountFromDb = accountRepository.findById(id);
        float result;
        if (accountFromDb.isPresent()) {
            Account account = accountFromDb.get();
            result = account.getTotalBalance();
            result += money;
            account.setTotalBalance(result);
            accountRepository.save(account);
        }else throw new AccountNotFoundException("Account not found");

        return accountFromDb;
    }

如果我保存accountRepository.save(account),它将更改database(accountFromDB)中的数据吗?

这是真的测试吗? :

@RunWith(SpringRunner.class)
@SpringBootTest
public class FrugalBudgetingApplicationTests {

    @Autowired
    private AccountService accountService;
    @Autowired
    private AccountRepository accountRepository;


    @Test
    public void contextLoads() {
        List<Bank> banks = new ArrayList<>();
        banks.add(new Bank("OTP", 1239.2f));
        banks.add(new Bank("UKRsib", 549.2f));
        Account account = new Account(1912.2f, 200.45f, banks);
        accountRepository.save(account);
        Optional<Account> account1 = accountService.addMoneyToBalance(1L, 92f);
        Assert.assertNull(account1);
    }

}

测试结果:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.korzhov.frugalbudgeting.entities.Account.banks, could not initialize proxy - no Session
    at java.base/java.lang.String.valueOf(String.java:2951)
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:168)
    at com.korzhov.frugalbudgeting.entities.Account.toString(Account.java:15)
    at java.base/java.util.Formatter$FormatSpecifier.printString(Formatter.java:3031)
    at java.base/java.util.Formatter$FormatSpecifier.print(Formatter.java:2908)
    at java.base/java.util.Formatter.format(Formatter.java:2673)
    at java.base/java.util.Formatter.format(Formatter.java:2609)
    at java.base/java.lang.String.format(String.java:2897)
    at java.base/java.util.Optional.toString(Optional.java:466)
    at java.base/java.lang.String.valueOf(String.java:2951)
    at java.base/java.lang.StringBuilder.append(StringBuilder.java:168)
    at org.junit.Assert.failNotNull(Assert.java:755)
    at org.junit.Assert.assertNull(Assert.java:737)
    at org.junit.Assert.assertNull(Assert.java:747)
    at com.korzhov.frugalbudgeting.FrugalBudgetingApplicationTests.contextLoads(FrugalBudgetingApplicationTests.java:36)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)

老实说,我什至不知道,我必须用这种方法测试什么。因此,我尝试从Db测试NullPointer帐户。

答案

根据提供的代码片段判断,当前正在执行的是集成测试,该测试连接到真实数据库(看不到pom / gradle.build文件,我无法确定是哪一个,但是我假设是H2。

如果我保存accountRepository.save(account),它将更改数据库(accountFromDB)中的数据吗?

以上是关于这是使用JUnit的服务方法的真实测试代码吗?的主要内容,如果未能解决你的问题,请参考以下文章

当@before节中的代码失败时Junit将执行@Test案例吗?

JUnit 是编写性能测试的正确工具吗? [关闭]

Junit 测试断言错误预期为 3,但附带 0

使用 DAO 和 Web 服务的数据库插入方法的 Junit 测试用例

如何使用Scanner和while循环[复制]对void方法进行JUnit测试

Spring Boot OncePerRequestFilter shouldNotFilter Junit