Skip to content

Instantly share code, notes, and snippets.

Avatar

Kristijan Zdelarec kzdelarec

  • Zagreb, Croatia
View GitHub Profile
View KotlinHttpTest.kt
@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)
View KotlinRestController.kt
@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")
View blog.html
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<h1>{{title}}</h1>
</body>
</html>
View KotlinKontroller.kt
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 {
View KotlinNPE.kt
//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)
View KotlinPojo.kt
class Customer(val name: String, val email: String, val company: String)
View JavaPojo.java
//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