Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何编写期望在java中发出错误的测试_Java_Kotlin_Testing - Fatal编程技术网

如何编写期望在java中发出错误的测试

如何编写期望在java中发出错误的测试,java,kotlin,testing,Java,Kotlin,Testing,我正在测试此功能: fun findByUsername(username: String): Account { return accountRepo.findByUsername(username).orElseThrow { UsernameNotFoundException("Username was not found") } } 这是我的测验 @Test fun checkFindByUsername() {

我正在测试此功能:

fun findByUsername(username: String): Account {
        return accountRepo.findByUsername(username).orElseThrow {
            UsernameNotFoundException("Username was not found")
        }
    }
这是我的测验

@Test
    fun checkFindByUsername() {
        val userRegistrationForm = UserRegistrationForm("testUser3", "123", "eee", false)
        val user = accountService.createAccount(userRegistrationForm)

        assertEquals(accountRepo.findByUsername("").orElseThrow {
            UsernameNotFoundException("Username was not found")
        }, "Username was not found")
    }
如何使考试通过? 您需要使用任何特定的断言吗?
抱歉,请理解这个愚蠢的问题

假设您使用的是JUnit(5),那么您可以使用断言.assertThrows

Assertions.assertThrows(UsernameNotFoundException.class, () -> 
    accountRepo.findByUsername("")
);

假设您使用的是JUnit(5),那么就可以使用
断言.assertThrows

Assertions.assertThrows(UsernameNotFoundException.class, () -> 
    accountRepo.findByUsername("")
);

使用
@Test
注释的
预期参数:

@Test(expected = UsernameNotFoundException.class)

使用
@Test
注释的
预期参数:

@Test(expected = UsernameNotFoundException.class)

如果JUnit 5可用,我建议在此基础上使用
assertThrows
,因为它指定了预期和可接受异常的确切位置。如果JUnit 5可用,我建议在此基础上使用
assertThrows
,因为它指定了预期和可接受异常的确切位置。当我ise assertThrows时,出现错误:org.opentest4j.AssertionFailedError:expected org.springframework.security.core.userdetails.UsernameNotFoundException要引发,但没有引发任何异常。当我ise assertThrows时,appearts错误:org.opentest4j.AssertionFailedError:预期将引发org.springframework.security.core.userdetails.UsernameNotFoundException,但未引发任何内容。当我ise assertThrows时,appearts错误:org.opentest4j.AssertionFailedError:预期将引发org.springframework.security.core.userdetails.UsernameNotFoundException,但是没有引发任何异常。然后,似乎没有引发异常…当我使用assertThrows时,出现错误:org.opentest4j.AssertionFailedError:Expected org.springframework.security.core.userdetails.UsernameNotFoundException将引发异常,但没有引发异常。然后,似乎没有引发异常。。。