Unit testing a constructor method - java

I'm having trouble getting my head round how to unit test a constructor method.
I need to check an error is being thrown. The constructor is:
#Autowired
public BankDetailsValidator() {
try {
logDebugMessage("BankDetailsValidator() constructor");
loadModulusWeightTable();
loadSortCodeSubstitutionTable();
} catch (final IOException e) {
throw new BankDetailsValidationRuntimeException("An error occured loading the modulus weight table or sort code substitution table", e);
}
}
To test this, I need to have the loadModulusWeightTable or loadSortCodeSubstitutionTable throw and IOException.
private void loadModulusWeightTable() throws IOException {
modulusWeightTable.clear();
logDebugMessage("Attempting to load modulus weight table " + MODULUS_WEIGHT_TABLE);
final InputStream in = new FileInputStream(MODULUS_WEIGHT_TABLE);
br = new BufferedReader(new InputStreamReader(in));
try {
String line;
while ((line = br.readLine()) != null) {
final String[] fields = line.split("\\s+");
modulusWeightTable.add(new ModulusWeightTableEntry(fields));
}
logDebugMessage("Modulus weight table loaded");
}
finally {
br.close();
}
}
I was trying to use Spy to have the buffered file reader return a IOException but couldn't get it working due to it being in the constructor.
public class BankDetailsValidatorTest {
#Spy
private BufferedReader mockBufferReader;
#InjectMocks
private CDLBankDetailsValidator testSubject;
#Test(expected = IOException.class)
public void testIOErrorLogging() throws Exception{
when(mockBufferReader.readLine()).thenThrow(new IOException());
testSubject = new CDLBankDetailsValidator();
}
}

I think that class BankDetailsValidator should be refactored. In such scenarios you should extract code responsible for reading data to separate class and inject it to BankDetailsValidator as constructor parameter. After that you could test that reader separately and of course test BankDetailsValidator with mocked reader.

Here's how you could refactor your code:
#Autowired
public BankDetailsValidator(Collection<ModulusWeightTableEntry> table) {
try {
logDebugMessage("BankDetailsValidator() constructor");
this.modulusWeightTable.addAll(table);
loadModulusWeightTable();
loadSortCodeSubstitutionTable();
} catch (final IOException e) {
throw new BankDetailsValidationRuntimeException("An error occured loading the modulus weight table or sort code substitution table", e);
}
}
which removes the loading from file part to maybe a #Configuration.
#Configuration
class WeightTableConfiguration {
#Bean
ModulusWeightTable loadFromFile(#Value("${mwt.filename}") String filename) {
Collection<ModulusWeightTableEntry> table = new ArrayList<>();
logDebugMessage("Attempting to load modulus weight table " + filename);
try (InputStream in = new FileInputStream(filename);
br = new BufferedReader(new InputStreamReader(in))) {
String line;
while ((line = br.readLine()) != null) {
final String[] fields = line.split("\\s+");
table.add(new ModulusWeightTableEntry(fields));
}
logDebugMessage("Modulus weight table loaded");
}
return table;
}
}
Now you can test your validator separately from the loading code, and create a separate test configuration providing a manually created weight table.
Or you parameterize your constructor with a Supplier<WeightTable> and put the loading code into the get() method of an implementing #Component class.

Related

How to access value of an abstract method in a different class?

