BAEL-2095 CrudRepository save() method (#5223)
* BAEL-2095 CrudRepository save() method * BAEL-2095 Enhanced the MerchandiseEntity with additional attributes; added unit-tests; removed spring boot runner * BAEL-2095 Test renamed to end with IntegrationTest to make it past Travis's rules
This commit is contained in:
parent
aef7b7f9aa
commit
86d60237f4
@ -0,0 +1,7 @@
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.domain.MerchandiseEntity;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface InventoryRepository extends CrudRepository<MerchandiseEntity, Long> {
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.baeldung.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
@Entity
|
||||
public class MerchandiseEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private BigDecimal price;
|
||||
|
||||
private String brand;
|
||||
|
||||
public MerchandiseEntity() {
|
||||
}
|
||||
|
||||
public MerchandiseEntity(String title, BigDecimal price) {
|
||||
this.title = title;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MerchandiseEntity{" +
|
||||
"id=" + id +
|
||||
", title='" + title + '\'' +
|
||||
", price=" + price +
|
||||
", brand='" + brand + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.domain.MerchandiseEntity;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest(excludeAutoConfiguration = {PersistenceConfiguration.class})
|
||||
public class InventoryRepositoryIntegrationTest {
|
||||
|
||||
private static final String ORIGINAL_TITLE = "Pair of Pants";
|
||||
private static final String UPDATED_TITLE = "Branded Luxury Pants";
|
||||
private static final String UPDATED_BRAND = "Armani";
|
||||
private static final String ORIGINAL_SHORTS_TITLE = "Pair of Shorts";
|
||||
|
||||
@Autowired
|
||||
private InventoryRepository repository;
|
||||
|
||||
@Test
|
||||
public void shouldCreateNewEntryInDB() {
|
||||
MerchandiseEntity pants = new MerchandiseEntity(ORIGINAL_TITLE, BigDecimal.ONE);
|
||||
pants = repository.save(pants);
|
||||
|
||||
MerchandiseEntity shorts = new MerchandiseEntity(ORIGINAL_SHORTS_TITLE, new BigDecimal(3));
|
||||
shorts = repository.save(shorts);
|
||||
|
||||
assertNotNull(pants.getId());
|
||||
assertNotNull(shorts.getId());
|
||||
assertNotEquals(pants.getId(), shorts.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldUpdateExistingEntryInDB() {
|
||||
MerchandiseEntity pants = new MerchandiseEntity(ORIGINAL_TITLE, BigDecimal.ONE);
|
||||
pants = repository.save(pants);
|
||||
|
||||
Long originalId = pants.getId();
|
||||
|
||||
pants.setTitle(UPDATED_TITLE);
|
||||
pants.setPrice(BigDecimal.TEN);
|
||||
pants.setBrand(UPDATED_BRAND);
|
||||
|
||||
MerchandiseEntity result = repository.save(pants);
|
||||
|
||||
assertEquals(originalId, result.getId());
|
||||
assertEquals(UPDATED_TITLE, result.getTitle());
|
||||
assertEquals(BigDecimal.TEN, result.getPrice());
|
||||
assertEquals(UPDATED_BRAND, result.getBrand());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user