AssertThat error: Cannot access path (java.nio.file.Path not found) - java

I want to use Robolectric for Unit Testing but I am trying a simple test with robolectric and I am stuck at the beginning. I followed the manual, I did the same with the examples and even other posts couldn't help me. Every time I get the error message: cannot access path. class file for java.nio.file.Path not found.
My build.gradle is:
testCompile "org.robolectric:robolectric:3.3.2"
testCompile "org.robolectric:shadows-support-v4:3.3.2"
testCompile 'junit:junit:4.12'
My Test class is:
package com.dev.mann.chronoly;
import android.support.v7.widget.RecyclerView;
import org.junit.runner.RunWith;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowToast;
import org.robolectric.shadows.support.v4.SupportFragmentTestUtil;
import static
org.robolectric.shadows.support.v4.SupportFragmentTestUtil.startFragment;
#RunWith(RobolectricTestRunner.class)
#Config(constants = BuildConfig.class)
public class CrngyMasterFragmentTestClass{
private CrngyMasterFragment mFrag;
RecyclerView mMainRecyclerView;
MainActivity activity;
#Before
public void setup() {
activity = Robolectric.buildActivity(MainActivity.class).create().start().visible().get();
//SupportFragmentTestUtil.startVisibleFragment(mFrag, MainActivity.class, R.id.master_frag_container);
}
#Test
public void checkEmptyFragments() throws Exception {
//SupportFragmentTestUtil.startVisibleFragment(mFrag, AppCompatActivity.class, R.id.master_frag_container);
assertThat(true);
// check the recyclerview items
//RecyclerView recyclerView = (RecyclerView) mFrag.getActivity().findViewById(R.id.mainRecyclerView);
//assertThat(recyclerView.isShown());
}
}
But every settings doesn't work.
Any help is much appreciated, thanks for any help.

From the comments, the solution was to replace the line
import static org.assertj.core.api.Assertions.assertThat;
with
import static org.assertj.core.api.Java6Assertions.assertThat;
as per the AssertJ documentation.

Related

The method assertThat(List<Flight>) is undefined for the type SpringDataOverviewApplicationTestsJava(67108964)

I am trying to follow along with a pluralsight demo course on JPA and the test I just built is throwing an error on the assertThat method and I can't figure out why. I'm sure this is super obvious, but I can't find the solution on here or on the java documentation examples. Please send help in this new year.
The error message
The method assertThat(List<Flight>) is undefined for the type SpringDataOverviewApplicationTestsJava(67108964)
The code
package com.pluralsight.springdataoverview;
import java.time.LocalDateTime;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import com.pluralsight.springdataoverview.entity.Flight;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.context.SpringBootTest;
#DataJpaTest
class SpringDataOverviewApplicationTests {
#Autowired
private EntityManager entityManager;
#Test
public void verifyFlightCanBeSaved() {
final Flight flight = new Flight();
flight.setOrigin("Amsterdam");
flight.setDestination("New york");
flight.setScheduledAt(LocalDateTime.parse("2011-12-13T12:12:00"));
entityManager.persist(flight);
final TypedQuery<Flight> results = entityManager
.createQuery("SELECT f FROM Flight f", Flight.class);
final List<Flight> resultList = results.getResultList();
assertThat(resultList)
.hasSize(1)
.first()
.isEqualTo(flight);
}
}
Be sure to statically import the used assertion method:
import static org.assertj.core.api.Assertions.assertThat;
Note that the assertj dependency also needs to be present, so declare it e.g. via gradle:
testImplementation("org.assertj:assertj-core:3.22.0")
https://assertj.github.io/doc/

Junit throwing initialization error. Method 'com' not found

