Skip to content

Commit

Permalink
Converting tests to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasia committed May 13, 2021
1 parent e134892 commit ed8a9d6
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 135 deletions.

This file was deleted.

64 changes: 64 additions & 0 deletions src/test/java/com/lucasia/ginquiry/controller/GinControllerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.lucasia.ginquiry.controller;

import com.lucasia.ginquiry.dao.BoozeRepository
import com.lucasia.ginquiry.dao.BrandRepository
import com.lucasia.ginquiry.domain.Booze
import com.lucasia.ginquiry.domain.Brand
import com.lucasia.ginquiry.domain.DomainFactory
import com.lucasia.ginquiry.service.BoozeService
import lombok.extern.log4j.Log4j2
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mockito
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.security.test.context.support.WithMockUser
import org.springframework.web.util.NestedServletException
import java.util.*


@WebMvcTest(GinCrudController::class) // run without the server
@Log4j2
class GinControllerTest() : AbstractCrudControllerTest<Booze>(DomainFactory.BoozeDomainFactory(Brand(UUID.randomUUID().toString())), GinCrudController.GIN_PATH) {

@MockBean
private val brandRepository : BrandRepository? = null

@MockBean
private val boozeRepository : BoozeRepository? = null

@MockBean
private val boozeService : BoozeService? = null

@BeforeEach
fun setUp() {
Assertions.assertNotNull(boozeService);
}


@Test
@WithMockUser(GUEST_USER)
override fun testNewSucceeds() {
val brand = Brand(UUID.randomUUID().toString())

Mockito.`when`(brandRepository?.save(brand)).thenReturn(brand)

testAddNewSucceeds(Booze(brand, UUID.randomUUID().toString(), UUID.randomUUID().toString()));
}

@Test
@WithMockUser(GUEST_USER)
fun testNewBoozeWithMissingBrandFails() {
Mockito.`when`(brandRepository?.save(null)).thenThrow(NullPointerException());

// TODO: change to NPE or a named Exception
val ex: Exception = Assertions.assertThrows(NestedServletException::class.java) { saveEntity(Booze()) }

Assertions.assertTrue(ex.message!!.contains("brand is marked non-null but is null"))
}

override fun getRepository(): BoozeRepository? {
return boozeRepository;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.lucasia.ginquiry.controller;

import org.hamcrest.Matchers
import org.hamcrest.text.IsEmptyString
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.security.test.context.support.WithMockUser
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.ResultActions
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import org.springframework.test.web.servlet.result.MockMvcResultHandlers.print
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
import java.util.*


@WebMvcTest(UserController::class) // run without the server
class UserControllerTest {

@Autowired
private val mockMvc: MockMvc? = null

private val principalPath: String = UserController.PRINCIPAL_PATH

@Test
fun testPrincipalSuccessWhenNotLoggedIn() {
val resultActions: ResultActions = mockMvc!!.perform(get(principalPath))

resultActions.andDo(
print())
.andExpect(status().isOk)
.andExpect(content().string(IsEmptyString.emptyString()))
}

@Test
@WithMockUser("guest")
fun testPrincipalSuccessAndUsernameAvailableWhenLoggedIn() {
val resultActions: ResultActions = mockMvc!!.perform(get(principalPath))

resultActions.andDo(
print())
.andExpect(status().isOk)
.andExpect(content().string(Matchers.containsString("guest")))
}


@Test
fun testOtherEndpointsReturnUnauthorizedWhenNotLoggedIn() {

val resultActions: ResultActions = mockMvc!!.perform(get("/" + UUID.randomUUID()))

resultActions.andDo(
print())
.andExpect(status().is4xxClientError)
.andExpect(status().isUnauthorized)
.andExpect(content().string(Matchers.containsString("")))
}

}

0 comments on commit ed8a9d6

Please sign in to comment.