AbstractInputFile is an abstract class and has the following code below:
public abstract class AbstractInputFile {
static final public int MIN_BILLING_CYCLE = 1;
static final public int MAX_BILLING_CYCLE = 12;
private File file;
public AbstractInputFile() {
}
public abstract List<Request> readFile() throws IOException, BarsException;
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
}
CSVInputFileImpl is a subclass of AbstractInputFile, thus inheriting the readFile() method. What it does is it reads the contents of a csv file using a buffered reader, stores it in an arraylist, and returns it.
public List<Request> readFile() throws IOException, BarsException {
List<Request> requests = new ArrayList<>();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
String line = "";
//READ CSV FILE
try {
BufferedReader br = new BufferedReader(new FileReader(getFile()));
int row = 1;
int index = 0;
while ((line = br.readLine()) != null) {
String[] data = line.split(",", 3);
//Validate if billing cycle ranges 1-12
try {
int billingCycle = Integer.parseInt(data[0]);
if(billingCycle <= MAX_BILLING_CYCLE && billingCycle >= MIN_BILLING_CYCLE) {
request.setBillingCycle(billingCycle);
} else {
log.error(BILLING_CYCLE_NOT_ON_RANGE + row);
throw new BarsException(BILLING_CYCLE_NOT_ON_RANGE + row);
}
} catch (BarsException e) {
log.error(INVALID_BILLING_CYCLE + row);
throw new BarsException(INVALID_BILLING_CYCLE + row);
}
//Validate start date
try {
LocalDate startDate = LocalDate.parse(data[1], formatter);
request.setStartDate(startDate);
} catch (DateTimeParseException e) {
log.error(INVALID_START_DATE_FORMAT + row);
throw new BarsException(INVALID_START_DATE_FORMAT + row);
}
//Validate end date
try {
LocalDate endDate = LocalDate.parse(data[2], formatter);
request.setStartDate(endDate);
} catch (DateTimeParseException e) {
log.error(INVALID_END_DATE_FORMAT + row);
throw new BarsException(INVALID_END_DATE_FORMAT + row);
}
row++;
//Add the requests to array list created previously
requests.add(index,new Request(request.getBillingCycle(), request.getStartDate(), request.getEndDate()));
index++;
}
//Validate if file exists
} catch (FileNotFoundException e) {
throw new BarsException(NO_SUPPORTED_FILE);
} catch (IOException e) {
throw new IOException(PATH_DOES_NOT_EXIST);
}
return requests;
}
In another public class FileProcessor, I am instructed to get the array list using List<Request> requests = abstractInputFile.readFile(); but is unable to do so because I am getting the error Non-static method 'readFile()' cannot be referenced from a static context.
public List<Request> execute(File file) throws BarsException {
InputFileFactory fact = InputFileFactory.getInstance();
try {
fact.getInputFile(file);
} catch (BarsException e) {
throw new BarsException(NO_SUPPORTED_FILE);
}
List<Request> requests = AbstractInputFile.readFile();
return requests;
}
My question is: How will I be able to retrieve the arraylist and return it in the execute(file) method? There might be a lot more errors in other areas of the code and logic but I'd appreciate any help and suggestions.
First things first, AbstractInputFile.readFile() can't be called for 2 reasons: readFile() in the AbstractInputFile class is abstract, and it is an instance method.
From what you wrote here, you implement the readFile() method in the subclass, CSVInputFileImpl. Therefore, in this case, your readFile() calls should be based on THIS class, not AbstractInputFile. However, this is still classified as an instance method, meaning you need to first create an instance of CSVInputFileImpl before you can call readFile(). I would correct the code like this (I'm making assumptions that the CSVInputFileImpl class is structured the same as AbstractInputFile, but make sure the file instance var is set):
CSVInputFileImpl csvInputFileImpl = new CSVInputFileImpl();
csvInputFileImpl.setFile(file);
List<Request> requests = csvInputFileImpl.readFile();
return requests;

Why is this Class' object not serialized properly in different processes?

Context
I made a Java application, and need to run two instances of that application, synchronizing some of their attributes via socket each time there's some change. To communicate those changes, Serializable objects are sent through a socket using ObjectStreams (input and output) using read/writeUTF() for an identifier, and read/writeObject() and flush(). The app is the exact same .jar, run twice with some changes like having different ports and ip (if necessary).
Problem
I noticed that objects of some of my classes (e.g. Notification) were sent and received without any troubles, but objects from another class (RegisteredUsers) weren't sent (or received) properly. So I ran some tests to send objects between the two apps and found that the object is being sent and isn't null, it's attribute (a HashMap<String,User>) is also being sent and isn't null, but is always empty.
So I decided to scale it down to what the problem was exactly: I'm trying to write an object through a Stream, and read it in a different process of the same .jar, and with most classes it seems to work, but it doesn't with one.
There seems to be something I'm missing or don't understand about this serialization process, if the object is written and read during the execution of the same process it works, but not if this object is read on another instance of the same app. I even added a HashMap to Notification with the same creation process, but it still works, I really don't get it, what am I missing?
Code
I have taken some code from the bigger app and trimmed it down to the basic problem if anyone wants to test it. To reproduce the errors, run Main1, which will create the two files with an object persisted in each one (one with a Notification object and the other with a RegisteredUsers object) and shows their information, then Main2, which reads them from the files and shows their information, and the problem should be printed. That being that reg3's HashMap is empty and thus neither of the Users are registered.
Main1
public class Main1 {
public static void main(String[] args) {
String regFile = "registry.txt";
String notificationFile = "notification.txt";
Persistence pers = new Persistence();
RegisteredUsers reg1 = new RegisteredUsers();
RegisteredUsers reg2 = new RegisteredUsers();
reg1.register("Name1", "127.0.0.1");
reg1.register("Name2", "127.0.0.1");
try {
pers.writeReg(reg1, regFile);
} catch (IOException e) {
System.out.println("Error writing registry.");
}
try {
reg2 = pers.readReg(regFile);
} catch (IOException e) {
System.out.println("Error reading registry.");
}
System.out.println("Original registry: ");
System.out.println(reg1.isRegistered("Name1") + " " + reg1.isRegistered("Name2"));
System.out.println("Registry read from file: ");
System.out.println(reg2.isRegistered("Name1") + " " + reg2.isRegistered("Name2"));
Notification noti1 = new Notification("Name", "127.0.0.1");
Notification noti2 = new Notification(); //not necesary but it's the way it's done in the bigger app.
try {
pers.writeNotif(noti1, notificationFile);
} catch (IOException e) {
System.out.println("Error writing notification.");
}
try {
noti2 = pers.readNotif(notificationFile);
} catch (IOException e) {
System.out.println("Error reading notification.");
}
System.out.println("Original notification: ");
System.out.println(noti1.getAttributes().get(0) + " " + noti1.getAttributes().get(1));
System.out.println(noti1.getMap());
System.out.println("Notification read from file: ");
System.out.println(noti2.getAttributes().get(0) + " " + noti2.getAttributes().get(1));
System.out.println(noti2.getMap());
}
}
Main2
public class Main2 {
public static void main(String[] args) {
String regFile = "registry.txt";
String notificationFile = "notification.txt";
Persistence pers = new Persistence();
RegisteredUsers reg3 = new RegisteredUsers();
try {
reg3 = pers.readReg(regFile);
} catch (IOException e) {
System.out.println("Error reading registry.");
}
if (reg3 == null) {
System.out.println("reg3 is null");
}
if (reg3.getMap() == null)
System.out.println("reg3 has a null map");
if (reg3.getMap().isEmpty())
System.out.println("reg3 has an empty map");
System.out.println("Registry read from file on another process: ");
System.out.println(reg3.isRegistered("Name1") + " " + reg3.isRegistered("Name2"));
Notification noti3 = new Notification(); //not necesary but it's the way it's done in the bigger app.
try {
noti3 = pers.readNotif(notificationFile);
} catch (IOException e) {
System.out.println("Error reading notification.");
}
System.out.println("Notification read from file on another process: ");
System.out.println(noti3.getAttributes().get(0) + " " + noti3.getAttributes().get(1));
System.out.println(noti3.getMap());
}
}
A Class to persist the objects in the files:
public class Persistence {
public void writeReg(RegisteredUsers regus, String file) throws IOException {
try(FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);) {
oos.writeObject(regus);
oos.flush();
}
}
public RegisteredUsers readReg(String file) throws IOException {
RegisteredUsers regus = null;
try(FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);) {
regus = (RegisteredUsers) ois.readObject();
} catch (ClassNotFoundException e) {
System.out.println("Wrong class.");
}
return regus;
}
public void writeNotif(Notification regus, String file) throws IOException {
try(FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);) {
oos.writeObject(regus);
oos.flush();
}
}
public Notification readNotif(String file) throws IOException {
Notification notif = null;
try(FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);) {
notif = (Notification) ois.readObject();
} catch (ClassNotFoundException e) {
System.out.println("Wrong class.");
}
return notif;
}
}
RegisteredUsers
public class RegisteredUsers implements Serializable {
private static HashMap<String, User> users;
public RegisteredUsers() {
users = new HashMap<String, User>();
}
public HashMap<String, User> getMap() {
return users;
}
public boolean isRegistered(String name) {
User us = users.get(name);
return us != null;
}
public void register(String name, String ip) {
users.put(name, new User(name, ip, false));
}
}
Notification
public class Notification implements Serializable {
private ArrayList<String> attributes;
private HashMap<String, User> map = new HashMap<>();
public Notification() {
}
public Notification(String name, String ip) {
attributes = new ArrayList<String>();
attributes.add(0, name);
attributes.add(1, ip);
map.put(ip, new User(name, ip, false));
}
public ArrayList<String> getAttributes() {
return attributes;
}
public HashMap<String, User> getMap() {
return map;
}
}
User
public class User implements Serializable {
private String name;
private String ip;
private boolean connection_state;
public User(String name, String ip, boolean connection_state) {
this.name = name;
this.ip = ip;
this.connection_state = connection_state;
}
}
In java static fields are implicitly transient, and transient fields are not serialized.
If you modify the RegisterdUsers to
public class RegisteredUsers implements Serializable {
private HashMap<String, User> users; // static modifier is removed
...
}
The serialization will work.

