Junit在内存数据库中使用h2测试@getmappings

时间:2019-11-25 14:19:58

标签: java spring-boot junit

我在测试DbRequest控制器时遇到问题。我有一个单元测试正在工作,但是无法对DBRequest控制器GET映射进行单元测试,该映射使用hibernate进行数据库查找。我在为junit测试创建的内存数据库中有一个H2。 我尝试了各种不同的设置,但似乎没有任何正常工作。

编辑以下内容,我得到一个NullPointer,

java.lang.NullPointerException
    at com.lmig.informaticaservice.api.DBcontroltest.saveTest(DBcontroltest.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

这是经过编辑的测试。

@RunWith(SpringRunner.class)
@SpringBootTest

@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class DBcontroltest {



@Autowired
DbRequest dbRequest;


    @Autowired
    ConnectionRequestRepository connectionRequestRepository;

    private MockMvc mockMvc;
    // @Autowired
   //private TestEntityManager entityManager;



     @Test
    public void saveTest() throws Exception {
         ConnectionRequest connectionRequest = new ConnectionRequest((long) 1, "test");
         connectionRequestRepository.save(connectionRequest);


         System.out.println(connectionRequestRepository.findAll().toString());

         mockMvc.perform(get("/api/selectDB/{connectionId}" ,1))
                 .andExpect(status().isOk());


    }

}


Typical JPA repository 

    package com.test.models;

    import org.springframework.data.jpa.repository.JpaRepository;

    public interface ConnectionRequestRepository extends JpaRepository<ConnectionRequest, Long> {

    }

这是我的控制者。

package com.test.api;

import com.models.ConnectionRequest;
import com.test.models.ConnectionRequestRepository;

import java.util.List;

import javax.validation.Valid;

import lombok.Data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Data
@RestController
@RequestMapping("/api/")
public class DbRequest {

    @Autowired
    private ConnectionRequestRepository connectionRequestRepository;
    private ConnectionRequest connectionRequest;

    @GetMapping("/selectDB")
    public List<ConnectionRequest> getAllRequests() {
        return connectionRequestRepository.findAll();
    }

    @GetMapping("/selectDB/{connectionId}")
    public ResponseEntity<ConnectionRequest> getRequestById(@PathVariable("connectionId") Long connectionId) throws Exception {
        ConnectionRequest connectionRequest = connectionRequestRepository.findById(connectionId)
            .orElseThrow(() -> new Exception("Connection Request " + connectionId + " not found"));
        return ResponseEntity.ok().body(connectionRequest);
    }
}

这是数据库的模型。

package com.testing.models;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Data
@Entity
@Table(name = "connrequest", schema = "testschema")
@AllArgsConstructor
@NoArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class ConnectionRequest {

    @Id
    @Column(name = "connection_id", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long connectionId;

    @Column(name = "requestor", nullable = false)
    private String requestor;
}

1 个答案:

答案 0 :(得分:0)

似乎是ConnectionRequest的PK上的批注的打开。

注释@GeneratedValue告诉JPA它需要确定该值,因此将为ID提供的任何值将被主动丢弃。来自文档

  

表示持久性提供程序必须使用数据库标识列为实体分配主键

要解决此问题,请尝试删除该注释,以便您必须始终提供一个ID,或者在将实体保存到测试中之后,获取ID被分配并使用该connectionRequestRepository.getOne()调用ID