All the tests are passing . What should I add for this error to disappear? I am assuming some annotation need to be added. It says Method 'com' not found. I am not able to debug . I dont know what is it talking about.
Code:
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
#RunWith(SpringRunner.class)
#SpringBootTest
public class test {
#Autowired
private JdbcTemplate jdbcTemplate;
#Autowired
private JdbcImpl jdbcImpl;
#Autowired
private MConfiguration config;
#Autowired
private MUtilities util;
#Test
public void testRetervices() throws Exception {
List<Tervice> marketServices = jdbcImpl.retrieveTervices("em", "Coitis");
assertEquals(marketServices.size(), 4);
}
Thanks for your time.
This got fixed when I changed the package from
import org.junit.jupiter.api.Test; to
import org.junit.Test;
Thanks!

I want to fix issue with #Before annotation

This is the #before annotation that I can't solve
#Before
public void initDb() {
{
User newUser = new User("testUser#mail.com", "testUser", "****");
userService.createUser(newUser);
}
{
User newUser = new User("testAdmin#mail.com", "testUser", "****");
userService.createUser(newUser);
}
}
Below are all my imports
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.util.List;
import org.aspectj.lang.annotation.Before;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.leadApp.Entities.Task;
import com.leadApp.Entities.User;
import com.leadApp.services.TaskService;
import com.leadApp.services.UserService;
I would appreciate if someone can give an insight of what next I can do, I am using eclipse Oxygen
You seem to be using JUnit 5 AKA JUnit Jupiter.
The annotation is named BeforeEach, not Before. And of course, it should be imported from an org.junit.jupiter package, not from org.aspectj.lang.annotation, which has nothing to do with JUnit.
There is a guide, you should read it: https://junit.org/junit5/docs/current/user-guide/

Mockito 2 + Junit 5 NoSuchMethodError on AnnotationSupport.findAnnotation

I'm trying to integrate JUnit 5 under Eclipse Oxygen3.
The project already has Mockito 2.
I have done all steps suggested in https://www.baeldung.com/mockito-junit-5-extension like so:
Dependencies:
junit-jupiter-engine 5.5.0
junit-jupiter-api 5.5.0
junit-vintage-engine 5.5.0
junit-platform-runner 1.5.0
mockito-core 2.28.2
mockito-junit-jupiter 2.28.2
Code:
public class Jupiter {
public boolean isAlpha() {
return true;
}
}
Test Code:
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import java.util.Date;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
#ExtendWith(MockitoExtension.class)
#RunWith(JUnitPlatform.class)
public class JupiterTest {
#InjectMocks
private Jupiter jupiter;
#BeforeEach
public void setup() {
//usually some stuff here
}
#Test
#DisplayName("heading jupiter - make it so")
public void test() {
boolean result = jupiter.isAlpha();
assertThat(result).isTrue();
}
}
Unfortunately, running tests fail.
Have anyone stumbled upon a similar problem? Is it something general or my project specific problem?
java.lang.NoSuchMethodError: org.junit.platform.commons.support.AnnotationSupport.findAnnotation(Ljava/util/Optional;Ljava/lang/Class;)Ljava/util/Optional;
at org.mockito.junit.jupiter.MockitoExtension.retrieveAnnotationFromTestClasses(MockitoExtension.java:178)
at org.mockito.junit.jupiter.MockitoExtension.beforeEach(MockitoExtension.java:160)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$null$0(TestMethodTestDescriptor.java:126)
...
Suppressed: java.lang.NullPointerException
at org.mockito.junit.jupiter.MockitoExtension.afterEach(MockitoExtension.java:214)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$null$11(TestMethodTestDescriptor.java:214)
at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$13(TestMethodTestDescriptor.java:226)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAllAfterMethodsOrCallbacks(TestMethodTestDescriptor.java:224)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAfterEachCallbacks(TestMethodTestDescriptor.java:213)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:116)
... 43 more
Not sure why need all that vintage and platform stuff.. Your tests are org.junit.jupiter.api.Test;
I would do this:
1) Remove all the annotations from the class level
2) Init Mockito:
#BeforeEach
public void setup() {
MockitoAnnotations.initMocks(this);
}
3) These deps should be sufficient:
junit-jupiter 5.5.0
mockito-core 2.28.2
mockito-junit-jupiter 2.28.2 allows you to use this way, this may possibly solve your problem.
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoSettings;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.quality.Strictness.LENIENT;
#MockitoSettings(strictness = LENIENT)
public class JupiterTest {
#InjectMocks
private Jupiter jupiter;
#BeforeEach
public void setup() {
//usually some stuff here
}
#Test
#DisplayName("heading jupiter - make it so")
public void test() {
boolean result = jupiter.isAlpha();
assertThat(result).isTrue();
}
}

Method assertThat has complication error in java project

I am trying to compile a java project. I get the following error from my code. What am I doing wrong?
import java.lang.reflect.Field;
import java.util.Map;
import java.lang.Object;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import java.lang.String;
import org.junit.Test;
public class EnvironmentsTest {
#Test
public void testGetFoobar() throws Exception {
assertNull(System.getenv("MY_VAR"));
injectEnvironmentVariable("MY_VAR", "my_var");
assertThat(System.getenv("MY_VAR"), is("my_var"));
}
cannot find symbol
symbol: method is(java.lang.String)
location: class EnvironmentsTest
is is a matcher from Hamcrest. See http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/core/Is.html
import is:
import static org.hamcrest.CoreMatchers.is;
Type is () and press Alt+Enter to click "import static method.."
You can add CoreMatchers.is.
like this

Categories