Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为spring boot应用程序编写Junit测试用例?_Java_Junit_Entity - Fatal编程技术网

Java 如何为spring boot应用程序编写Junit测试用例?

Java 如何为spring boot应用程序编写Junit测试用例?,java,junit,entity,Java,Junit,Entity,我必须编写一些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 Di

我必须编写一些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> {

}
@存储库
公共接口DisplayRepository扩展了Crudepository{
}
接口

public interface IDisplayService {
    List<Display> findAll();
}
公共接口IDisplayService{
列出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;
}
}
@服务
公共类DisplayService实现IDisplayService{
@自动连线
私有存储库;
@凌驾
公共列表findAll(){
List d=(List)repository.findAll();
返回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);
}
@RunWith(MockitoJUnitRunner.class)
@TestPropertySource(“类路径:conn.properties”)
公共类DisplayServiceTest{
@值(“${id}”)
私有字符串值;
@嘲弄
私有DisplayReps存储库DisplayReps;
@注射模拟
专用显示服务显示服务;
@试验
当findell\u thenReturnProductList()时公共无效{
菜单m=新菜单()
m、 setId(值);
List expectedDisplay=Arrays.asList(m);
doReturn(expectedDisplay).when(DisplayReps.findAll();
List actualDisplay=DisplayService.findAll();
资产(实际显示)。等质量(预期显示);
}
在test/java/example/demo/resources中 康涅狄格州酒店 id=2

它返回0作为值 什么问题?
谢谢

我已设法使您的代码正常工作。我将只发布更改的类:

界面:

public interface DisplayRepository extends CrudRepository<Display, Long> {

   Optional<Display> findByTitle(String name);
}

然后,您可以像往常一样模拟响应:
Mockito.when(displayrps.findbytle(“A”))。然后返回(可选的.of(新显示(2L,“ALFA”,“GRP1”));

你能发布你创建的测试类吗?你对要使用SpringRunner运行的类进行了注释吗?你可能还想添加@SpringBootTest。也可以在这里查看教程我已经用测试用例更新了我的帖子使用HSQL或H2 DB作为代理数据库DisplayRepository存储
菜单
。请发布该类。它的显示类本身不是有人能写一个演示测试类并解释吗?我试过了,但现在我得到了SQL错误提示错误:列“category”类型为integer,但表达式类型为character Variable。但类别仅为字符串值。我不知道为什么它会显示integer。我已使用H2 DB进行了测试。能否切换到它以查看代码是否运行?如果您使用的是现有的DB实例,可能应该删除该表以显示,然后重试,或者将Hibernate配置为drop表格
hibernate.hbm2ddl.auto=create
(仅在运行此测试时)。嘿,我已经更新了单元测试。你能告诉我这有什么问题吗?如果我手动输入id为2,它会工作,但通过属性文件它不会工作。你应该使用
@RunWith(SpringRunner.class)运行测试类
。按照您的设置方式,Spring没有尝试加载其上下文,配置被忽略。我已编辑了响应。请将问题标记为已回答,因为您开始询问与原始问题无关的其他问题,您应该为他们提出新问题。
@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);
}
}
@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
}