Testing a method that reads and processes data from a file [duplicate]

This question already has answers here:
Mocking Files in Java - Mock Contents - Mockito
(3 answers)
Closed 6 years ago.
I have the following code:
public class FolderServiceImpl implements FolderService {
private static final Logger L = LoggerFactory.getLogger(FolderServiceImpl.class);
public int getStatus(String folderPath) {
int status = 0;
File folderStatusFile = new File(folderPath, ".folderstatus");
if (folderStatusFile.exists()) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(folderStatusFile));
String line = br.readLine();
status = Integer.parseInt(line);
} catch (Exception e) {
L.error("can't read file " + folderStatusFile.getAbsolutePath(), e);
status = 4;
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
L.warn("could not close reader ", e);
}
}
}
} else {
status = 3;
}
return status;
}
}
I want to test this method without creating actual files for every case. I should be using Java 1.7, JUnit 4, Mockito and/or PowerMockito.
Any ideas on how to do that?
I am talking about mocking either the data source or simply changeing the input for the method.
My test looks something like this:
`#Rule
public TemporaryFolder folder = new TemporaryFolder();
private FolderServiceImpl serviceToTest = new FolderServiceImpl();
private String folderPath;
#Before
public void setUp() {
folderPath = folder.getRoot().getAbsolutePath();
try {
folder.newFile(".folderstatus");
} catch (IOException e) {
e.printStackTrace();
}
}
#Test
public void shouldReturnFolderStatus3WhenFolderStatusIsNotFound() {
// given
deleteFolderStatusFile();
// actual
int status = serviceToTest.getFolderStatus(folderPath);
// expected
assertEquals(3, status);
}
#Test
public void shouldReturnFolderStatus4WhenTheStatusIsUnreadable() {
// given
writeStatusToTestFile("Test");
// actual
int status = serviceToTest.getFolderStatus(folderPath);
// expected
assertEquals(4, status);
}
#Test
public void shouldReturnFolderStatusInTheFile() {
// given
writeStatusToTestFile("1");
// actual
int status = serviceToTest.getFolderStatus(folderPath);
// expected
assertEquals(1, status);
}
private void writeStatusToTestFile(String status) {
Path file = Paths.get(folder.getRoot().getAbsolutePath(), ".folderstatus");
try {
Files.write(file, status.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
private void deleteFolderStatusFile() {
Path file = Paths.get(folder.getRoot().getAbsolutePath(), ".folderstatus");
try {
Files.delete(file);
} catch (IOException e) {
e.printStackTrace();
}
}`
Although the answer of #BenHeid may work I'd suggest to change to different approach.
IMHO when ever I use PowerMock(-ito) it is a surrender to bad design.
Also the PowerMock solution will confuse test coverage tools since it changes the Applications byte code after it has been instrumented for coverage measurement.
So the approach I'd prefer is to stick to Clean Code and OOP rules.
One of them is separation of concerns.
In your case the method creates some infrastructure classes (dependencies) to work with, namely FileReader and BufferedReader.
But the instantiation of (direct) dependencies is not a responsibility of a class containing business logic.
Therefore I'd suggest to refactor that code out into a separate class:
class ReaderFactory {
public BufferedReader createFor(File file) throws FileNotFoundException {
return new BufferedReader(new FileReader(file));
}
}
Your Class would change to this:
class FolderServiceImpl {
private static final Logger L = LoggerFactory.getLogger(FolderServiceImpl.class);
private final ReaderFactory readerFactory;
FolderServiceImpl(ReaderFactory readerFactory) {
this.readerFactory = readerFactory;
}
public int getStatus(String folderPath) {
int status = 0;
File folderStatusFile = new File(folderPath, ".folderstatus");
// try "with resource" takes care of closing the reader
try (BufferedReader br = readerFactory.createFor(folderStatusFile);) {
String line = br.readLine();
status = Integer.parseInt(line);
} catch (IOException e) {
status = 3;
} catch (Exception e) {
L.error("can't read file " + folderStatusFile.getAbsolutePath(), e);
status = 4;
}
return status;
}
}
And your Test would be this:
public class FolderServiceImplTest {
private static final String ANY_FILE_NAME = "";
#Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();
#Rule
public ExpectedException thrown = ExpectedException.none();
#Mock
private ReaderFactory readerFactory;
#InjectMocks
FolderServiceImpl sut;
#Test
public void getStatus_FileNotExisting_returnStatus3() throws Exception {
// arrange
Mockito.doThrow(new FileNotFoundException("UnitTest")).when(readerFactory).createFor(Mockito.any(File.class));
// act
int status = sut.getStatus(ANY_FILE_NAME);
// assert
Assert.assertThat("status",status,CoreMatchers.equalTo(3));
}
#Test
public void getStatus_ValidFile_returnFileContentAsInt() throws Exception {
// arrange
BufferedReader bufferedReader = Mockito.mock(BufferedReader.class);
Mockito.doReturn(bufferedReader).when(readerFactory).createFor(Mockito.any(File.class));
Mockito.doReturn("4711").when(bufferedReader).readLine();
// act
int status = sut.getStatus(ANY_FILE_NAME);
// assert
Assert.assertThat("status",status,CoreMatchers.equalTo(4711));
}
}
You have to use something like this:
#RunWith(PowerMockRunner.class)
#PrepareForTest(tests.class)
public class test {
#Test
public void test() throws Exception {
File fileMock = Mockito.mock(File.class);
PowerMockito.whenNew(File.class).withArguments(Mockito.anyString(), Mockito.anyString()).thenReturn(fileMock);
FolderServiceImpl sut = new FolderServiceImpl sut ();
Mockito.when(fileMock.exists()).thenReturn(true);
sut.getStatus("");
// Your verifications..
}
}
Powermock will mock the File object which is created in the method getStatus of your class. With Mockito.when you can say what is the return value of folderStatusFile.exists() in your code.
EDIT
I have included the following two jars with maven, but you don't need to use maven: https://mvnrepository.com/artifact/org.powermock/powermock-module-junit4/1.4.6 and https://mvnrepository.com/artifact/org.powermock/powermock-api-mockito/1.4.9 and https://mvnrepository.com/artifact/org.mockito/mockito-all/1.10.19

