Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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
SpringJavaOracle获取测试_Java_Spring_Spring Mvc_Testing_Junit - Fatal编程技术网

SpringJavaOracle获取测试

SpringJavaOracle获取测试,java,spring,spring-mvc,testing,junit,Java,Spring,Spring Mvc,Testing,Junit,我尝试测试我和数据库的连接,当我尝试在IntelliJ IDEA中使用查询时,它可以工作,但单元测试失败。我刚开始学java,所以我可能做错了什么,你能告诉我如何做对吗?我试着测试,若我从表工作者那个里得到所有的值,第二个测试是若我从表工作者那个里得到一个特殊的值 工人模范班: package com.bmp.bmp.model; import javax.persistence.*; @Entity @Table(name = "WORKERS") public class Wor

我尝试测试我和数据库的连接,当我尝试在IntelliJ IDEA中使用查询时,它可以工作,但单元测试失败。我刚开始学java,所以我可能做错了什么,你能告诉我如何做对吗?我试着测试,若我从表工作者那个里得到所有的值,第二个测试是若我从表工作者那个里得到一个特殊的值

工人模范班:

    package com.bmp.bmp.model;

import javax.persistence.*;

@Entity
@Table(name = "WORKERS")
public class Workers {

  @Id
  @Column(name = "WORKER_ID")
  private int workerId;
  @Column(name = "FIRSTNAME")
  private String firstname;
  @Column(name = "LASTNAME")
  private String lastname;
  @Column(name = "WORKPLACE_ID")
  private int workplaceId;

  public Workers(int workerId, String firstname, String lastname, int workplaceId)
  {
      super();
      this.workplaceId = workplaceId;
      this.lastname = lastname;
      this.firstname = firstname;
      this.workerId = workerId;
  }
  public Workers()
  {
      super();
  }

  public int getWorkerId() {
    return workerId;
  }

  public void setWorkerId(int workerId) {
    this.workerId = workerId;
  }


  public String getFirstname() {
    return firstname;
  }

  public void setFirstname(String firstname) {
    this.firstname = firstname;
  }


  public String getLastname() {
    return lastname;
  }

  public void setLastname(String lastname) {
    this.lastname = lastname;
  }


  public int getWorkplaceId() {
    return workplaceId;
  }

  public void setWorkplaceId(int workplaceId) { this.workplaceId = workplaceId;
  }

}
工作存储库类:

    package com.bmp.bmp.service.implement;

import com.bmp.bmp.model.Workers;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

@Repository
public class Worker implements com.bmp.bmp.service.interfaces.Worker
{
    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void save(Workers workers) {
        String query = "insert into Workers (WORKER_ID, FIRSTNAME, LASTNAME, WORKPLACE_ID) values ((select max(WORKER_ID)+1 from workers),?,?,?); commit;";
        Connection con = null;
        PreparedStatement ps = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);

