Skip to content

Instantly share code, notes, and snippets.

View kzdelarec's full-sized avatar

Kristijan Zdelarec kzdelarec

  • Zagreb, Croatia
View GitHub Profile
//Customer POJO class to represent entity Customer
class Customer(val name: String, val email: String, val company: String)
public class Customer{
String name;
String email;
String company;
//constructor to initialise fields
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<h1>{{title}}</h1>
</body>
</html>
@WebMvcTest
class HttpControllersTests(@Autowired val mockMvc: MockMvc) {
@MockkBean
private lateinit var articleRepository: ArticleRepository
@Test
fun `List articles`() {
val user = User("testUser", "John", "Doe")
val spring5Article = Article("Spring Framework 5.0 goes GA", "Dear Spring community ...", "Lorem ipsum", user)
@RestController
@RequestMapping("/api/article")
class ArticleController(private val repository: ArticleRepository) {
@GetMapping("/")
fun findAll() = repository.findAllByOrderByAddedAtDesc()
@GetMapping("/{id}")
fun findOne(@PathVariable id: UUID) =
repository.findBySlug(slug) ?: ResponseStatusException(HttpStatus.NOT_FOUND, "This article does not exist")
package com.example.blog
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.ui.set
import org.springframework.web.bind.annotation.GetMapping
@Controller
class HtmlController {
//Null reference
var a: String = "abc"
a = null //compilation error
//output: Null can not be a value of a non-null type String
//Nullable operator ?
var a: String? = "abc"
a = null //ok
print(a)
class Customer(val name: String, val email: String, val company: String)