How to use a Path object as a String

I'm looking to try and create a Java trivia application that reads the trivia from separate question files in a given folder. My idea was to use the run() method in the FileHandler class to set every text file in the folder into a dictionary and give them integer keys so that I could easily randomize the order at which they appear in the game. I found a simple chunk of code that is able to step through the folder and get the paths of every single file, but in the form a Path class. I need the paths (or just the names) in the form a String class. Because I need to later turn them into a file class (which excepts a String Constructor, not a Path). Here is the chunk of code that walks through the folder:
public class FileHandler implements Runnable{
static Map<Integer, Path> TriviaFiles; //idealy Map<Integer, String>
private int keyChoices = 0;
public FileHandler(){
TriviaFiles = new HashMap<Integer, Path>();
}
public void run(){
try {
Files.walk(Paths.get("/home/chris/JavaWorkspace/GameSpace/bin/TriviaQuestions")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
TriviaFiles.put(keyChoices, filePath);
keyChoices++;
System.out.println(filePath);
}
});
} catch (FileNotFoundException e) {
System.out.println("File not found for FileHandler");
} catch (IOException e ){
e.printStackTrace();
}
}
public static synchronized Path getNextValue(){
return TriviaFiles.get(2);
}
}
There is another class named TextHandler() which reads the individual txt files and turns them into questions. Here it is:
public class TextHandler {
private String A1, A2, A3, A4, question, answer;
//line = null;
public void determineQuestion(){
readFile("Question2.txt" /* in file que*/);
WindowComp.setQuestion(question);
WindowComp.setAnswers(A1,A2,A3,A4);
}
public void readFile(String toRead){
try{
File file = new File("/home/chris/JavaWorkspace/GameSpace/bin/TriviaQuestions",toRead);
System.out.println(file.getCanonicalPath());
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
question = br.readLine();
A1 = br.readLine();
A2 = br.readLine();
A3 = br.readLine();
A4 = br.readLine();
answer = br.readLine();
br.close();
}
catch(FileNotFoundException e){
System.out.println("file not found");
}
catch(IOException e){
System.out.println("error reading file");
}
}
}
There is stuff I didn't include in this TextHandler sample which is unimportant.
My idea was to use the determineQuestion() method to readFile(FileHandler.getNextQuestion).
I am just having trouble working around the Path to String discrepancy
Thanks a bunch.
You can simply use Path.toString() which returns full path as a String. But kindly note that if path is null this method can cause NullPointerException. To avoid this exception you can use String#valueOf instead.
public class Test {
public static void main(String[] args) throws NoSuchFieldException, SecurityException {
Path path = Paths.get("/my/test/folder/", "text.txt");
String str = path.toString();
// String str = String.valueOf(path); //This is Null Safe
System.out.println(str);
}
}
Output
\my\test\folder\text.txt

