简体   繁体   中英

Catch Unresolved Compilation Errors in Java

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.

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM