当前位置: 首页 > 知识库问答 >
问题:

嘿,我有一个关于java中断言和嘲笑的问题

白迪
2023-03-14
import com.example.currence_exchange.Interfaces.Currency_Interface;
import com.example.currence_exchange.Interfaces.OldRates_Interface;
import com.example.currence_exchange.Interfaces.Rates_Interface;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class CurrencyExchange_LogicTest {

    @Mock
    Currency_Interface currency_interface;
    @Mock
    Rates_Interface rates_interface;
    @Mock
    OldRates_Interface Oldrates_interface;
    @InjectMocks
    CurrencyExchange_Logic exchange_logic;


    @Test
    void currencyJsonTest() {
        Assertions.assertNotNull(currency_interface);
        Assertions.assertNotNull(rates_interface);
        Assertions.assertNotNull(Oldrates_interface);
        Assertions.assertNotNull(exchange_logic);

    }
}
    <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.21.0</version>
            <scope>test</scope>
        </dependency>

为什么我会得到这个错误?为什么尽管导入了junit断言,我还是不能编写assertNotnull();而是必须编写assertNotnull()?为什么我不能使用mockito中的when().then()(message is不能解析'currencyexchange_logictest'中的方法'when')。是不是因为依赖关系,它们出了什么问题?

共有1个答案

袁子瑜
2023-03-14

NoSuchMethod错误通常表示您的依赖项中的jar版本不匹配。您需要确保Maven依赖项使用的是受支持版本的组合,而不是随机组合。

以及为什么尽管导入了junit断言,我还是不能编写assertNotNull();但是必须编写assertions.assertNotNull()?

您需要使用如下所示的静态导入:

import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;
 类似资料:
  • 为什么最后输出的是“WuffRingding”而不是“RingdingRingding”? 为什么最终输出是“颤动”而不是“振铃”?为什么最终输出是“颤动”而不是“振铃”?

  • 我试图在中模拟axios和。到目前为止,这是我的代码: 据我所知,这和文件里的一模一样 确切的例子: 并且我得到了 我错过了什么?

  • 以下是日志供参考: IllegalStateException:无法转换名为com.xyz.TestLoad的类。原因:java.io.ioException:无效常量类型:在org.powermock.core.classloader.mockClassLoader.loadModifiedClass(MockClassLoader.java:180)在org.powermock.core.cl

  • 我也是Mockito和PowerMockito的新手。我发现我无法使用纯Mockito测试静态方法,因此我需要使用PowerMockito(对吗?)。 我有一个非常简单的类,名为Validate,使用这个非常简单的方法 因此,我需要验证: 1) 当我在null message参数上调用该静态方法时,将调用IllegalArgumentException。2)当我在null object参数上调用该

  • 由于各种原因,我的单元测试环境无法访问启动ignite所需的环境。我不需要ignite来启动测试,我希望代码忽略对ignite的调用。开始()。我该怎么做? 我尝试过模拟点火,但当我尝试模拟start()方法时,它会抛出一个错误。 这会导致以下错误: 组织。莫基托。例外情况。误用。MissingMethodInvocationException:when()需要的参数必须是“模拟的方法调用”。例如

  • 我在pset3(cs50)中做tideman问题,但是我不能区分我的程序和函数look_pairs的正确程序之间的区别。 这是我的程序,我在正确的程序之间做了一点区别。我没有判断makeCycle(winner,I)的返回,而是返回这个函数,你可以在我的代码中看到区别。 这是正确的版本,但我认为我的程序和正确的版本是一样的,我不知道为什么我的程序会造成这个错误。