getMethod return null java

I wrote getMethod in the file MovieReader and if I print this method inside this file everything is working well.
import java.io.BufferedReader; // scanner
import java.io.FileReader;
public class MovieReader {
private static String text;
public static void main(String args[]) throws Exception {
FileReader file = new FileReader("C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt");
BufferedReader reader = new BufferedReader(file);
text = "";
String line = reader.readLine();
while(line != null) {
text+= line +"\n";
line=reader.readLine();
}
reader.close();
System.out.println(getText()); // This method works
}
public static String getText() {
return text;
}
}
But when I'm trying to call this method from other file it's printing null
public class Userr{
public static void main(String args[]){
MovieReader user = new MovieReader();
System.out.println(user.getText());
}
}
Can you help me with it?
In the MovieReader class you load the file and fill the contents of text in the main() method. When you create a new MovieReader object, the main() method is not executed, so the text field is not initialized.
You can create a static loader method in MovieReader and move the code from main() to there, like this:
public static void loadMovieInfo() {
FileReader file = new FileReader("C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt");
... // rest of the code
reader.close();
}
Just call this before trying to call getText():
MovieReader.loadMovieInfo();
System.out.println(MovieReader.getText());
If you want the file to be loaded and the content of text to be filled when the object is created, you can turn text into an instance variable and load the file info in the MovieReader constructor.
Example:
public class MovieReader {
private String text;
public MovieReader() {
FileReader file = new FileReader("C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt");
BufferedReader reader = new BufferedReader(file);
this.text = "";
String line = reader.readLine();
while(line != null) {
this.text += line +"\n";
line=reader.readLine();
}
reader.close();
}
public String getText() {
return this.text;
}
}
Then this should work:
MovieReader user = new MovieReader();
System.out.println(user.getText());
Also, a couple of observations:
Static methods belong to the class (not to a particular object), and should be called with the name of the class:
MovieReader.getText()
You should use a StringBuilder (docs here) instead of String concatenation to fill the contents of the text variable.
Try this one.
import java.io.BufferedReader; // scanner
import java.io.FileReader;
public class MovieReader {
private static String text;
public static String getText() {
FileReader file = new FileReader("C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt");
BufferedReader reader = new BufferedReader(file);
text = "";
String line = reader.readLine();
while(line != null) {
text+= line +"\n";
line=reader.readLine();
}
reader.close();
System.out.println(getText()); // This method works
return text;
}
}
public class Userr{
public static void main(String args[]){
MovieReader user = new MovieReader();
System.out.println(user.getText());
}
}
The fast and dirty fix: Call the MovieReader.main method.
The longer answer, how you should actually do it:
You probably come from a scripting background like python. What you did here was to create two scripts, basically, wrapped in classes. When you call java, you have one class as entry point, whose main method is called.
So you created one script that loads a file, and another script that reads it, and your expectation is that both main methods are called. You need to go back to design!
The better way would be to only have a minimal main() in MovieReader, and instead have a method like readMovies(), which the main() calls. Then have User.main() call that method as well, before calling getText().
Don't put all the logic in main
First of all, you should call static getText() method with class name.
MovieReader.getText()
Second, default value of static string:
It's initialized to null if you do nothing, as are all reference types.
As, you are not doing anything that's why the value of text is null.
Refer the following fixed code:
MovieReader class
public class MovieReader {
private static String text;
public static void main(String args[]) throws Exception {
FileReader file = new FileReader("C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt");
BufferedReader reader = new BufferedReader(file);
text = "";
String line = reader.readLine();
while(line != null) {
text+= line +"\n";
line=reader.readLine();
}
reader.close();
System.out.println(getText()); // This method works
}
public static String getText() {
return text;
}
}
Userr class:
public class Userr{
public static void main(String args[]){
try {
MovieReader.main(null);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(MovieReader.getText());
}
}
Assuming that you are running the main() method of Userr class.
main() method and getText() method of the class MovieReader are independent of each other. If you are calling getText() method, it will return the value of text variable without any operations on it, cos operations of main() method [ of MovieReader class ] are not going to execute. That's why you are not getting intended result.
try to re factor your code as below.
public class Movie {
public static void main(String[] args) {
MovieReader user = new MovieReader();
String file = "C:/Users/krystian/Desktop/filmDateBaseProject/movies.txt";
System.out.println(user.getText(file));
}
}
and the MovieReader class,
class MovieReader {
private String text;
String getText(String fileName) {
FileReader file;
file = null;
try {
file = new FileReader(fileName);
BufferedReader reader = new BufferedReader(file);
text = "";
String line = reader.readLine();
while (line != null) {
text += line + "\n";
line = reader.readLine();
}
reader.close();
return text;
} catch (FileNotFoundException ex) {
Logger.getLogger(MovieReader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MovieReader.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
file.close();
} catch (IOException ex) {
Logger.getLogger(MovieReader.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}
}
its always a good practice to use exception handling.

Categories