Tests don't include WPILib #16

Closed
gbear605 opened this Issue Feb 13, 2016 · 3 comments

Projects

None yet

2 participants

@gbear605

When I'm making tests and compiling with ant test, it errors with something like the following if it includes any WPILib referencing code. It can however run test files just fine for code that doesn't import WPILib code.

My code: https://github.com/Nashoba-Robotics/2016-Pre-Season-Drivetrain

[junit]   Caused an ERROR
[junit] edu/wpi/first/wpilibj/PIDSource
[junit] java.lang.NoClassDefFoundError: edu/wpi/first/wpilibj/PIDSource
[junit]     at java.lang.ClassLoader.defineClass1(Native Method)
[junit]     at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[junit]     at java.lang.Class.getDeclaredFields0(Native Method)
[junit]     at java.lang.Class.privateGetDeclaredFields(Class.java:2575)
[junit]     at java.lang.Class.getDeclaredFields(Class.java:1908)
[junit]     at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
[junit] Caused by: java.lang.ClassNotFoundException: edu.wpi.first.wpilibj.PIDSource
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

I tried adding <pathelement location="lib/wpilib/java/current/lib/WPILib.jar" />
to build.xml inside of <path id="classpath.test"> ... </path>, but that causes it to error with

[junit] Caused an ERROR
[junit] /private/var/folders/wx/bb5tss6d4ynfzd27z706g9wr0000gp/T/libwpilibJavaJNI7023376253146947704.so: dlopen(/private/var/folders/wx/bb5tss6d4ynfzd27z706g9wr0000gp/T/libwpilibJavaJNI7023376253146947704.so, 1): no suitable image found. Did find:
[junit] /private/var/folders/wx/bb5tss6d4ynfzd27z706g9wr0000gp/T/libwpilibJavaJNI7023376253146947704.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x03
[junit] java.lang.UnsatisfiedLinkError: /private/var/folders/wx/bb5tss6d4ynfzd27z706g9wr0000gp/T/libwpilibJavaJNI7023376253146947704.so: dlopen(/private/var/folders/wx/bb5tss6d4ynfzd27z706g9wr0000gp/T/libwpilibJavaJNI7023376253146947704.so, 1): no suitable image found. Did find:
[junit] /private/var/folders/wx/bb5tss6d4ynfzd27z706g9wr0000gp/T/libwpilibJavaJNI7023376253146947704.so: unknown file type, first eight bytes: 0x7F 0x45 0x4C 0x46 0x01 0x01 0x01 0x03
[junit] at java.lang.ClassLoader$NativeLibrary.load(Native Method)
[junit] at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1929)
[junit] at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1814)
[junit] at java.lang.Runtime.load0(Runtime.java:809)
[junit] at java.lang.System.load(System.java:1083)
[junit] at edu.wpi.first.wpilibj.hal.JNIWrapper.(JNIWrapper.java:47)
[junit] at edu.wpi.first.wpilibj.SerialPort.(SerialPort.java:193)
[junit] at edu.wpi.first.wpilibj.SerialPort.(SerialPort.java:244)
[junit] at edu.nr.lib.navx.NavX.(NavX.java:15)
[junit] at edu.nr.lib.navx.TestNavX.(TestNavX.java:19)
[junit] at AngleGyroCorrectionTest.(AngleGyroCorrectionTest.java:10)
[junit] at java.lang.reflect.Constructor.newInstance(Constructor.java:408)

This log (https://api.travis-ci.org/jobs/109041196/log.txt?deansi=true) with this commit (Nashoba-Robotics/Nashoba-Robotics2016@a99afda) shows the main error.

@michaelsilver
Member

The behavior you get is the expected; running WPILib requires the robot, it's ports and motors, etc. The build.xml is fine as is; we specifically decided not to import WPILib for unit testing, becuase we don't actually need to unit test WPILib, but rather math and other logic around it. What we do is abstract the class under test; making one instance that references WPILib and another that doesn't, and unit test the one that doesn't require WPILib (which only contains the logic and stuff you want to test).

@gbear605

This specific code that I wanted to test was a class that implements PIDSource but otherwise is unconnected to robot code, which seems like something that should be testable. That said, I understand the reasoning behind limiting it.

@gbear605 gbear605 closed this Feb 13, 2016
@michaelsilver
Member

Another thing that you could do if you are interested is using a fake WPILib like 254's, which would allow you to run WPILib like code without the robot. You would have to make a jar for that library and then add it to the lib folder and reference it in the test classpath in the build.xml. The README in 254's 2015 code has some helpful instructions, with a reference to a ruby script they use to set up the WPILib mock using their fake WPILib. It should be fairly simple to implement, but we haven't had the chance to explore that fake WPILib. If you do go this route, we invite you to fork this repo and make a pull request! And if you have questions along the way regarding our build.xml or Travis, we are happy to help answer them!

@michaelsilver michaelsilver referenced this issue in Nashoba-Robotics/Nashoba-Robotics2016 Feb 14, 2016
Merged

Abstract away dependance of AngleGyroCorrection on PIDSource #2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment