0

I am new to JUnit and spring frameworks, When I was writing test cases to my spring files I ended up getting an error where I couldn't fix it. Here are some snippets attached below ask me if you need more.

Pom.xml

    <!-- Spring framework --> 



<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.6.RELEASE</version>
 </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>

error

java.lang.IllegalStateException: Could not load TestContextBootstrapper [class org.springframework.test.context.support.DefaultTestContextBootstrapper]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.
at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:87)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:102)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:124)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:115)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.NoSuchMethodError: org.springframework.beans.BeanUtils.instantiateClass(Ljava/lang/Class;Ljava/lang/Class;)Ljava/lang/Object;
    at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:81)
    ... 21 more

JunitTestclass.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"file:src/main/resources/config/spring/beans/HibernateSessionFactory.xml",
"file:src/main/resources/config/spring/beans/DataSourceTest.xml",
"file:src/main/resources/com/dynaprice/customer/spring/CustomerBean.xml"})
public class RepricingBeanTest {

@Autowired
RepricingBean beanrep ;}

Thanks ask me any files if you need.

1 Answers1

0

Probably it's a version conflict. You use an old version of base Spring (2.5.6) with a newer version (4.1.6.RELEASE) of other Spring packages. Look at this post: Could not load TestContextBootstrapper - Spring Unit testing

Community
  • 1
  • 1
luke
  • 3,435
  • 33
  • 41
  • No change even I change the old version to something new. – Srikanth Pragallapati Jan 19 '17 at 09:35
  • Have you tried to rerun maven with clean command for example `mvn clean test`? Another thing might be the fact that you don't have to include junit in your dependencies because it is already included in spring-test. Could you post your pom.xml after changes you have made? – luke Jan 19 '17 at 09:48
  • I retested your case in my project. I made a small mistake. You still need junit dependecy, but when you add it to your current pom.xml the pom.xml shoud be ok. I think the problem is there `@ContextConfiguration(locations={ ... })`. It seems that you don't import context properly, so spring doesn't see it. Maybe pathes to files are somehow wrong or configuration in these files is somehow wrong. In my app I have context imported properly and tests work properly, but when I comment out `@ContextConfiguration( ... )` I get the same error as you so thus I think problem lies in a mentioned place – luke Jan 19 '17 at 10:32
  • the same way I commented the @contextconfiguration...ended up with same error! I was frustrating with this bro. – Srikanth Pragallapati Jan 19 '17 at 11:00
  • I have spring configuration in java file like in `AppConfig.java` in https://www.mkyong.com/unittest/junit-spring-integration-example/ but without `@ComponentScan(basePackages = {"com.mkyong.examples.spring"})` So my `AppConfig.java` is: `import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { }`. I have this `AppConfig.java` in src/main/java. And in test I have `@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = AppConfig.class)`. Maybe you could try the same. – luke Jan 19 '17 at 11:24
  • Before it worked for me and once I updated my code from svn this problem came may be version conflict is the main problem. But i cleaned server, project and maven clean but I couldn't resolve the issue. – Srikanth Pragallapati Jan 19 '17 at 11:26
  • I see. I'm sure you will deal with this problem sooner or later. Sometimes you just need some time and often with a fresh mind and knowledge from previous research you suddenly resolve in a short time something that bothered you for hours. I'm affraid that with my current knowledge and without access to full source code I can't give you any more advice. But if you think there is something I can do for you feel free to ask. – luke Jan 19 '17 at 11:47
  • Thanks @luke ping me in skype if possible id: srikanth4372 – Srikanth Pragallapati Jan 19 '17 at 11:55