我必须编写一些junit测试用例来检查实体。我正在使用postgres作为数据库。
我的实体类
@Entity
@Table(name = "display")
public class Display {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String title;
private String group;
public Display() {
}
public Display(Long id, String title, String grp) {
this.id = id;
this.title= title;
this.group= grp;
}
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return this.id;
}
public void setGroup(String id) {
this.group = id;
}
public String getGroup() {
return this.group;
}
public void settitle(String title) {
this.title = title;
}
public String gettitle() {
return this.title;
}
}
我的资料库
@Repository
public interface DisplayRepository extends CrudRepository<Display, Long> {
}
接口
public interface IDisplayService {
List<Display> findAll();
}
服务等级
@Service
public class DisplayService implements IDisplayService {
@Autowired
private DisplayRepository repository;
@Override
public List<Display> findAll() {
List<Display> d = (List<Display>) repository.findAll();
return d;
}
}
我尝试编写junit测试用例,但无法加载应用程序。为此编写junit测试用例的正确方法是什么?
这是我为服务编写的测试用例
文件夹:test / java / example / demo / Test.java
@RunWith(MockitoJUnitRunner.class)
@TestPropertySource("classpath:conn.properties")
public class DisplayServiceTest {
@Value("${id}")
private String value;
@Mock
private DisplayRepository DisplayReps;
@InjectMocks
private DisplayService DisplayService;
@Test
public void whenFindAll_thenReturnProductList() {
Menu m = new Menu()
m.setId(value);
List<Display> expectedDisplay = Arrays.asList(m);
doReturn(expectedDisplay).when(DisplayReps).findAll();
List<Display> actualDisplay = DisplayService.findAll();
assertThat(actualDisplay).isEqualTo(expectedDisplay);
}
在test / java / example / demo / resources中 conn.properties id = 2
返回值0 有什么问题? 谢谢
答案 0 :(得分:1)
我设法使您的代码正常工作。我将只发布更改的类:
界面:
public interface DisplayRepository extends CrudRepository<Display, Long> {
Optional<Display> findByTitle(String name);
}
测试类:
@RunWith(SpringRunner.class)
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
@DataJpaTest
public class DisplayRepositoryTest {
@Autowired
private TestEntityManager testEntityManager;
@Autowired
private DisplayRepository productRespository;
@Before()
public void setUp(){
Display m = new Display();
// m.setId(2L); // The ID is autogenerated; can retrieve it from the persistAndFlush result
m.setCategory("Group1");
m.setTitle("Product2");
testEntityManager.persistAndFlush(m);
}
@Test
public void whenFindByName_thenReturnProduct() {
// when
Display product = productRespository.findByTitle("Product2").orElseThrow(() -> new RuntimeException("Product not found"));
// then
assertThat(product.getTitle()).isEqualTo("Product2");
}
@Test
public void whenFindAll_thenReturnProductList() {
// when
List<Display> products = (List<Display>) productRespository.findAll();
// then
assertThat(products).hasSize(1);
}
}
尝试运行您提供的代码时,出现了一些问题:
group
用作Display类中的字段。因此,Hibernate无法创建表,因此我将其重命名为category。findByName
;另外,Display类中没有要映射的字段name
;因此,我添加了方法findByTitle
,因为它是一个现有字段,并且似乎与您在测试方法中查询的值匹配。如果要使用@Mock模拟类,则必须调用:
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
然后您可以照常模拟响应:Mockito.when(DisplayReps.findByTitle("A")).thenReturn(Optional.of(new Display(2L, "ALFA", "GRP1")));