I use Eclipse and Maven and have made a single JUnit test, just to test if it works. The first time I ran the test everything went as expected, but since then, every time I run it, I get the same result, even though I'm changing the actual test-file's content.
I tried just emptying the file, then it said that there are no JUnit test files. But as long as I just have #Test in front of a method in that file, I always get the same results.
Anyone know why that could be?
I tried restarting eclipse.
EDIT:
Just realized that I'm not getting the test results since there is an exception before it gets tested. So, the problem is that I'm always getting the exception even though I changed the file.
Testclass:
public class zipTester {
/**
* The class to be tested on.
*/
private Generator generator;
/**
* Sets up the generator.
*/
#Before
public void setUp() {
generator = new Generator(null, 0);
}
/**
* Creates a zip file and tests whether it exists.
*/
#Test
public void testCreateZip() {
File file = new File("/Users/nicola/Documents/trunk);
generator.createZip(file, new Vector<File>());
}
}
Changed TestClass:
public class zipTester {
#Test
public void heyo() {
}
}
Always getting the following Exception:
java.io.FileNotFoundException: /Users/nicola/Documents/trunk (No such file or directory)
...
1 May be you should clean your project
2 and then recheck project-BuildAutomatically
if still have something wrong,
you can right-click your project "java build path" and open the first tab Source
set default output folder content "test/target/classes"
good luck :)
i think your code was not compiled by eclipse
Seems that happen when there is no file in the relevant location.Because you are passing the file to Generator and try to access that file.Then this exception happen as there are no file to access with generator.
You can follow the below steps to avoid this scenario.
First check is that file exist in that location as below,
File file = new File("/Users/nicola/Documents/trunk");
assertTrue(file.exists());
Then check with your Generator.
Related
I'm designing unit tests for the following method:
public void loadFile(Path filename) throws IOException {
try {
// do something
} catch (RuntimeException ex) {
Files.move(filename, filename.resolveSibling("ERROR_" + filename.getFileName()));
return;
}
Files.delete(filename);
}
And I'm looking for a way of testing it without producing different results, since the input file is going to be either renamed or deleted after the execution test:
If RuntimeException takes place, file is renamed.
If execution goes as expected, file is deleted.
Either way, consecutive runs of the test will make it fail because file will be named different or it won't exist... How could I prevent that? Also, is there a way to verify Files#move(Path, Path, CopyOption...) and Files#delete(Path) were invoked?
I would recommend to use new #TempDir annotation from junit 5.
Docs:
When the end of the scope of a temporary directory is reached, i.e. when the test method or class has finished execution, JUnit will attempt to recursively delete all files and directories in the temporary directory and, finally, the temporary directory itself.
In that case you test cases will look as the next:
#Test
void loadsFileCorrectly(#TempDir Path temp) {
final Path file = temp.resolve("testable.file");
loadFile(file);
Assertions.assertTrue(Files.notExists(file));
}
#Test
void loadsFileWithException(#TempDir Path temp) {
final Path file = temp.resolve("testable.file");
loadFile(file);
Assertions.assertTrue(Files.exists(file.resolveSibling("ERROR_" + file.getFileName())));
}
In both scenarios, the created files will be automatically deleted after execution.
When I run the below code, I get the error that Could not find or load main class. I have removed the package and created it again. But the error is still exist. I did some methods to fix it such as right clicking on package name -> properties -> run option to change the main method but there is nothing. But if I create another package name and write this code in it, the program work.
package craps;
public class Craps {
public static void main(String[] args) {
int number = 10;
System.out.println(number);
}
}
Your code is not having any errors
I don't know what is happening in Netbeans .I have been using this for years and living with this kind of errors.
perhaps you get this when netbeans running out of memory and that particular moment you are editing this file.
My workaround for this kind of errors are
1.Do some dummy editing in that file like commenting some empty line // and save All and recompile it
2.Close and open this project (Sometimes work)
I am editing a very old piece of code off a plugin project. It is throwing an ClassCastException which is as follows:
java.lang.ClassCastException: org.eclipse.wst.html.ui.StructuredTextViewerConfigurationHTML cannot be cast to puakma.vortex.editors.pma.parser2.PmaStructuredTextViewerConfiguration
at puakma.vortex.editors.pma.PmaStructuredTextViewer.configure(PmaStructuredTextViewer.java:20)
at org.eclipse.ui.texteditor.AbstractTextEditor.createPartControl(AbstractTextEditor.java:3419)
at org.eclipse.ui.texteditor.StatusTextEditor.createPartControl(StatusTextEditor.java:54)
I have shuffled the contents of PmaStructuredTextViewer for testing purposes and the same error is thrown again and again. I have cleaned and built the project every time. I have even cleared the output folder /bin and cleaned the project. However, the same error is thrown. I have searched the whole system for any compiled class file of the same name but there seems to be none. I suspect eclipse is referencing to the class file that is in a folder oither than /bin which is the default output folder. What am I doing wrong?
I have cleared bin folder and without rebuilding the project, ran it. The project runs fine. How am I supposed to make sure that eclipse loads the correct class files in the bin folder.
public void configure(SourceViewerConfiguration configuration)
{
((PmaStructuredTextViewerConfiguration) configuration).setupReconciler(this);
super.configure(configuration);
}
I even customised the code to the following. Somehow the same exception is thrown. This makes me believe that the newly compiled class file is not being used by eclipse during runtime. Instead, it uses the old class file.
public class PmaStructuredTextViewer extends StructuredTextViewer
{
public PmaStructuredTextViewer(Composite parent, IVerticalRuler verticalRuler, IOverviewRuler overviewRuler, boolean showAnnotationsOverview, int styles)
{
super(parent, verticalRuler, overviewRuler, showAnnotationsOverview, styles);
}
public void configure(SourceViewerConfiguration configuration)
{
if(configuration instanceof PmaStructuredTextViewerConfiguration)
{
((PmaStructuredTextViewerConfiguration) configuration).setupReconciler(this);
super.configure(configuration);
}
}
}
For some reason the test below fails on Windows, but passes on Linux. The test is designed to generate an exception in the code being tested. The exception is basically a file exception. The approach is to make the file unreadable in order to generate the exception. It looks like the setReadable(false) has no affect on Windows.
#Test(dependsOnGroups = "expectedFlow",expectedExceptions = ParserException.class)
#Parameters("unreadableFile")
public void mineDataParserExceptionTest(String unreadableFile) throws ParserException{
AbstractParser parser;
File f = new File(unreadableFile);
f.setReadable(false);
parser = ParserFactory.getParser(ParserFactory.TYPES.SAR);
parser.mine(fileHelper, xml);
}
You should check the return value to see if it succeeded; however, it seems likely that f.setReadable(false, false); might be a better idea, since otherwise it is only supposed to alter the read permission for the owner of the file.
I am writing a "Total Commander" like application in Java. There is quite obvious file system dependency here.
I want to unit test it. I created directory structure for test purposes, I keep it in known location in SVN repository. It works great so far.
Now, I have a method that should ignore hidden files. How can I go about this? Can I mark file hidden in the SVN somehow?
Other solution would be to make one of the files hidden in the build script before running tests, but I'm afraid this would mark file as modified and always show in a commit dialog.
Any suggestions?
I would put all the initialization of a test directory into the tests themselves. And such a case would be simple:
create a directory
put some hidden and visible files into it
test
tear down by removing the directory
Essentially, accessing the file system is a big no-no when unit testing. For starters, these tests are slow(er) than your in-system tests, thus reducing the likelihood of you running your tests at a high frequency (such as with every compilation).
Much better if you use an adapter-pattern to abstract away the access to the file system. I'm a .NET developer so my example will be in C#, but I expect you to be able to translate it simply enough:
public class MyManager
{
private readonly IFileAdapter _fileAdapter;
public MyManager(IFileAdapter fileAdapter)
{
_fileAdapter = fileAdapter;
}
}
public interface IFileAdapter
{
public FileStream Open(string fileName);
public string ReadLine(FileStream fileStream);
// More methods...
}
public class FileAdapter : IFileAdapter
{
public FileStream Open(string fileName)
{
return System.Io.File.Open(fileName);
}
public string ReadLine(FileStream fileStream)
{
return File.Open(fileStream);
}
}
Now, as usual, you can mock the file system, as you would any other class, supplying the returned results. Remember - you are not testing Java's IO classes it is assumed that they work. You are only testing your classes (i.e. MyManager, in the example above).
Leave the tests that actually use the file system to your integration / acceptance tests.
Hope this helps,
Assaf.
I would prefer to abstract file system, so that my unit-test wouldn't require access to real file system. Of course, this abstraction layer must be tested with real file system, but this allow you to reduce dependency on it.
As for storing hidden files in SVN, I second artemb. You should create all files necessary to test in JUnit set up. Presumably, you should prefer setup per test method (#Before and #After). But if you encounter test slowness problems, have a look at #BeforeClass and #AfterClass. I consider they can be used with test suites too.
artemb's answer is correct, you can use #Before and #After to create and remove your structure for each test.
Here is some code I use to create a new directory with some files in it, it will create the directory in the systems temp dir, this is important because depending on the machine your tests will run on, you may will not be allowed to create files or dirs somewhere else. (I had to write this code to allow my tests to be executed on our linux integration machine...)
final String tempdir = System.getProperty("java.io.tmpdir");
final String dirname = Long.toHexString(System.currentTimeMillis());
final File dir = new File(tempdir, dirname);
dir.deleteOnExit();
dir.mkdir();
final String path = dir.getAbsolutePath();
assertTrue(dir.exists());
// pre condition, the directory is empty
assertTrue(dir.list().length == 0);
// create temp files in the directory
final int nbFiles = 3;
for (int i = 0; i < nbFiles; i++) {
(File.createTempFile("test", ".txt", dir)).deleteOnExit();
}
BTW you will have to know on what platform you run to be able to create hiden files...