Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Unit Testing in Java How to Test Running Tests and Reading Output

Edward Strudwick
Edward Strudwick
2,036 Points

JUnit5: 'fail();' returns error: "java: cannot access java.util.function.Supplier..."

As above, running the test works fine and gives a green passed return. When I try to run fail() as in the video, I get the following error:

java: cannot access java.util.function.Supplier class file for java.util.function.Supplier not found

Google has been no help on this one I don't understand the error at all.

The same problem with the same tutorial was asked a year ago here: https://teamtreehouse.com/community/java-package-javautilfunction-does-not-exist

I'm assuming it's because the tutorial is Junit4 and there's some bit missing.

Getting more than a little fed up with issues on this Java course, that have no answers and no fix over a year on (have TTreehouse just left the Java course out to die a slow death?!?) . So any help much appreciated.

Do you have import static org.junit.Assert.*; and import org.junit.Test;?

Edward Strudwick
Edward Strudwick
2,036 Points

Hi - it is different with JUnit5 - but yes I have these:

import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*;

I have the same imports as you, but my code is not giving me unexpected errors yet (it's failing the test like it's supposed to). One of my project's Test Dependencies is junit-jupitar-api-5.6.0.jar, so I think I'm on junit5.

What does your code look like? Inside my test class, I have

@Test
public void testThisTestMustFail() throws Exception {
    fail("Failing test here.");
}

It also fails properly for me if I don't throw the Exception.

Edward Strudwick
Edward Strudwick
2,036 Points

Cheers jb30 for your help...see below.

1 Answer

Edward Strudwick
Edward Strudwick
2,036 Points

So I'll answer this myself as I have figured it out, in case it helps anyone else going forwards. Google told me: "Supplier<T> is an in-built functional interface introduced in Java 8 in the java.util.function package"

Despite only having the latest JDK, for some reason when I used the tutorial files from GitHub the Project SDK was set to a ridiculously early version and the language level was too. Under Project structure, once I changed the JDK to 16, and also set the language level to either 8 or 16, then fail() worked without the error. So it is a language level issue I believe to be aware of if you are using the current files from Github.