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

Spring中的单元测试POST API-Boot+kotlin+Junit

壤驷阳冰
2023-03-14

我对春靴和科特林很陌生。我从一个net的基本应用程序开始写单元测试,但我得到了以下错误:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: articleRepository.save(article) must not be null

让我向您展示代码:实体类

@Entity
data class Article (
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Long = 0,

    @get: NotBlank
    val title: String = "",

    @get: NotBlank
    val content: String = ""
)

控制器:

@PostMapping("/articles")
fun createNewArticle(@Valid @RequestBody article: Article) : Article {
    return articleRepository.save(article)
}
@Repository
interface ArticleRepository : JpaRepository<Article, Long>
RunWith(SpringRunner::class)
@SpringBootTest
class KotlinDemoApplicationTests {

lateinit var mvc: MockMvc

@InjectMocks
lateinit var controller: ArticleController

@Mock
lateinit var respository: ArticleRepository

@Before
fun setup() {
    MockitoAnnotations.initMocks(this)
    mvc = MockMvcBuilders.standaloneSetup(controller).setMessageConverters(MappingJackson2HttpMessageConverter()).build()
}

@Test
fun createBlog() {
    var article = Article(1, "Test", "Test Content")
    var jsonData = jacksonObjectMapper().writeValueAsString(article)
    mvc.perform(MockMvcRequestBuilders.post("/api/articles/").contentType(MediaType.APPLICATION_JSON).content(jsonData))
            .andExpect(MockMvcResultMatchers.status().isOk)
            .andDo(MockMvcResultHandlers.print())
            .andReturn()
}
}

共有1个答案

夏和雅
2023-03-14

问题是您的articlerepository模拟。

虽然正确地将它注入到控制器中,但您并没有指定对save的调用应该返回什么。因此,它返回null,这在Kotin中是不允许的,因为您将其指定为非可选的。

通过添加,允许控制器的createnewarticle返回null,这将其签名更改为

fun createNewArticle(@Valid @RequestBody article: Article) : Article? {...}
@Before
fun setup() {
    MockitoAnnotations.initMocks(this)
    ...
    `when`(respository.save(any())
        .thenReturn(Article()) // creates a new article
}
 类似资料:
  • 我对SpringBoot非常陌生,并试图学习如何在SpringBoot中进行测试。我读到了@SpringBootTest注释,它有助于集成测试应用程序。我想知道在SpringBoot中应该如何进行单元测试。单元测试是否需要指定@SpringBootTest注释,还是仅用于集成测试?是否有用于单元测试的特定注释? 任何指点都将不胜感激。提前谢谢! 编辑:SpringBootTest注释是否仅用于集成

  • 本文向大家介绍Spring Boot 单元测试JUnit的实践,包括了Spring Boot 单元测试JUnit的实践的使用技巧和注意事项,需要的朋友参考一下 一、介绍 JUnit是一款优秀的开源Java单元测试框架,也是目前使用率最高最流行的测试框架,开发工具Eclipse和IDEA对JUnit都有很好的支持,JUnit主要用于白盒测试和回归测试。 <!--more--> 白盒测试:把测试对象看

  • 本文向大家介绍详解Spring Boot Junit单元测试,包括了详解Spring Boot Junit单元测试的使用技巧和注意事项,需要的朋友参考一下 Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性。 凭本人的感觉和经验来说,在项目中完全按标准都写Junit用例覆盖大部分业务代码的,应该不会超过一半。 刚好前段时间写了一些关于SpringBoo

  • 我有一个用@service注释的类(HttpHandler)。我正在为这个服务类编写单元测试用例。我在测试类中使用@Autowired注释来获取服务的对象。但是,当我运行单元测试时,它返回NullPointerException。有人能帮帮我吗? 下面是服务类的代码:

  • 我如何运行单元测试的Spring引导应用程序,而建设和部署使用命令。 我的期望是在运行应用程序之前执行所有单元测试,但我不想在运行应用程序之前再执行另一个maven命令,如。 我的问题是:我制作了一个简单的spring启动应用程序,在从intellij或命令行运行应用程序时,我可以找到一种运行单元测试的方法。首先,我认为可能我有错误的配置或者错误的测试类名称或者错误的项目结构。所以我从intell

  • 我正在尝试它,在尝试单元测试我的ViewModel时遇到了一个问题。我想要实现的目标:测试我的在我的ViewModel中以正确的顺序接收所有状态值。 我代码如下: ViewModel: 编辑:作为一个临时解决方案,我将它作为一个实时数据进行测试: