Skip to content

Instantly share code, notes, and snippets.

View soudmaijer's full-sized avatar

Stephan Oudmaijer soudmaijer

View GitHub Profile
public void doStuffWithOrder(Long id) {
Optional<Order> orderById = orderRepository.findOrderById(id);
orderById.ifPresent( order -> {
// do stuff with order
});
}
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.RowMapper
import org.springframework.stereotype.Repository
@Repository
class OrderRepository(private val jdbcTemplate: JdbcTemplate) {
private val rowMapper: RowMapper<Order> = RowMapper { rs, i -> Order(rs.getLong("id")) }
fun findOrderById(id: Long): Order? {
val result = jdbcTemplate.query<Order>("select * from orders where id=:id", rowMapper, id)
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.RowMapper
import org.springframework.stereotype.Repository
import java.util.*
@Repository
class OrderRepository(private val jdbcTemplate: JdbcTemplate) {
private val rowMapper: RowMapper<Order> = RowMapper { rs, i -> Order(rs.getLong("id")) }
fun findOrderById(id: Long): Optional<Order> {
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public class OrderRepository {
private static final RowMapper<Order> rowMapper = (rs, i) -> new Order(rs.getLong("id"));
@RunWith(JMockit::class)
class AddressBookTestKt {
@Injectable
private lateinit var addressBookRepository: AddressBookRepository
@Tested
private lateinit var addressBook: AddressBook
@Test
fun retrieveAddressByIdTest() {
inline fun expectations(crossinline block: KExpectations.() -> Unit) {
object : KExpectations() {
init {
block()
}
}
}
inline fun verifications(crossinline block: KVerifications.() -> Unit) {
object : KVerifications() {
expectations {
// Instruct mocks
}
object : Expectations() {
init {
// Instruct mocks
}
}
class AddressBook {
private AddressBookRepository repository;
public AddressBook(AddressBookRepository repository) {
this.repository = repository;
}
public Address getAddressById(int i) {
return repository.findById(i);
}
@RunWith(JMockit::class)
class AddressBookTest {
@Injectable
private lateinit var addressBookRepository: AddressBookRepository
@Tested
private lateinit var addressBook: AddressBook
@Test
fun retrieveAddressByIdTest() {