Robolectric抛出ResourceNotFound异常

时间:2015-10-30 07:13:58

标签: android robolectric

Robolectric在运行时读取 public class Gui2 extends JFrame{ private JPanel inputoutput = new JPanel(); private JPanel container = new JPanel(); private CartesianPanel mousepanel = new CartesianPanel(); private JLabel statusbar = new JLabel("default"); // to give status private JPanel shape = new JPanel(); //where button is private enum DrawingShape {ELLIPSE, RECTANGLE, CIRCLE, TRIANGLE, POLYGON, LINE; } private DrawingShape shapes; private String design; private int count, count1; private String lineAlgo; private int X1, X2, Y1, Y2; private Color colour = (Color.BLACK); private int a=0, n=0; public int[] x_points = new int[10]; public int[] y_points = new int[10]; //buttons to set enable, disable private JRadioButton thick1 = new JRadioButton("1"); private JRadioButton thick3 = new JRadioButton("2"); private JRadioButton thick5 = new JRadioButton("3"); Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); private static int scale = 1; private double tix,tiy; private double radius; private double p; public Gui2(){ // interface implementation goes here } private class click extends JPanel implements ActionListener { public void actionPerformed(ActionEvent e) { Handlerclass handler = new Handlerclass(); mousepanel.addMouseListener(handler); mousepanel.addMouseMotionListener(handler); String command = e.getActionCommand(); if (command.equals("Color")) { colour = JColorChooser.showDialog(null, "pick your colour", colour); if (colour == null) colour = (Color.BLACK); } if (command.equals("Rectangle")) { shapes = DrawingShape.RECTANGLE; } if (command.equals("Circle")) { shapes = DrawingShape.CIRCLE; } if (command.equals("Triangle")) { shapes = DrawingShape.TRIANGLE; } if (command.equals("Ellipse")) { shapes = DrawingShape.ELLIPSE; } if (command.equals("Polygon")) { shapes = DrawingShape.POLYGON; String fn = JOptionPane.showInputDialog("Enter number of edges"); n = Integer.parseInt(fn); } if (command.equals("Line")) { shapes = DrawingShape.LINE; thick1.setEnabled(true); thick3.setEnabled(true); thick5.setEnabled(true); } if(command.equals("Solid")) design = "Solid"; if(command.equals("Dotted")) design = "Dotted"; if(command.equals("Dashed")) design = "Dashed"; if(command.equals("1")) design = "1"; if(command.equals("2")) design = "2"; if(command.equals("3")) design = "3"; if (command.equals("Bresenham Algorithm")) { lineAlgo = "Bresenham"; } if (command.equals("DDA")) { lineAlgo = "DDA"; } } } // Other setter methods public void setX1(int x) { X1= x; } public void setY1(int y) { Y1 = y; } public void setX2(int x) { X2 = x; } public void setY2(int y) { Y2 = y; } // Getter methods public Color getColour() { return colour; } public int getX1() { return X1; } public int getY1() { return Y1; } public int getX2() { return X2; } public int getY2() { return Y2; } private class Handlerclass extends JPanel implements MouseListener, MouseMotionListener { public void mouseClicked(MouseEvent e) { statusbar.setText(String.format("Clicked at %d,%d", e.getX(), e.getY())); if (shapes == shapes.TRIANGLE) { x_points[a] = e.getX(); y_points[a] = e.getY(); a++; if (a>=3) { System.out.println(x_points); drawShape(x_points,y_points,3,e.getButton()==MouseEvent.BUTTON3, true); a=0; x_points = new int[10]; y_points = new int[10]; } } if (shapes==shapes.POLYGON) { x_points[a] = e.getX(); y_points[a] = e.getY(); a++; if (a>=n) { System.out.println(x_points); drawShape(x_points,y_points,n,e.getButton()==MouseEvent.BUTTON3, true); a=0; x_points = new int[10]; y_points = new int[10]; } } } public void mousePressed(MouseEvent e){ statusbar.setText(String.format("Pressed at %d,%d", X1, Y1)); setX1(e.getX()); setY1(e.getY()); } public void mouseReleased(MouseEvent e){ statusbar.setText(String.format("You release the mouse at: %d, %d", e.getX(), e.getY())); setX2(e.getX()); setY2(e.getY()); drawShape(X1, Y1, X2, Y2, e.getButton()==MouseEvent.BUTTON3, true); } public void mouseEntered(MouseEvent event){ statusbar.setText("You have enter the area"); } public void mouseExited(MouseEvent event){ statusbar.setText("The mouse has left the window"); } //mousemotion public void mouseDragged(MouseEvent e) { statusbar.setText("You are draggin the mouse to form shape"); } public void mouseMoved(MouseEvent event){ statusbar.setText("You moved the mouse"); } } private void drawShape(int x1, int y1, int x2, int y2, boolean fill, boolean paint) { // All painting is done on the panel's Graphics object Graphics g = getGraphics(); Graphics2D g2d = (Graphics2D) g.create(); g.setColor(getColour()); if (paint) // Shape is to be drawn permanently g.setPaintMode(); else g.setXORMode(mousepanel.getBackground()); int minX = Math.min(x1, x2); int minY = Math.min(y1, y2); int dx = Math.abs(x2 - x1); int dy = Math.abs(y2 - y1); switch (shapes) { case LINE: if(lineAlgo == "Bresenham") { if ((x1 == x2) && (y1 == y2)) { if(design =="Solid" || design=="1") g.drawLine(x1, y1, x1, y1); if(design =="Dashed") { Stroke dashed = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0); g2d.setStroke(dashed); g2d.drawLine(x1, y1, x2, y2); //gets rid of the copy g2d.dispose(); } if(design =="Dotted") { if(count<1) { g.drawLine(x1, y1, x1, y1); count++; if(count==1) count1=0; } else { count1++; if(count1==2) count=0; } } } else { int rozdil = dx - dy; int pos_x, pos_y; if (x1 < x2) pos_x = 1; else pos_x = -1; if (y1 < y2) pos_y = 1; else pos_y = -1; while ((x1 != x2) || (y1 != y2)) { int p = 2 * rozdil; if (p > -dy) { rozdil = rozdil - dy; x1 = x1 + pos_x; } if (p < dx) { rozdil = rozdil + dx; y1 = y1 + pos_y; } if(design =="Solid" || design=="1") g.drawLine(x1, y1, x1, y1); if(design =="Dashed") { Stroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0); g2d.setStroke(dashed); g2d.drawLine(x1, y1, x2, y2); //gets rid of the copy g2d.dispose(); } if(design =="Dotted") { if(count<1) { g.drawLine(x1, y1, x1, y1); count++; if(count==1) count1=0; } else { count1++; if(count1==2) count=0; } } } } } else if(lineAlgo == "DDA") { if (dy <= dx) { if ((x1 == x2) && (y1 == y2)) { g.drawLine(x1, y1, x1, y1); } else { if (x2 < x1) { int tmp = x2; x2 = x1; x1 = tmp; tmp = y2; y2 = y1; y1 = tmp; } double k = (double)dy/dx; int new_y; double y = (double)y1; for (int x = x1 ; x <= x2 ; x++) { new_y = (int)Math.round(y); if(design =="Solid" || design=="1") g.drawLine(x, new_y, x, new_y); if(design =="Dashed") { Stroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0); g2d.setStroke(dashed); g2d.drawLine(x, new_y, x, new_y); //gets rid of the copy g2d.dispose(); } if(design =="Dotted") { if(count<1) { g.drawLine(x, new_y, x, new_y); count++; if(count==1) count1=0; } else { count1++; if(count1==2) count=0; } } y += k; } } } else { if (y2 < y1) { int tmp = x2; x2 = x1; x1 = tmp; tmp = y2; y2 = y1; y1 = tmp; } double k = (double)dx/dy; int new_x; double x = (double)x1; for (int y = y1; y <= y2; y++) { new_x = (int)Math.round(x); if(design =="Solid" || design=="1") g.drawLine(new_x, y, new_x, y); if(design =="Dashed") { Stroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0); g2d.setStroke(dashed); g2d.drawLine(new_x, y, new_x, y); //gets rid of the copy g2d.dispose(); } if(design =="Dotted") { if(count<1) { g.drawLine(new_x, y, new_x, y); count++; if(count==1) count1=0; } else { count1++; if(count1==2) count=0; } } x += k; } } } else { if(design==null){ g.drawLine(X1, Y1, X2, Y2); } if(design =="Solid" || design=="1") g.drawLine(X1, Y1, X2, Y2); if(design =="Dashed") { Stroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0); g2d.setStroke(dashed); g2d.drawLine(X1, Y1, X2, Y2); //gets rid of the copy g2d.dispose(); } if(design =="Dotted") { if(count<1) { g.drawLine(X1, Y1, X2, Y2); count++; if(count==1) count1=0; } else { count1++; if(count1==2) count=0; } } if(design =="2") { Stroke stroke = new BasicStroke(3f); g2d.setStroke(stroke); g2d.drawLine(X1, Y1, X2, Y2); g2d.dispose(); } if(design =="3") { Stroke stroke = new BasicStroke(5f); g2d.setStroke(stroke); g2d.drawLine(X1, Y1, X2, Y2); g2d.dispose(); } } break; case RECTANGLE: if (fill) g.fillRect(minX, minY, dx, dy); else g.drawRect(minX, minY, dx, dy); break; case CIRCLE: radius=Math.sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1))); //block radius of circle Dimension cal = Toolkit.getDefaultToolkit().getScreenSize(); if ((y1-radius)<35) { radius=y1-35; } if ((x1-radius)<15) { radius=x1-15; } if ((x1+radius)>cal.width) { radius=cal.width-x1; } if ((y1+radius)>cal.height-35) { radius=cal.height-35-y1; } tix=0; tiy=radius; do { if(p<0) p=p+(2*tix)+1; else { p=p+(2*tix)+1-(2*tiy); tiy=tiy-1; } tix=tix+1; g2d.draw(new Line2D.Double(x1+tix,y1+tiy,x1+tix,y1+tiy)); g2d.draw(new Line2D.Double(x1+tiy,y1+tix,x1+tiy,y1+tix)); g2d.draw(new Line2D.Double(x1+tiy,y1-tix,x1+tiy,y1-tix)); g2d.draw(new Line2D.Double(x1+tix,y1-tiy,x1+tix,y1-tiy)); g2d.draw(new Line2D.Double(x1-tix,y1-tiy,x1-tix,y1-tiy)); g2d.draw(new Line2D.Double(x1-tiy,y1-tix,x1-tiy,y1-tix)); g2d.draw(new Line2D.Double(x1-tiy,y1+tix,x1-tiy,y1+tix)); g2d.draw(new Line2D.Double(x1-tix,y1+tiy,x1-tix,y1+tiy)); }while(tix<tiy); DecimalFormat df = new DecimalFormat("#.##"); //g.drawString("Centre("+df.format(((x1-size.width/2.0)/20.0)*scale) +","+ df.format(((size.height/2.0-y1)/20.0)*scale)+")",x1,y1); g.drawString("radius:" +df.format(((radius/20.0)*scale)),(int) x1,(int) y1); break; case ELLIPSE: int xc = x1; int yc = y1; int x = x2; int y = y2; int sigma; int xr=(int)Math.sqrt(((x-xc)*(x-xc))); //+((y1-Y)*(y1-Y))); int yr=(int)Math.sqrt((y-yc)*(y-yc)); int a2 = xr * xr; int b2 = yr * yr; int fa2 = 4 * a2, fb2 = 4 * b2; if ((y-yr)<35) { yr=y-35; } if ((x-xr)<15) xr=x-15; if ((x+xr)>size.width) xr=size.width-x; if ((y+yr)>size.height-35) { yr=size.height-35-y; } DecimalFormat da = new DecimalFormat("#.##"); //g.drawString("Centre("+da.format(((xc-size.width/2)/20.0)*scale) +","+ da.format((((size.height/2)-yc)/20.0)*scale)+")", (int)xc, (int)yc); /* first half */ for (x = 0, y = yr, sigma = 2*b2+a2*(1-2*yr); b2*x <= a2*y; x++) { g2d.drawLine((int)(xc + x), (int)(yc + y),(int)(xc + x), (int)(yc + y)); g2d.drawLine ((int)(xc - x),(int)( yc + y),(int)(xc - x),(int)( yc + y)); g2d.drawLine ((int)(xc + x), (int)(yc - y),(int)(xc + x), (int)(yc - y)); g2d.drawLine ((int)(xc - x), (int)(yc - y),(int)(xc - x), (int)(yc - y)); if (sigma >= 0) { sigma += fa2 * (1 - y); y--; } sigma += b2 * ((4 * x) + 6); } /* second half */ for (x = xr, y = 0, sigma = 2*a2+b2*(1-2*xr); a2*y <= b2*x; y++) { g2d.drawLine((int)(xc + x), (int)(yc + y),(int)(xc + x), (int)(yc + y)); g2d.drawLine ((int)(xc - x), (int)(yc + y),(int)(xc - x), (int)(yc + y)); g2d.drawLine ((int)(xc + x), (int)(yc - y),(int)(xc + x), (int)(yc - y)); g2d.drawLine ((int)(xc - x), (int)(yc - y),(int)(xc - x), (int)(yc - y)); if (sigma >= 0) { sigma += fb2 * (1 - x); x--; } sigma += a2 * ((4 * y) + 6); } break; } } private void drawShape(int[] x, int[] y, int N, boolean fill, boolean paint) { Graphics g = getGraphics(); g.setColor(getColour()); if (paint) g.setPaintMode(); else g.setXORMode(mousepanel.getBackground()); switch (shapes) { case TRIANGLE: if (fill) g.fillPolygon(x, y, N); else g.drawPolygon(x, y, N); break; case POLYGON: if (fill) g.fillPolygon(x, y, N); else g.drawPolygon(x, y, N); break; } } class CartesianPanel extends JPanel { //implementation of scalable Cartesian plane } } } 内的res文件。但是,在进行测试时,它会抛出/build/intermediates/res。我的Android应用程序有许多不同的风格,不同的风格有不同的ResourceNotFoundException。如果这是问题,是否有任何修复方法?

2 个答案:

答案 0 :(得分:0)

是的,当风味具有不同的应用程序ID时,这是一个已知问题。您可以使用packageName属性来解决此问题。 https://github.com/robolectric/robolectric/issues/2110

@Config(packageName = "app.base.package.name")

答案 1 :(得分:0)

尝试使用自定义的roboletric测试运行器类来告诉roboletric资源类的位置:

public class CustomRobolectricGradleTestRunner extends RobolectricGradleTestRunner {

    public CustomRobolectricGradleTestRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    // Fix for the NotFound error with openRawResource()
    @Override
    protected AndroidManifest getAppManifest(Config config) {
        String manifest = "src/main/AndroidManifest.xml";
        String res = String.format("../app/build/intermediates/res/merged/%1$s/%2$s", BuildConfig.FLAVOR, BuildConfig.BUILD_TYPE);
        String asset = "src/test/assets";
        return new AndroidManifest(Fs.fileFromPath(manifest), Fs.fileFromPath(res), Fs.fileFromPath(asset));
    }
}

在测试类中,指定自定义测试运行器类,如:

@Config(constants = BuildConfig.class)
@RunWith(CustomRobolectricGradleTestRunner.class)
public class DataServiceTest {
}