Junit 5 : AssertionFailedError: expected: <true> but was: <false>
Junit 5 : AssertionFailedError: expected: <true> but was: <false>
我正在尝试为服务中的以下功能编写单元测试用例 class
@Service
public class currencyHandler {
public boolean isNoDecimal(String currency) {
return "JPY".equalsIgnoreCase(currency) || "KRW".equalsIgnoreCase(currency);
}
}
单位案例如下:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.svc.it.handlers;
@ExtendWith(SpringExtension.class)
public class CurrencyTests {
@Mock
CurrencyHandler currencyHandlerTest;
@Test
public void isNoCurrency() throws Exception {
assertTrue(currencyHandlerTest.isNoCurrency("ABC"));
assertTrue(currencyHandlerTest.isNoCurrency("XYZ"));
assertFalse(currencyHandlerTest.isNoCurrency("BOGUS"));
assertFalse(currencyHandlerTest.isNoCurrency(""));
assertFalse(currencyHandlerTest.isNoCurrency(null));
}
}
下面是我运行上面的单元测试用例时的错误:
org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:40)
at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:35)
at org.junit.jupiter.api.Assertions.assertTrue(Assertions.java:162)
我想我对@Mock 对象做错了什么。
感谢你的帮助。提前致谢!
要为 CurrencyHandler
class 编写单元测试,您必须针对 class 的一个实例进行测试。您不得针对 class.
的 mock 实例进行测试
下面展示了如何编写这样的单元测试。
package example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class CurrencyHandlerTests {
private CurrencyHandler currencyHandler = new CurrencyHandler();
@Test
void isNoDecimal() {
assertTrue(currencyHandler.isNoDecimal("JPY"));
assertTrue(currencyHandler.isNoDecimal("KRW"));
assertFalse(currencyHandler.isNoDecimal("BOGUS"));
assertFalse(currencyHandler.isNoDecimal(""));
assertFalse(currencyHandler.isNoDecimal(null));
}
}
为了更好地理解这里的概念,我建议您阅读一些关于单元测试、模拟和 SUT(被测对象)的文章或书籍。
另外,如果你写的是像上面那样的纯单元测试,你不需要注册SpringExtension
,因为纯单元测试不需要与[=27中的组件进行交互=] ApplicationContext
.
我正在尝试为服务中的以下功能编写单元测试用例 class
@Service
public class currencyHandler {
public boolean isNoDecimal(String currency) {
return "JPY".equalsIgnoreCase(currency) || "KRW".equalsIgnoreCase(currency);
}
}
单位案例如下:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.svc.it.handlers;
@ExtendWith(SpringExtension.class)
public class CurrencyTests {
@Mock
CurrencyHandler currencyHandlerTest;
@Test
public void isNoCurrency() throws Exception {
assertTrue(currencyHandlerTest.isNoCurrency("ABC"));
assertTrue(currencyHandlerTest.isNoCurrency("XYZ"));
assertFalse(currencyHandlerTest.isNoCurrency("BOGUS"));
assertFalse(currencyHandlerTest.isNoCurrency(""));
assertFalse(currencyHandlerTest.isNoCurrency(null));
}
}
下面是我运行上面的单元测试用例时的错误:
org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
at org.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:55)
at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:40)
at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:35)
at org.junit.jupiter.api.Assertions.assertTrue(Assertions.java:162)
我想我对@Mock 对象做错了什么。 感谢你的帮助。提前致谢!
要为 CurrencyHandler
class 编写单元测试,您必须针对 class 的一个实例进行测试。您不得针对 class.
下面展示了如何编写这样的单元测试。
package example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class CurrencyHandlerTests {
private CurrencyHandler currencyHandler = new CurrencyHandler();
@Test
void isNoDecimal() {
assertTrue(currencyHandler.isNoDecimal("JPY"));
assertTrue(currencyHandler.isNoDecimal("KRW"));
assertFalse(currencyHandler.isNoDecimal("BOGUS"));
assertFalse(currencyHandler.isNoDecimal(""));
assertFalse(currencyHandler.isNoDecimal(null));
}
}
为了更好地理解这里的概念,我建议您阅读一些关于单元测试、模拟和 SUT(被测对象)的文章或书籍。
另外,如果你写的是像上面那样的纯单元测试,你不需要注册SpringExtension
,因为纯单元测试不需要与[=27中的组件进行交互=] ApplicationContext
.