5

I'm currently a Teacher's Assistant for a class that uses Java. I'm trying to write a snippet of code that will test to make sure that student's methods are correct, but often times the student won't even implement the method, or they'll call it something incorrect, which obviously will cause a Unresolved Compilation problem when my test code is run. Is there a way to catch this error during runtime, so that my test code can execute without having to play around with the code submitted by the student?

  • edit: Just discovered that an Unresolved compilation problem is generated by the compiler before runtime. With this in mind, is there a way to do what I explained above?
  • edit: Also, I don't have any control over the way that assignments are structured, so I can't introduce interfaces, or stubs, etc.
  • 5
    can't you provide them an [iterface](http://download.oracle.com/javase/tutorial/java/IandI/createinterface.html) and 'make' them implement it, as a part of the task description? I think it should be a simpler solution.. :\ – amit Oct 25 '11 at 18:06
  • It would definitely be better, but I have to rely on what the teacher provides for them, and usually the teacher doesn't provide an interface. – Michael Hogenson Oct 25 '11 at 18:08
  • How do you want to link your verifier to your student's code? Do they just provide a method, then you paste it into your source and compile it? I guess @amit comment provides the answer... – home Oct 25 '11 at 18:11
  • A couple people mentioned reflection, which is exactly what I was looking for. [Here](http://java.sun.com/developer/technicalArticles/ALT/Reflection/) is a good link I found. – Michael Hogenson Oct 25 '11 at 18:41
  • Also consider writing a classloader for pulling in code from a named jar. – Thorbjørn Ravn Andersen May 25 '12 at 17:27

4 Answers4

2

If I were a TA, I would write some unit tests and tell the students to make the test pass. Get them into testing early on.

If the code doesn't compile, it doesn't make sense to detect that at runtime. You can't run if it won't compile.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • Again, I'm restricted by what the teacher implements. My job is just to grade. It kind of makes sense if I want to automate a test case where if they don't implement the method they don't get the points, but if they implement everything else, then they get the points for what they do without the whole tests crashing. – Michael Hogenson Oct 25 '11 at 18:15
  • convince the instructor to have the assignments fit into a unit testing framework – hvgotcodes Oct 25 '11 at 18:50
  • @hvgotcodes It can definitely run without compiling. Not if it's the main class but another class that is called later. Or it may compile and run perfectly but later on some secondary JAR is deleted for some reason and the application will fail on runtime – golimar Oct 17 '19 at 14:08
2

If you use reflection you can check if the method exists, invoke methods, and iterate though existing methods to possibly find the student's method.

See the UrlClassLoader class for loading the students code from file.

xthexder
  • 1,555
  • 10
  • 22
  • Doing a quick Google search on this topic indicates that reflection is possibly what I'm looking for. Thank you. If you have any good links on reflection, please feel free to post them! – Michael Hogenson Oct 25 '11 at 18:21
  • I would but I'm on a mobile device =) It's hard to get links on an ipod. – xthexder Oct 25 '11 at 18:24
1

If I understand your question, you're trying to validate both the interface that the student implements as well as the correctness of the implementation. The reflection API would allow to determine if a Class has implemented the correct API and if it has, invoke that API. Look at java.lang.Class, java.lang.Method, etc.

sceaj
  • 1,573
  • 3
  • 12
  • 24
1

Sounds like you have entered into the object orient portion of your course now. I think it might be a good idea to create a super class that has all of the assigned methods stubbed out. Then students can simply extend that super class and implement the methods. Your stubbed ones should just throw a RuntimeException which would cause the corresponding unit tests to fail. That should allow you to create unit tests with having to worry too much about students not implementing particular methods.

Chris J
  • 9,164
  • 7
  • 40
  • 39
  • Sounds like a good idea, which is what I would do if I had more control over the assignments. I should make an edit in my question stating this, but I don't have any control over the creation of the assignments. I'm purely a grader. – Michael Hogenson Oct 25 '11 at 18:24