            ps.setString(1, workers.getFirstname());
            ps.setString(2, workers.getLastname());
            ps.setInt(3, workers.getWorkplaceId());
            int out = ps.executeUpdate();
            if(out !=0){
                System.out.println("Worker saved with id="+workers.getWorkerId());
            }else System.out.println("Worker save failed with id="+workers.getWorkerId());
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Workers getById(int id) {
        String query = "select FIRSTNAME, LASTNAME, WORKPLACE_ID from WORKERS where WORKER_ID = ?";
        Workers workers = null;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if(rs.next()){
                workers = new Workers();
                workers.setWorkerId(id);
                workers.setFirstname(rs.getString("firstName"));
                workers.setLastname(rs.getString("lastname"));
                workers.setWorkplaceId(rs.getInt("workplace_id"));
                System.out.println("Worker Found::"+workers);
            }else{
                System.out.println("No worker found with id="+id);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                rs.close();
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return workers;
    }

    @Override
    public void update(Workers workers) {
        String query = "update Workers set FIRSTNAME=?, LASTNAME=?, WORKPLACE_ID=? where WORKER_ID=?; commit;";
        Connection con = null;
        PreparedStatement ps = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            String firstName = ("'" + workers.getFirstname() + "'");
            String lastName = ("'" + workers.getLastname() + "'");
            ps.setString(1, firstName);
            ps.setString(2, lastName);
            ps.setInt(3, workers.getWorkplaceId());
            ps.setInt(4,workers.getWorkerId());
            int out = ps.executeUpdate();
            if(out !=0){
                System.out.println("Worker updated with id="+workers.getWorkerId());
            }else System.out.println("No worker found with id="+workers.getWorkerId());
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void deleteById(int id) {
        String query = "delete from WORKERS where WORKER_ID=?; commit;";
        Connection con = null;
        PreparedStatement ps = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            ps.setInt(1, id);
            int out = ps.executeUpdate();
            if(out !=0){
                System.out.println("Worker deleted with id="+id);
            }else System.out.println("No worker found with id="+id);
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Workers> getAll() {
        String query = "select WORKER_ID, FIRSTNAME, LASTNAME, WORKPLACE_ID from WORKERS";
        List<Workers> empList = new ArrayList<Workers>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            rs = ps.executeQuery();
            while(rs.next()){
                Workers workers = new Workers();
                workers.setWorkerId(rs.getInt("worker_id"));
                workers.setFirstname(rs.getString("firstname"));
                workers.setLastname(rs.getString("lastname"));
                workers.setWorkplaceId(rs.getInt("workplace_id"));
                empList.add(workers);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                rs.close();
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return empList;
    }
}

缺少上下文配置导致您的工作进程无法初始化,因此导致空指针

用实际名称替换ContextConfiguration名称,它应该自动连接

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration(locations = { "classpath:META-INF/your-spring-context.xml" })
public class WorkerTest {
    @Autowired
    Worker worker;

    @Test
    public void getAll() {
        List<Workers> workersList = worker.getAll();
    }

    @Test
    public void getById() {
        Workers workers = worker.getById(7);
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(位置={“classpath:META-INF/your spring context.xml”})
公共班工人测验{
@自动连线
工人;
@试验
public void getAll(){
List workersList=worker.getAll();
}
@试验
public void getById(){
Workers=worker.getById(7);
}
}
编辑:建议不要使用XML文件,唯一的选择是将基于注释的Spring与一些具有@Configuration的YourAppConfiguration.class一起使用

为此,我们只需要稍微更改ContextConfiguration

@RunWith( SpringJUnit4ClassRunner.class )
   @ContextConfiguration(classes=YourAppConfiguration.class, loader=AnnotationConfigContextLoader.class)
    public class WorkerTest {
        @Autowired
        Worker worker;

        @Test
        public void getAll() {
            List<Workers> workersList = worker.getAll();
        }

        @Test
        public void getById() {
            Workers workers = worker.getById(7);
        }
    }
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=YourAppConfiguration.class,loader=AnnotationConfigContextLoader.class)
公共班工人测验{
@自动连线
工人;
@试验
public void getAll(){
List workersList=worker.getAll();
}
@试验
public void getById(){
Workers=worker.getById(7);
}
}
如果仍然没有清除,请退出autowire并使用新操作符

@RunWith( SpringJUnit4ClassRunner.class )
        public class WorkerTest {

            Worker worker=new Worker();

            @Test
            public void getAll() {
                List<Workers> workersList = worker.getAll();
            }

                @Test
                public void getById() {
                    Workers workers = worker.getById(7);
                }
            }
@RunWith(SpringJUnit4ClassRunner.class)
公共班工人测验{
工人=新工人();
@试验
public void getAll(){
List workersList=worker.getAll();
}
@试验
public void getById(){
Workers=worker.getById(7);
}
}

是否以任何方式配置了
WorkerTest
?从stacktrace上看,似乎
工作者
字段未初始化。它为null,因此引发NullPointerException。如果您需要有关Spring测试配置的帮助,请参考以下链接:可能不是,这是我有史以来的第一次测试,因此可能有问题。为什么要
new
ing您的自动连线字段…?是否需要使用上下文配置?我这样问是因为,我没有使用任何XML。你是否使用基于spring注释的@Configuration,并在某些类中使用@Configuration,如自动关联依赖项的应用程序配置。我已经对我的答案进行了相应的编辑,请看一看,并让我知道它是否适用于你。嗨,我尝试了,但仍然不起作用。我更新了代码示例和错误消息。你能看看吗,怎么了?ThanksDon’使用autowired和new都可以删除autowired,就像我的第三个代码一样
@RunWith( SpringJUnit4ClassRunner.class )
   @ContextConfiguration(classes=YourAppConfiguration.class, loader=AnnotationConfigContextLoader.class)
    public class WorkerTest {
        @Autowired
        Worker worker;

        @Test
        public void getAll() {
            List<Workers> workersList = worker.getAll();
        }

        @Test
        public void getById() {
            Workers workers = worker.getById(7);
        }
    }
@RunWith( SpringJUnit4ClassRunner.class )
        public class WorkerTest {

            Worker worker=new Worker();

            @Test
            public void getAll() {
                List<Workers> workersList = worker.getAll();
            }

                @Test
                public void getById() {
                    Workers workers = worker.getById(7);
                }
            }