#Spring MVC: Failed to discover tests java.lang.NoClassDefFoundError: org.junit.jupiter.api.io.Cleanu

1 messages ยท Page 1 of 1 (latest)

shrewd daggerBOT
#

Detected code, here are some useful tools:

Formatted code
package com.example;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders. * ;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * ;
import org.junit.jupiter.api.BeforeEach;
import org.junit.Test;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@SpringJUnitWebConfig(locations = "test-servlet-context.xml") class ExampleTests {
  MockMvc mockMvc;
  @BeforeEach
  void setup(WebApplicationContext wac) {
    this .mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  }
  @Test
  public void testHomePage() throws Exception {
    mockMvc.perform(get("/AccessPoint")).andExpect(status().isOk()).andExpect(view().name("login"));
  }
}
#

<@&1004656351647117403> please have a look, thanks.

surreal canopy
#

What does hover over mean? I guess you are in an IDE. Which one are you using?

#

You are not using Spring Boot?

grizzled coyote
#

Spring MVC: Failed to discover tests java.lang.NoClassDefFoundError: org.junit.jupiter.api.io.Cleanu

surreal canopy
#

the error you have in the title has nothing to do with not finding your tests

#

can you show the content of your pom.xml / gradle.build file? The full error would be nice too ๐Ÿ˜„

#

spring boot would solve this problem for you with its parent pom btw. cough cough ^^

#

I guess you messed up some of the junit dependencies and now you have conflicts

surreal canopy
#

aah I see... too bad ๐Ÿ˜ฆ

surreal canopy
#

So, what's the full error? How did you define the junit dependencies in your pom.xml?

surreal canopy
#

let's start with all dependency definitions regarding JUnit; maybe it's enough ^^

shrewd daggerBOT
#

Detected code, here are some useful tools:

surreal canopy
#

I'm already seeing 2 different major versions for JUnit

#

4 and 5

#

you should choose one ^^

#
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.11.1</version>
    <scope>test</scope>
</dependency>

Should be enough; I guess you can remove everything else ^^

shrewd daggerBOT
surreal canopy
#

ya; let's try ๐Ÿ˜„

#

yaps

surreal canopy
#

you are using the wrong annotation

#

this is the old JUnit 4 one

#

org.junit.jupiter.api.Test

#

I would recommend to do so, yes ๐Ÿ˜„

#

yap this here:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.11.1</version>
    <scope>test</scope>
</dependency>
surreal canopy
#

don't use this annotation; use org.junit.jupiter.api.Test

#

org.junit.Test is JUnit 4 ^^

#

replace the import and remove the JUnit 4 dependency from the pom

#

instead of import org.junit.Test; use import org.junit.jupiter.api.Test;

#

whoop whoop ๐Ÿ˜„

shrewd daggerBOT
#

Detected code, here are some useful tools:

surreal canopy
#

AFAIK; you can either define your bean config in a XML file or in a Java class via code

#

when you have it in an xml file use this location thingy

#
@SpringJUnitWebConfig(ExampleTests.Config.class)
class ExampleTests {

    MockMvc mockMvc;

    @BeforeEach
    void setup(WebApplicationContext wac) {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    public void testHomePage() throws Exception {
        mockMvc.perform(get("/AccessPoint"))
            .andExpect(status().isOk())
            .andExpect(view().name("login"));
    }
    
    @Configuration
    static class Config {
    }
}
surreal canopy
#

I would recommend Java classes and code ๐Ÿ™‚

#

the xml way is very oldskool xD

surreal canopy
#

this is outdated ^^

#

also looks like Spring Boot is in use

#

nobody (tm) uses Spring without spring boot nowadays xD

#

too much pain to do everything by yourself

#

maintaining old crappy stuff too xD

#

Spring Boot also lowers complexity by a lot; and complexity is the biggest enemy of every dev :>

#

but ya; I guess you can't do much about it and have to life with it somehow :x

#

I'm not rly sure; as this is also mostly done by Spring Boot for you =/

#

I've never used this kind of config ^^

#

maybe you have to wire up all your beans in there? or just the ones which differ from the normal beans?

surreal canopy
#

what's the error?

#

I guess you have @Controllers?

#

you have to configure these in the context

#

is my guess xD

#

your controllers maybe aren't loaded

#

yes

#

later you could extract this Config class into it's own file and share it with multiple tests when needed

surreal canopy
#

yes; but this is highly dangerous half knowledge from me ^^

#

maybe you can check other projects in your company how they did testing

#

this is spring boot xD

#

are you using Spring v 5.x.x?

#

because documentation there still uses JUnit 4 :x

#

maybe you have to fall back to JUnit 4 T_T

#

yap

#

remember to change the Test import annotation thingy again ^^

#

this wont work ๐Ÿ˜„

#
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.13</version>
  <scope>test</scope>
</dependency>
#

yes

#

xDD

#

seems so xDD

#

hmmm you won't get an example that easily out of it I guess

#

aaah darrn...

#

you will have a rly hard time without all the help of spring boot T_T

#

you need some rly old dude teaching you how vanilla spring works ^^

#

I initially started learning with pure spring like 15 years ago... but stuff was different 15 years ago xD

#

tell your boss; if they keep this stack... they will run out of developers able to maintain their code base ^^

#

without seeing the code base it's hard to tell anything xD

#

generally it will cost more money to maintain an old code base than rewriting / migrating it in the long run

#

banks for example have a lot of their code written in COBOL; nearly nobody knows how to write COBOL anymore; ppl get payed like 200k a year for writing this old crap ^^

#

finding and paying ppl knowing how Spring vanilla works isn't ez I would say :x

#

I work mostly remotely from home ^^

#

but I'm permanently employed and visit the office each wednesday

#

nope it's gotten worse xD

surreal canopy
#

nope I'm working with kubernetes in gcp ^^

surreal canopy
#

your only option is trial and error ^^

#

they use different annotations for their tests than you; but I'm also not sure what's the correct one for vanilla spring

#

you have to look through the spring source code and try to understand why things happening and what you have to do

#

when you are lucky there's some inline code docu o.o

#

I would demand a pay raise for doing this xD

surreal canopy
#

germany ^^

surreal canopy
#

you can try to add a @Configuration class with a component scan

#
@Configuration
@ComponentScan(basePackages = {
    "your.package"
})
public class TestConfig {

}
#

maybe this will load the controllers :x

surreal canopy
#

the @ComponentScan will look for classes annotated with @Service, @Component, @Controller, @RestController, .... and load them as a bean

#

this will basically do the magic for you ๐Ÿ˜„

surreal canopy
#

yes

surreal canopy
#

can you @Autowire your controller into the test? is it null or is there an instance in it?

#

does the route match you defined in the controller?

surreal canopy
#

into the class ^^

shrewd daggerBOT
#

Detected code, here are some useful tools:

surreal canopy
#

the controller package isn't com.example :x

#

you have to scan the package where your code lives ^^

#

and your test packages should reflect your production code packages

#

don't call them com.example when it is com.<organisation-name> ^^

#

how green are you actually? don't get me wrong; but you seem to be very junior; and you should definitely have a senior dev supporting you o.o

#

don't let them exploit you, because you are cheaper than a senior dev or smth xD

#

"green horn" is a common phrase I guess for somebody new ^^

#

yes ^^

#

I'm worried that you are being exploited for cheap money T_T

#

but you did code in Java before?

#

aaaah

#

than this is a very hard task T_T

#

sure ๐Ÿ˜„

shrewd daggerBOT
#

@grizzled coyote

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure ๐Ÿ‘