1

Please help me. I am trying to run a unit test in a Java code (simple code I am running as part of Java trainning), but I don't manage to import the packages related to the annotation "@Test" and method "equalTo()" in AccountTest class (see the code below).

Here is the setup information:

  • I have tried to run this code using IDE's "Eclipse Java Oxygen" and "Eclipse IDE for Java Developers - 2020-03";
  • I have updated the JDK to version 13 (I have updated PATH system variable adding the file directory "C:\Program Files\Java\jdk-13.0.2\bin");
  • I am using JRE 1.8.0_241;
  • OS: Windows 7 Professional;

Code of Unit Test (here is the problem), AccountTest class:

public class AccountTest {

    @Test // Eclipse error reported: "Test cannot be resolved to a type". It does not offer to import the package related to the annotation "@Test"
    public void onWithdrawalBalanceShouldBeReduced() {
        Account account = new Account(200d);
        account.withdraw(50d);

        assertThat(account.getBalance(), is(equalTo(150d))); // Eclipse error reported: "The method equalTo(Double) is undefined for the type AccountTest". It does not offer to import the package related to method "equalTo()"
    }
}

Code for Account Class:

public class Account {

    private double balance;

    public Account(double balance) {
        this.balance = balance;
    }

    public void withdraw(double value) {
        this.balance = this.balance + value;
    }

    public void deposit(double valor) {
        this.balance = this.balance + valor;
    }

    public void setBalance(double saldo) {
        this.balance = saldo;
    }

    public double getBalance() {
        return balance;
    }
}

Code for Bank Class:

public class Bank {

    public void deposit(Account account, double value) {
        account.deposit(value);
    }

    public void doTranference(Account account1, double value, Account account2) {
        account1.withdraw(value);
        account2.deposit(value);
    }
}
1
  • You must have JUnit on the classpath in order to build and run your tests Apr 11, 2020 at 17:13

1 Answer 1

0

You need to make sure that the JUnit library is on your build path. Your Eclipse version has built-in JUnit3, JUnit4, and JUnit5 libraries.

You should try to create a new JUnit test case using menu command: File/New/JUnit Test Case and select one of the following options: New JUnit Test Case Dialog

  • New Junit3 Test
  • New Junit4 Test
  • New Junit Jupiter Test

Type in the class name of your test (and package if needed), press Finish button and you'll get to the dialog to add appropriate JUnit library to your build path. Add Junit4 library to the build path

Press OK button and then you can add/edit your test class.

package my.task;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

class AccountTest {

    @Test
    public void onWithdrawalBalanceShouldBeReduced() {
        Account account = new Account(200d);
        account.withdraw(50d);

        assertEquals("Account balance should be reduced to 150", 150d, account.getBalance(), 0.001);
    }
}

Please also note the following points:

  • method withdraw in Account does not actually subtract the withdrawal amount and the test will fail:
java.lang.AssertionError: Account balance should be reduced to 150 expected:<150.0> but was:<250.0>
    at org.junit.Assert.fail(Assert.java:89)
    at org.junit.Assert.failNotEquals(Assert.java:835)
    at org.junit.Assert.assertEquals(Assert.java:555)
    at my.task.AccountTest.onWithdrawalBalanceShouldBeReduced(AccountTest.java:14)
    ...
  • generally it is not recommended to use floating point type for balance because it may lead to unwanted rounding errors :)

  • if you really need to use assertThat, and hamcrest matchers is() and equalTo, you'll need to fix the imports:

package my.task;

import static org.hamcrest.MatcherAssert.assertThat; 
import static org.hamcrest.CoreMatchers.*;

import org.junit.Test;

public class AccountTest {

    @Test
    public void onWithdrawalBalanceShouldBeReduced() {
        Account account = new Account(200d);
        account.withdraw(50d);

        assertThat(account.getBalance(), is(equalTo(150d)));
    }
}
5
  • Hello, Alex. You solved my problem! Thank you very much! I intentionally made a mistake on withdraw method to get the error during the test. Have a great weekend. :) Apr 11, 2020 at 21:54
  • Alex, what is the 0.001 parameter used for in "assertEquals("Account balance should be reduced to 150", 150d, account.getBalance(), 0.001);"? I tried to open the assertEquals() method to check the documentation, but I did not manage. Apr 11, 2020 at 21:57
  • Should I report your answer as the solution to the problem? If yes, I am not managing to find the option to do that. How do I do that. This is the first question I do in Stack Overflow. Apr 12, 2020 at 0:39
  • Hi Dênis! You're welcome! :) 0.001 is a delta parameter used to compare double values in JUnit. Method assertEquals(double, double) is deprecated. Apr 12, 2020 at 8:03
  • To accept an answer, you should click on the check mark beside an answer to toggle it to green: stackoverflow.com/help/someone-answers Apr 12, 2020 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.